From 20c5d8efef409e2d53173e364725336fbc84628b Mon Sep 17 00:00:00 2001 From: Michael Edgar Date: Fri, 20 Sep 2024 09:39:04 -0400 Subject: [PATCH] Find `@RequestBody` annotations by argument index instead of type Signed-off-by: Michael Edgar --- .../runtime/io/parameters/RequestBodyIO.java | 6 ++-- .../runtime/scanner/RequestBodyScanTests.java | 19 +++++++++++ ...params.request-body-annotation-on-arg.json | 34 +++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 extension-jaxrs/src/test/resources/io/smallrye/openapi/runtime/scanner/params.request-body-annotation-on-arg.json diff --git a/core/src/main/java/io/smallrye/openapi/runtime/io/parameters/RequestBodyIO.java b/core/src/main/java/io/smallrye/openapi/runtime/io/parameters/RequestBodyIO.java index 594c7a836..15405651b 100644 --- a/core/src/main/java/io/smallrye/openapi/runtime/io/parameters/RequestBodyIO.java +++ b/core/src/main/java/io/smallrye/openapi/runtime/io/parameters/RequestBodyIO.java @@ -5,6 +5,7 @@ import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; +import java.util.stream.IntStream; import java.util.stream.Stream; import org.eclipse.microprofile.openapi.models.media.Content; @@ -43,9 +44,8 @@ public RequestBodyIO(IOContext context) { Stream getAnnotations(MethodInfo method, DotName annotation) { Stream methodAnnos = Stream.of(scannerContext().annotations().getAnnotation(method, annotation)); - Stream paramAnnos = method.parameterTypes() - .stream() - .map(p -> scannerContext().annotations().getMethodParameterAnnotation(method, p, annotation)); + Stream paramAnnos = IntStream.range(0, method.parametersCount()) + .mapToObj(p -> scannerContext().annotations().getMethodParameterAnnotation(method, p, annotation)); return Stream.concat(methodAnnos, paramAnnos) .filter(Objects::nonNull); diff --git a/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/RequestBodyScanTests.java b/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/RequestBodyScanTests.java index 27ff7c6f3..3a96496e6 100644 --- a/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/RequestBodyScanTests.java +++ b/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/RequestBodyScanTests.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.util.HashMap; +import org.eclipse.microprofile.openapi.annotations.parameters.Parameter; import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody; import org.eclipse.microprofile.openapi.models.OpenAPI; import org.jboss.jandex.Index; @@ -104,4 +105,22 @@ public void addBar(@jakarta.validation.constraints.NotEmpty String foo) { } test("params.request-body-constraints.json", Resource.class); } + + @Test + void testResponseAnnotationUsed() throws IOException, JSONException { + @jakarta.ws.rs.Path("foo") + class FooApi { + @jakarta.ws.rs.PUT + @jakarta.ws.rs.Path("{name}/bar") + @jakarta.ws.rs.Consumes("text/plain") + @jakarta.ws.rs.Produces("application/json") + public jakarta.ws.rs.core.Response putDescription( + @Parameter(description = "The name", required = true) @jakarta.ws.rs.PathParam("name") String name, + @RequestBody(description = "The description", required = true) String description) { + return null; + } + } + + assertJsonEquals("params.request-body-annotation-on-arg.json", FooApi.class); + } } diff --git a/extension-jaxrs/src/test/resources/io/smallrye/openapi/runtime/scanner/params.request-body-annotation-on-arg.json b/extension-jaxrs/src/test/resources/io/smallrye/openapi/runtime/scanner/params.request-body-annotation-on-arg.json new file mode 100644 index 000000000..34492e9fe --- /dev/null +++ b/extension-jaxrs/src/test/resources/io/smallrye/openapi/runtime/scanner/params.request-body-annotation-on-arg.json @@ -0,0 +1,34 @@ +{ + "openapi" : "3.1.0", + "paths" : { + "/foo/{name}/bar" : { + "put" : { + "parameters" : [ { + "name" : "name", + "in" : "path", + "description" : "The name", + "required" : true, + "schema" : { + "type" : "string" + } + } ], + "requestBody" : { + "description" : "The description", + "content" : { + "text/plain" : { + "schema" : { + "type" : "string" + } + } + }, + "required" : true + }, + "responses" : { + "200" : { + "description" : "OK" + } + } + } + } + } +}