diff --git a/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringAnnotationScanner.java b/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringAnnotationScanner.java index 09ab9812d..f3864ecf0 100644 --- a/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringAnnotationScanner.java +++ b/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringAnnotationScanner.java @@ -4,7 +4,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.HashSet; -import java.util.LinkedHashSet; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -243,7 +242,7 @@ private void processControllerMethods(final ClassInfo resourceClass, for (MethodInfo methodInfo : getResourceMethods(context, resourceClass)) { if (!methodInfo.annotations().isEmpty()) { - for (PathItem.HttpMethod httpMethod : getHttpMethods(methodInfo)) { + for (PathItem.HttpMethod httpMethod : SpringSupport.getHttpMethods(methodInfo)) { processControllerMethod(resourceClass, methodInfo, httpMethod, openApi, tagRefs, locatorPathParameters); } @@ -251,43 +250,6 @@ private void processControllerMethods(final ClassInfo resourceClass, } } - static Set getHttpMethods(MethodInfo methodInfo) { - Set methods = new LinkedHashSet<>(); - - // Try @XXXMapping annotations - for (DotName validMethodAnnotations : SpringConstants.HTTP_METHODS) { - if (methodInfo.hasAnnotation(validMethodAnnotations)) { - String toHttpMethod = toHttpMethod(validMethodAnnotations); - methods.add(PathItem.HttpMethod.valueOf(toHttpMethod)); - } - } - - // Try @RequestMapping - if (methodInfo.hasAnnotation(SpringConstants.REQUEST_MAPPING)) { - AnnotationInstance requestMappingAnnotation = methodInfo.annotation(SpringConstants.REQUEST_MAPPING); - AnnotationValue methodValue = requestMappingAnnotation.value("method"); - - if (methodValue != null) { - String[] enumArray = methodValue.asEnumArray(); - for (String enumValue : enumArray) { - if (enumValue != null) { - methods.add(PathItem.HttpMethod.valueOf(enumValue.toUpperCase())); - } - } - } else { - // Default ? - } - } - - return methods; - } - - private static String toHttpMethod(DotName dotname) { - String className = dotname.withoutPackagePrefix(); - className = className.replace("Mapping", ""); - return className.toUpperCase(); - } - /** * Process a single Spring method to produce an OpenAPI Operation. * diff --git a/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringParameterProcessor.java b/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringParameterProcessor.java index 5cd501af3..4af136cf9 100644 --- a/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringParameterProcessor.java +++ b/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringParameterProcessor.java @@ -151,7 +151,7 @@ static boolean annotatesHttpGET(AnnotationTarget target) { MethodInfo resourceMethod = targetMethod(target); if (resourceMethod != null) { - return SpringAnnotationScanner.getHttpMethods(resourceMethod).contains(PathItem.HttpMethod.GET); + return SpringSupport.getHttpMethods(resourceMethod).contains(PathItem.HttpMethod.GET); } return false; diff --git a/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringSupport.java b/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringSupport.java new file mode 100644 index 000000000..7ecdb95a1 --- /dev/null +++ b/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringSupport.java @@ -0,0 +1,53 @@ +package io.smallrye.openapi.spring; + +import java.util.LinkedHashSet; +import java.util.Set; + +import org.eclipse.microprofile.openapi.models.PathItem; +import org.jboss.jandex.AnnotationInstance; +import org.jboss.jandex.AnnotationValue; +import org.jboss.jandex.DotName; +import org.jboss.jandex.MethodInfo; + +class SpringSupport { + + private SpringSupport() { + } + + static Set getHttpMethods(MethodInfo methodInfo) { + Set methods = new LinkedHashSet<>(); + + // Try @XXXMapping annotations + for (DotName validMethodAnnotations : SpringConstants.HTTP_METHODS) { + if (methodInfo.hasAnnotation(validMethodAnnotations)) { + String toHttpMethod = toHttpMethod(validMethodAnnotations); + methods.add(PathItem.HttpMethod.valueOf(toHttpMethod)); + } + } + + // Try @RequestMapping + if (methodInfo.hasAnnotation(SpringConstants.REQUEST_MAPPING)) { + AnnotationInstance requestMappingAnnotation = methodInfo.annotation(SpringConstants.REQUEST_MAPPING); + AnnotationValue methodValue = requestMappingAnnotation.value("method"); + + if (methodValue != null) { + String[] enumArray = methodValue.asEnumArray(); + for (String enumValue : enumArray) { + if (enumValue != null) { + methods.add(PathItem.HttpMethod.valueOf(enumValue.toUpperCase())); + } + } + } else { + // Default ? + } + } + + return methods; + } + + private static String toHttpMethod(DotName dotname) { + String className = dotname.withoutPackagePrefix(); + className = className.replace("Mapping", ""); + return className.toUpperCase(); + } +} diff --git a/extension-spring/src/test/java/io/smallrye/openapi/runtime/scanner/SpringAnnotationScannerTest.java b/extension-spring/src/test/java/io/smallrye/openapi/runtime/scanner/SpringAnnotationScannerTest.java index e13396f98..bf9aa44c7 100644 --- a/extension-spring/src/test/java/io/smallrye/openapi/runtime/scanner/SpringAnnotationScannerTest.java +++ b/extension-spring/src/test/java/io/smallrye/openapi/runtime/scanner/SpringAnnotationScannerTest.java @@ -15,10 +15,9 @@ import test.io.smallrye.openapi.runtime.scanner.resources.GreetingGetControllerAlt; import test.io.smallrye.openapi.runtime.scanner.resources.GreetingGetControllerAlt2; import test.io.smallrye.openapi.runtime.scanner.resources.GreetingPostController; +import test.io.smallrye.openapi.runtime.scanner.resources.GreetingPostControllerAlt; import test.io.smallrye.openapi.runtime.scanner.resources.GreetingPutController; import test.io.smallrye.openapi.runtime.scanner.resources.GreetingPutControllerAlt; -import test.io.smallrye.openapi.runtime.scanner.resources.javax.GreetingPostControllerWithServletContext; -import test.io.smallrye.openapi.runtime.scanner.resources.javax.GreetingPutControllerWithServletContext; /** * Basic Spring annotation scanning @@ -107,7 +106,7 @@ void testBasicPostSpringDefinitionScanning() throws IOException, JSONException { */ @Test void testBasicPostSpringDefinitionScanningAlt() throws IOException, JSONException { - Index i = indexOf(GreetingPostController.class, Greeting.class, GreetingParam.class); + Index i = indexOf(GreetingPostControllerAlt.class, Greeting.class, GreetingParam.class); OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), i); OpenAPI result = scanner.scan(); diff --git a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingDeleteController.java b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingDeleteController.java index 01b644eae..78c000c72 100644 --- a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingDeleteController.java +++ b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingDeleteController.java @@ -27,7 +27,7 @@ public class GreetingDeleteController { // 1) Basic path var test @DeleteMapping("/greet/{id}") public void greet(@PathVariable(name = "id") String id) { - + // No op } // 2) ResponseEntity without a type specified diff --git a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingDeleteControllerAlt.java b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingDeleteControllerAlt.java index 6093653c8..a3f58495f 100644 --- a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingDeleteControllerAlt.java +++ b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingDeleteControllerAlt.java @@ -28,7 +28,7 @@ public class GreetingDeleteControllerAlt { // 1) Basic path var test @RequestMapping(value = "/greet/{id}", method = RequestMethod.DELETE) public void greet(@PathVariable(name = "id") String id) { - + // No op } // 2) ResponseEntity without a type specified diff --git a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingGetControllerAlt.java b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingGetControllerAlt.java index 10b1b3aee..6c02f9a5e 100644 --- a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingGetControllerAlt.java +++ b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingGetControllerAlt.java @@ -14,7 +14,6 @@ import org.springdoc.api.annotations.ParameterObject; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -73,7 +72,7 @@ public Greeting helloParameterObject(@ParameterObject() GreetingParam params) { @SuppressWarnings("rawtypes") @RequestMapping(value = "/helloPathVariableWithResponse/{name}", method = RequestMethod.GET) @APIResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(ref = "#/components/schemas/Greeting"))) - public ResponseEntity helloPathVariableWithResponse(@PathVariable(name = "name") String name) { + public ResponseEntity helloPathVariableWithResponse(@PathVariable(name = "name") String name) { return ResponseEntity.ok(new Greeting("Hello " + name)); } diff --git a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingGetControllerAlt2.java b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingGetControllerAlt2.java index 9300f30e1..40537ab3d 100644 --- a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingGetControllerAlt2.java +++ b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingGetControllerAlt2.java @@ -13,7 +13,6 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.security.access.annotation.Secured; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -72,7 +71,7 @@ public Greeting helloParameterObject(@ParameterObject() GreetingParam params) { @SuppressWarnings("rawtypes") @RequestMapping(path = "/helloPathVariableWithResponse/{name}", method = RequestMethod.GET) @APIResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(ref = "#/components/schemas/Greeting"))) - public ResponseEntity helloPathVariableWithResponse(@PathVariable(name = "name") String name) { + public ResponseEntity helloPathVariableWithResponse(@PathVariable(name = "name") String name) { return ResponseEntity.ok(new Greeting("Hello " + name)); } diff --git a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPostController.java b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPostController.java index dfe854989..17f68518e 100644 --- a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPostController.java +++ b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPostController.java @@ -32,7 +32,7 @@ public Greeting greet(@RequestBody Greeting greeting) { // 2) ResponseEntity without a type specified @PostMapping("/greetWithResponse") @APIResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(ref = "#/components/schemas/Greeting"))) - public ResponseEntity greetWithResponse(@RequestBody Greeting greeting) { + public ResponseEntity greetWithResponse(@RequestBody Greeting greeting) { return ResponseEntity.ok(greeting); } diff --git a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPostControllerAlt.java b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPostControllerAlt.java index fefef95f0..8b04d6019 100644 --- a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPostControllerAlt.java +++ b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPostControllerAlt.java @@ -34,7 +34,7 @@ public Greeting greet(@RequestBody Greeting greeting) { // 2) ResponseEntity without a type specified @RequestMapping(value = "/greetWithResponse", method = RequestMethod.POST) @APIResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(ref = "#/components/schemas/Greeting"))) - public ResponseEntity greetWithResponse(@RequestBody Greeting greeting) { + public ResponseEntity greetWithResponse(@RequestBody Greeting greeting) { return ResponseEntity.ok(greeting); } diff --git a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPutController.java b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPutController.java index 4924d38b2..f789ceada 100644 --- a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPutController.java +++ b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPutController.java @@ -33,7 +33,7 @@ public Greeting greet(@RequestBody Greeting greeting, @PathVariable(name = "id") // 2) ResponseEntity without a type specified @PutMapping("/greetWithResponse/{id}") @APIResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(ref = "#/components/schemas/Greeting"))) - public ResponseEntity greetWithResponse(@RequestBody Greeting greeting, @PathVariable(name = "id") String id) { + public ResponseEntity greetWithResponse(@RequestBody Greeting greeting, @PathVariable(name = "id") String id) { return ResponseEntity.ok(greeting); } diff --git a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPutControllerAlt.java b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPutControllerAlt.java index a5a5e2f85..85502804a 100644 --- a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPutControllerAlt.java +++ b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPutControllerAlt.java @@ -33,7 +33,7 @@ public Greeting greet(@RequestBody Greeting greeting, @PathVariable(name = "id") // 2) ResponseEntity without a type specified @RequestMapping(value = "/greetWithResponse/{id}", method = RequestMethod.PUT) @APIResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(ref = "#/components/schemas/Greeting"))) - public ResponseEntity greetWithResponse(@RequestBody Greeting greeting, @PathVariable(name = "id") String id) { + public ResponseEntity greetWithResponse(@RequestBody Greeting greeting, @PathVariable(name = "id") String id) { return ResponseEntity.ok(greeting); }