Skip to content

Commit

Permalink
Find @RequestBody annotations by argument index instead of type (#1994
Browse files Browse the repository at this point in the history
)

Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Sep 23, 2024
1 parent 9497960 commit 182cf8d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,9 +44,8 @@ public RequestBodyIO(IOContext<V, A, O, AB, OB> context) {

Stream<AnnotationInstance> getAnnotations(MethodInfo method, DotName annotation) {
Stream<AnnotationInstance> methodAnnos = Stream.of(scannerContext().annotations().getAnnotation(method, annotation));
Stream<AnnotationInstance> paramAnnos = method.parameterTypes()
.stream()
.map(p -> scannerContext().annotations().getMethodParameterAnnotation(method, p, annotation));
Stream<AnnotationInstance> paramAnnos = IntStream.range(0, method.parametersCount())
.mapToObj(p -> scannerContext().annotations().getMethodParameterAnnotation(method, p, annotation));

return Stream.concat(methodAnnos, paramAnnos)
.filter(Objects::nonNull);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}

0 comments on commit 182cf8d

Please sign in to comment.