Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permit Callback.pathItemRef to be any JSON path #2156

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Callback read(AnnotationInstance annotation) {
callback.setRef(ReferenceType.CALLBACK.refValue(annotation));

Optional.ofNullable(this.<String> 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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}
}
24 changes: 17 additions & 7 deletions model/src/main/java/io/smallrye/openapi/model/ReferenceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
}
Loading