From 50077e7d5e52953abe5df05fc366e6789c8d2f5d Mon Sep 17 00:00:00 2001 From: Andrew Rouse Date: Wed, 15 Jan 2025 16:10:25 +0000 Subject: [PATCH] Permit Callback.pathItemRef to be any JSON path Change processing of Callback.pathItemRef so that it doesn't always prefix the value with #/components/pathItems/ when adding the reference to the model. --- .../runtime/io/callbacks/CallbackIO.java | 2 +- .../runtime/scanner/CallbackScanTests.java | 28 ++++++++++ ...callback.reference-component-pathitem.json | 53 +++++++++++++++++++ .../smallrye/openapi/model/ReferenceType.java | 24 ++++++--- 4 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 extension-jaxrs/src/test/resources/io/smallrye/openapi/runtime/scanner/callback.reference-component-pathitem.json diff --git a/core/src/main/java/io/smallrye/openapi/runtime/io/callbacks/CallbackIO.java b/core/src/main/java/io/smallrye/openapi/runtime/io/callbacks/CallbackIO.java index cbabffe16..1372c1ecb 100644 --- a/core/src/main/java/io/smallrye/openapi/runtime/io/callbacks/CallbackIO.java +++ b/core/src/main/java/io/smallrye/openapi/runtime/io/callbacks/CallbackIO.java @@ -32,7 +32,7 @@ public Callback read(AnnotationInstance annotation) { callback.setRef(ReferenceType.CALLBACK.refValue(annotation)); Optional.ofNullable(this. value(annotation, PROP_PATH_ITEM_REF)) - .map(ReferenceType.PATH_ITEM::referenceOf) + .map(ReferenceType.PATH_ITEM::parseRefValue) .ifPresent(ref -> callback.addPathItem( value(annotation, PROP_CALLBACK_URL_EXPRESSION), OASFactory.createPathItem().ref(ref))); diff --git a/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/CallbackScanTests.java b/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/CallbackScanTests.java index 2752a9131..1519568d5 100644 --- a/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/CallbackScanTests.java +++ b/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/CallbackScanTests.java @@ -9,6 +9,8 @@ import org.eclipse.microprofile.openapi.OASConfig; import org.eclipse.microprofile.openapi.annotations.Components; import org.eclipse.microprofile.openapi.annotations.OpenAPIDefinition; +import org.eclipse.microprofile.openapi.annotations.PathItem; +import org.eclipse.microprofile.openapi.annotations.PathItemOperation; import org.eclipse.microprofile.openapi.annotations.callbacks.Callback; import org.eclipse.microprofile.openapi.annotations.callbacks.CallbackOperation; import org.eclipse.microprofile.openapi.annotations.info.Info; @@ -47,4 +49,30 @@ public String hello() { printToConsole(result); assertJsonEquals("callback.reference-component-callback.json", result); } + + @Test + void testCallbackPathItemReference() throws Exception { + @OpenAPIDefinition(info = @Info(title = "Greetings", version = "0.0.1"), components = @Components(pathItems = @PathItem(name = "myPathItem", operations = @PathItemOperation(method = "POST", responses = { + @APIResponse(responseCode = "2XX", description = "Ok"), + @APIResponse(responseCode = "5XX", description = "Failed"), + })))) + class App extends Application { + } + + @Path("/") + class Resource { + @GET + @Produces(MediaType.TEXT_PLAIN) + @Callback(name = "cb1", callbackUrlExpression = "/test1", pathItemRef = "myPathItem") + @Callback(name = "cb2", callbackUrlExpression = "/test2", pathItemRef = "#/components/pathItems/myPathItem") + public String hello() { + return "goodbye"; + } + } + + Index index = indexOf(App.class, Resource.class); + OpenAPI result = OpenApiProcessor.bootstrap(dynamicConfig(OASConfig.FILTER, UnusedSchemaFilter.class.getName()), index); + printToConsole(result); + assertJsonEquals("callback.reference-component-pathitem.json", result); + } } diff --git a/extension-jaxrs/src/test/resources/io/smallrye/openapi/runtime/scanner/callback.reference-component-pathitem.json b/extension-jaxrs/src/test/resources/io/smallrye/openapi/runtime/scanner/callback.reference-component-pathitem.json new file mode 100644 index 000000000..96660e7e0 --- /dev/null +++ b/extension-jaxrs/src/test/resources/io/smallrye/openapi/runtime/scanner/callback.reference-component-pathitem.json @@ -0,0 +1,53 @@ +{ + "openapi" : "3.1.0", + "info" : { + "title" : "Greetings", + "version" : "0.0.1" + }, + "components" : { + "pathItems" : { + "myPathItem" : { + "post" : { + "responses" : { + "2XX" : { + "description" : "Ok" + }, + "5XX" : { + "description" : "Failed" + } + } + } + } + } + }, + "paths" : { + "/" : { + "get" : { + "responses" : { + "200" : { + "description" : "OK", + "content" : { + "text/plain" : { + "schema" : { + "type" : "string" + } + } + } + } + }, + "callbacks" : { + "cb1" : { + "/test1" : { + "$ref" : "#/components/pathItems/myPathItem" + } + }, + "cb2" : { + "/test2" : { + "$ref" : "#/components/pathItems/myPathItem" + } + } + } + } + } + } +} \ No newline at end of file diff --git a/model/src/main/java/io/smallrye/openapi/model/ReferenceType.java b/model/src/main/java/io/smallrye/openapi/model/ReferenceType.java index 3c47c719a..7e9ff67b5 100644 --- a/model/src/main/java/io/smallrye/openapi/model/ReferenceType.java +++ b/model/src/main/java/io/smallrye/openapi/model/ReferenceType.java @@ -83,15 +83,13 @@ public String referenceOf(String ref) { } /** - * Reads a string property named "ref" value from the given annotation and converts it - * to a value appropriate for setting on a model's "$ref" property. + * Takes the value from a ref property from an annotation, and converts it to a JSON Pointer, suitable for use as a + * reference in an OpenAPI model. * - * @param annotation AnnotationInstance - * @return String value + * @param ref the ref value read from an annotation + * @return a value suitable for use in an OpenAPI model. */ - public String refValue(AnnotationInstance annotation) { - String ref = referenceValue(annotation); - + public String parseRefValue(String ref) { if (ref == null) { return null; } @@ -102,4 +100,16 @@ public String refValue(AnnotationInstance annotation) { return referenceOf(ref); } + + /** + * Reads a string property named "ref" value from the given annotation and converts it + * to a value appropriate for setting on a model's "$ref" property. + * + * @param annotation AnnotationInstance + * @return String value + */ + public String refValue(AnnotationInstance annotation) { + String ref = referenceValue(annotation); + return parseRefValue(ref); + } }