From 6586e2c5cd3ebb3c948d77678c019c86a61cd82e Mon Sep 17 00:00:00 2001 From: Leonardo Moretti Date: Tue, 7 May 2024 15:54:19 +0200 Subject: [PATCH 1/4] Implemented isFrameworkContextType() in SpringAnnotationScanner --- extension-spring/pom.xml | 6 +++ .../spring/SpringAnnotationScanner.java | 5 ++ .../openapi/spring/SpringConstants.java | 11 ++++- .../scanner/SpringAnnotationScannerTest.java | 36 ++++++++++++++ ...etingPostControllerWithServletContext.java | 47 ++++++++++++++++++ ...eetingPutControllerWithServletContext.java | 49 +++++++++++++++++++ 6 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPostControllerWithServletContext.java create mode 100644 extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPutControllerWithServletContext.java diff --git a/extension-spring/pom.xml b/extension-spring/pom.xml index 13f40bb8d..387cdfde3 100644 --- a/extension-spring/pom.xml +++ b/extension-spring/pom.xml @@ -63,6 +63,12 @@ spring-webmvc test + + javax.servlet + javax.servlet-api + 3.1.0 + test + 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 a83cf8e57..e08318d21 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 @@ -92,6 +92,11 @@ public boolean isMultipartInput(Type inputType) { // TODO: Check this return SpringConstants.MULTIPART_INPUTS.contains(inputType.name()); } + + @Override + public boolean isFrameworkContextType(Type type) { + return SpringConstants.CONTEXTS.contains(type.name()); + } @Override public boolean containsScannerAnnotations(List instances, diff --git a/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringConstants.java b/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringConstants.java index df47e387a..3ff0fd4eb 100644 --- a/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringConstants.java +++ b/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringConstants.java @@ -4,7 +4,8 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; - +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.jboss.jandex.DotName; /** @@ -33,6 +34,14 @@ public class SpringConstants { static final DotName PATH_PARAM = DotName.createSimple("org.springframework.web.bind.annotation.PathVariable"); static final DotName HEADER_PARAM = DotName.createSimple("org.springframework.web.bind.annotation.RequestHeader"); static final DotName MATRIX_PARAM = DotName.createSimple("org.springframework.web.bind.annotation.MatrixVariable"); + + static final Set CONTEXTS = Stream.of("javax") + .map(prefix -> DotName.createComponentized(null, prefix)) + .map(prefix -> DotName.createComponentized(prefix, "servlet")) + .map(prefix -> DotName.createComponentized(prefix, "http")) + .flatMap(prefix -> Stream.of("HttpServletRequest", "HttpServletResponse", "HttpSession") + .map(simpleName -> DotName.createComponentized(prefix, simpleName))) + .collect(Collectors.toSet()); public static final Set MULTIPART_OUTPUTS = Collections .unmodifiableSet(new HashSet<>(Arrays.asList(MUTIPART_FILE))); 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 82a5dc89a..b0c0477d3 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,8 +15,10 @@ 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.GreetingPostControllerWithServletContext; 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.GreetingPutControllerWithServletContext; /** * Basic Spring annotation scanning @@ -113,6 +115,23 @@ void testBasicPostSpringDefinitionScanningAlt() throws IOException, JSONExceptio printToConsole(result); assertJsonEquals("resource.testBasicSpringPostDefinitionScanning.json", result); } + + /** + * This test a basic, no OpenApi annotations, hello world service + * + * @throws IOException + * @throws JSONException + */ + @Test + void testBasicPostSpringDefinitionScanningWithServletContext() throws IOException, JSONException { + Index i = indexOf(GreetingPostControllerWithServletContext.class, Greeting.class); + OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), i); + + OpenAPI result = scanner.scan(); + + printToConsole(result); + assertJsonEquals("resource.testBasicSpringPostDefinitionScanning.json", result); + } /** * This test a basic, no OpenApi annotations, hello world service @@ -148,6 +167,23 @@ void testBasicPutSpringDefinitionScanningAlt() throws IOException, JSONException assertJsonEquals("resource.testBasicSpringPutDefinitionScanning.json", result); } + /** + * This test a basic, no OpenApi annotations, hello world service + * + * @throws IOException + * @throws JSONException + */ + @Test + void testBasicPutSpringDefinitionScanningWithServletContext() throws IOException, JSONException { + Index i = indexOf(GreetingPutControllerWithServletContext.class, Greeting.class); + OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), i); + + OpenAPI result = scanner.scan(); + + printToConsole(result); + assertJsonEquals("resource.testBasicSpringPutDefinitionScanning.json", result); + } + /** * This test a basic, no OpenApi annotations, hello world service * diff --git a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPostControllerWithServletContext.java b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPostControllerWithServletContext.java new file mode 100644 index 000000000..c0cea16d5 --- /dev/null +++ b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPostControllerWithServletContext.java @@ -0,0 +1,47 @@ +package test.io.smallrye.openapi.runtime.scanner.resources; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import org.eclipse.microprofile.openapi.annotations.media.Content; +import org.eclipse.microprofile.openapi.annotations.media.Schema; +import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import test.io.smallrye.openapi.runtime.scanner.entities.Greeting; + +/** + * Spring. + * Some basic test, comparing with what we get in the JAX-RS version. + * See the GreetingPostResource in the JAX-RS test + * + * @author Phillip Kruger (phillip.kruger@redhat.com) + */ +@RestController +@RequestMapping(value = "/greeting", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) +public class GreetingPostControllerWithServletContext { + + // 1) Basic path var test + @PostMapping("/greet") + public Greeting greet(HttpServletRequest request, HttpServletResponse response, @RequestBody Greeting greeting) { + return 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, HttpServletRequest request, HttpServletResponse response) { + return ResponseEntity.ok(greeting); + } + + // 3) ResponseEntity with a type specified (No JaxRS comparison) + @PostMapping("/greetWithResponseTyped") + public ResponseEntity greetWithResponseTyped(HttpSession session, @RequestBody Greeting greeting) { + return ResponseEntity.ok(greeting); + } +} diff --git a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPutControllerWithServletContext.java b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPutControllerWithServletContext.java new file mode 100644 index 000000000..a4ada58b6 --- /dev/null +++ b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPutControllerWithServletContext.java @@ -0,0 +1,49 @@ +package test.io.smallrye.openapi.runtime.scanner.resources; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import org.eclipse.microprofile.openapi.annotations.media.Content; +import org.eclipse.microprofile.openapi.annotations.media.Schema; +import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import test.io.smallrye.openapi.runtime.scanner.entities.Greeting; + +/** + * Spring. + * Some basic test, comparing with what we get in the JAX-RS version. + * See the GreetingPutResource in the JAX-RS test + * + * @author Phillip Kruger (phillip.kruger@redhat.com) + */ +@RestController +@RequestMapping(value = "/greeting", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) +public class GreetingPutControllerWithServletContext { + + // 1) Basic path var test + @PutMapping("/greet/{id}") + public Greeting greet(HttpServletRequest request, HttpServletResponse response, @RequestBody Greeting greeting, @PathVariable(name = "id") String id) { + return greeting; + } + + // 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, HttpServletRequest request, HttpServletResponse response) { + return ResponseEntity.ok(greeting); + } + + // 3) ResponseEntity with a type specified (No JaxRS comparison) + @PutMapping("/greetWithResponseTyped/{id}") + public ResponseEntity greetWithResponseTyped(HttpSession session, @RequestBody Greeting greeting, + @PathVariable(name = "id") String id) { + return ResponseEntity.ok(greeting); + } +} From a93bda7b7462d1015defed5d2ab8cae12f97d95c Mon Sep 17 00:00:00 2001 From: morettileo <47557718+morettileo@users.noreply.github.com> Date: Fri, 10 May 2024 16:55:26 +0200 Subject: [PATCH 2/4] Apply suggestions from code review on Use the newer jakarta classes Co-authored-by: Michael Edgar --- extension-spring/pom.xml | 6 +++--- .../java/io/smallrye/openapi/spring/SpringConstants.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/extension-spring/pom.xml b/extension-spring/pom.xml index 387cdfde3..722ff2719 100644 --- a/extension-spring/pom.xml +++ b/extension-spring/pom.xml @@ -64,9 +64,9 @@ test - javax.servlet - javax.servlet-api - 3.1.0 + jakarta.servlet + jakarta.servlet-api + 6.0.0 test diff --git a/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringConstants.java b/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringConstants.java index 3ff0fd4eb..fc7cfd2c6 100644 --- a/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringConstants.java +++ b/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringConstants.java @@ -35,7 +35,7 @@ public class SpringConstants { static final DotName HEADER_PARAM = DotName.createSimple("org.springframework.web.bind.annotation.RequestHeader"); static final DotName MATRIX_PARAM = DotName.createSimple("org.springframework.web.bind.annotation.MatrixVariable"); - static final Set CONTEXTS = Stream.of("javax") + static final Set CONTEXTS = Stream.of("javax", "jakarta") .map(prefix -> DotName.createComponentized(null, prefix)) .map(prefix -> DotName.createComponentized(prefix, "servlet")) .map(prefix -> DotName.createComponentized(prefix, "http")) From d817438daeca4d61c30e60207b1fd46868d7db50 Mon Sep 17 00:00:00 2001 From: Leonardo Moretti Date: Fri, 10 May 2024 17:29:15 +0200 Subject: [PATCH 3/4] Now Jakarta classes are managed too --- extension-spring/pom.xml | 6 +- .../spring/SpringAnnotationScanner.java | 2 +- .../openapi/spring/SpringConstants.java | 15 ++--- .../scanner/SpringAnnotationScannerTest.java | 56 ++++++++++++++++--- ...etingPostControllerWithServletContext.java | 49 ++++++++++++++++ ...eetingPutControllerWithServletContext.java | 52 +++++++++++++++++ ...etingPostControllerWithServletContext.java | 6 +- ...eetingPutControllerWithServletContext.java | 9 ++- 8 files changed, 174 insertions(+), 21 deletions(-) create mode 100644 extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/jakarta/GreetingPostControllerWithServletContext.java create mode 100644 extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/jakarta/GreetingPutControllerWithServletContext.java rename extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/{ => javax}/GreetingPostControllerWithServletContext.java (92%) rename extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/{ => javax}/GreetingPutControllerWithServletContext.java (87%) diff --git a/extension-spring/pom.xml b/extension-spring/pom.xml index 722ff2719..abe71d654 100644 --- a/extension-spring/pom.xml +++ b/extension-spring/pom.xml @@ -63,10 +63,14 @@ spring-webmvc test + + javax.servlet + javax.servlet-api + 3.1.0 + jakarta.servlet jakarta.servlet-api - 6.0.0 test 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 e08318d21..1a46a864e 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 @@ -92,7 +92,7 @@ public boolean isMultipartInput(Type inputType) { // TODO: Check this return SpringConstants.MULTIPART_INPUTS.contains(inputType.name()); } - + @Override public boolean isFrameworkContextType(Type type) { return SpringConstants.CONTEXTS.contains(type.name()); diff --git a/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringConstants.java b/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringConstants.java index fc7cfd2c6..51d58d14f 100644 --- a/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringConstants.java +++ b/extension-spring/src/main/java/io/smallrye/openapi/spring/SpringConstants.java @@ -6,6 +6,7 @@ import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; + import org.jboss.jandex.DotName; /** @@ -34,14 +35,14 @@ public class SpringConstants { static final DotName PATH_PARAM = DotName.createSimple("org.springframework.web.bind.annotation.PathVariable"); static final DotName HEADER_PARAM = DotName.createSimple("org.springframework.web.bind.annotation.RequestHeader"); static final DotName MATRIX_PARAM = DotName.createSimple("org.springframework.web.bind.annotation.MatrixVariable"); - + static final Set CONTEXTS = Stream.of("javax", "jakarta") - .map(prefix -> DotName.createComponentized(null, prefix)) - .map(prefix -> DotName.createComponentized(prefix, "servlet")) - .map(prefix -> DotName.createComponentized(prefix, "http")) - .flatMap(prefix -> Stream.of("HttpServletRequest", "HttpServletResponse", "HttpSession") - .map(simpleName -> DotName.createComponentized(prefix, simpleName))) - .collect(Collectors.toSet()); + .map(prefix -> DotName.createComponentized(null, prefix)) + .map(prefix -> DotName.createComponentized(prefix, "servlet")) + .map(prefix -> DotName.createComponentized(prefix, "http")) + .flatMap(prefix -> Stream.of("HttpServletRequest", "HttpServletResponse", "HttpSession") + .map(simpleName -> DotName.createComponentized(prefix, simpleName))) + .collect(Collectors.toSet()); public static final Set MULTIPART_OUTPUTS = Collections .unmodifiableSet(new HashSet<>(Arrays.asList(MUTIPART_FILE))); 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 b0c0477d3..e65c24231 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,10 @@ 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.GreetingPostControllerWithServletContext; 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.GreetingPutControllerWithServletContext; +import test.io.smallrye.openapi.runtime.scanner.resources.javax.GreetingPostControllerWithServletContext; +import test.io.smallrye.openapi.runtime.scanner.resources.javax.GreetingPutControllerWithServletContext; /** * Basic Spring annotation scanning @@ -115,7 +115,26 @@ void testBasicPostSpringDefinitionScanningAlt() throws IOException, JSONExceptio printToConsole(result); assertJsonEquals("resource.testBasicSpringPostDefinitionScanning.json", result); } - + + /** + * This test a basic, no OpenApi annotations, hello world service + * + * @throws IOException + * @throws JSONException + */ + @Test + void testBasicPostSpringDefinitionScanningWithServletContextJakarta() throws IOException, JSONException { + Index i = indexOf( + test.io.smallrye.openapi.runtime.scanner.resources.jakarta.GreetingPostControllerWithServletContext.class, + Greeting.class); + OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), i); + + OpenAPI result = scanner.scan(); + + printToConsole(result); + assertJsonEquals("resource.testBasicSpringPostDefinitionScanning.json", result); + } + /** * This test a basic, no OpenApi annotations, hello world service * @@ -123,8 +142,10 @@ void testBasicPostSpringDefinitionScanningAlt() throws IOException, JSONExceptio * @throws JSONException */ @Test - void testBasicPostSpringDefinitionScanningWithServletContext() throws IOException, JSONException { - Index i = indexOf(GreetingPostControllerWithServletContext.class, Greeting.class); + void testBasicPostSpringDefinitionScanningWithServletContextJavax() throws IOException, JSONException { + Index i = indexOf( + test.io.smallrye.openapi.runtime.scanner.resources.javax.GreetingPostControllerWithServletContext.class, + Greeting.class); OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), i); OpenAPI result = scanner.scan(); @@ -174,8 +195,29 @@ void testBasicPutSpringDefinitionScanningAlt() throws IOException, JSONException * @throws JSONException */ @Test - void testBasicPutSpringDefinitionScanningWithServletContext() throws IOException, JSONException { - Index i = indexOf(GreetingPutControllerWithServletContext.class, Greeting.class); + void testBasicPutSpringDefinitionScanningWithServletContextJakarta() throws IOException, JSONException { + Index i = indexOf( + test.io.smallrye.openapi.runtime.scanner.resources.jakarta.GreetingPutControllerWithServletContext.class, + Greeting.class); + OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), i); + + OpenAPI result = scanner.scan(); + + printToConsole(result); + assertJsonEquals("resource.testBasicSpringPutDefinitionScanning.json", result); + } + + /** + * This test a basic, no OpenApi annotations, hello world service + * + * @throws IOException + * @throws JSONException + */ + @Test + void testBasicPutSpringDefinitionScanningWithServletContextJavax() throws IOException, JSONException { + Index i = indexOf( + test.io.smallrye.openapi.runtime.scanner.resources.javax.GreetingPutControllerWithServletContext.class, + Greeting.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/jakarta/GreetingPostControllerWithServletContext.java b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/jakarta/GreetingPostControllerWithServletContext.java new file mode 100644 index 000000000..93e619af9 --- /dev/null +++ b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/jakarta/GreetingPostControllerWithServletContext.java @@ -0,0 +1,49 @@ +package test.io.smallrye.openapi.runtime.scanner.resources.jakarta; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +import org.eclipse.microprofile.openapi.annotations.media.Content; +import org.eclipse.microprofile.openapi.annotations.media.Schema; +import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import test.io.smallrye.openapi.runtime.scanner.entities.Greeting; + +/** + * Spring. + * Some basic test, comparing with what we get in the JAX-RS version. + * See the GreetingPostResource in the JAX-RS test + * + * @author Phillip Kruger (phillip.kruger@redhat.com) + */ +@RestController +@RequestMapping(value = "/greeting", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) +public class GreetingPostControllerWithServletContext { + + // 1) Basic path var test + @PostMapping("/greet") + public Greeting greet(HttpServletRequest request, HttpServletResponse response, @RequestBody Greeting greeting) { + return 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, HttpServletRequest request, + HttpServletResponse response) { + return ResponseEntity.ok(greeting); + } + + // 3) ResponseEntity with a type specified (No JaxRS comparison) + @PostMapping("/greetWithResponseTyped") + public ResponseEntity greetWithResponseTyped(HttpSession session, @RequestBody Greeting greeting) { + return ResponseEntity.ok(greeting); + } +} diff --git a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/jakarta/GreetingPutControllerWithServletContext.java b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/jakarta/GreetingPutControllerWithServletContext.java new file mode 100644 index 000000000..203f3840f --- /dev/null +++ b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/jakarta/GreetingPutControllerWithServletContext.java @@ -0,0 +1,52 @@ +package test.io.smallrye.openapi.runtime.scanner.resources.jakarta; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +import org.eclipse.microprofile.openapi.annotations.media.Content; +import org.eclipse.microprofile.openapi.annotations.media.Schema; +import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import test.io.smallrye.openapi.runtime.scanner.entities.Greeting; + +/** + * Spring. + * Some basic test, comparing with what we get in the JAX-RS version. + * See the GreetingPutResource in the JAX-RS test + * + * @author Phillip Kruger (phillip.kruger@redhat.com) + */ +@RestController +@RequestMapping(value = "/greeting", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) +public class GreetingPutControllerWithServletContext { + + // 1) Basic path var test + @PutMapping("/greet/{id}") + public Greeting greet(HttpServletRequest request, HttpServletResponse response, @RequestBody Greeting greeting, + @PathVariable(name = "id") String id) { + return greeting; + } + + // 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, + HttpServletRequest request, HttpServletResponse response) { + return ResponseEntity.ok(greeting); + } + + // 3) ResponseEntity with a type specified (No JaxRS comparison) + @PutMapping("/greetWithResponseTyped/{id}") + public ResponseEntity greetWithResponseTyped(HttpSession session, @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/GreetingPostControllerWithServletContext.java b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/javax/GreetingPostControllerWithServletContext.java similarity index 92% rename from extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPostControllerWithServletContext.java rename to extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/javax/GreetingPostControllerWithServletContext.java index c0cea16d5..75e245631 100644 --- a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPostControllerWithServletContext.java +++ b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/javax/GreetingPostControllerWithServletContext.java @@ -1,8 +1,9 @@ -package test.io.smallrye.openapi.runtime.scanner.resources; +package test.io.smallrye.openapi.runtime.scanner.resources.javax; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; + import org.eclipse.microprofile.openapi.annotations.media.Content; import org.eclipse.microprofile.openapi.annotations.media.Schema; import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; @@ -35,7 +36,8 @@ public Greeting greet(HttpServletRequest request, HttpServletResponse response, // 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, HttpServletRequest request, HttpServletResponse response) { + public ResponseEntity greetWithResponse(@RequestBody Greeting greeting, HttpServletRequest request, + HttpServletResponse response) { return ResponseEntity.ok(greeting); } diff --git a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPutControllerWithServletContext.java b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/javax/GreetingPutControllerWithServletContext.java similarity index 87% rename from extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPutControllerWithServletContext.java rename to extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/javax/GreetingPutControllerWithServletContext.java index a4ada58b6..3a1af52ae 100644 --- a/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/GreetingPutControllerWithServletContext.java +++ b/extension-spring/src/test/java/test/io/smallrye/openapi/runtime/scanner/resources/javax/GreetingPutControllerWithServletContext.java @@ -1,8 +1,9 @@ -package test.io.smallrye.openapi.runtime.scanner.resources; +package test.io.smallrye.openapi.runtime.scanner.resources.javax; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; + import org.eclipse.microprofile.openapi.annotations.media.Content; import org.eclipse.microprofile.openapi.annotations.media.Schema; import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; @@ -29,14 +30,16 @@ public class GreetingPutControllerWithServletContext { // 1) Basic path var test @PutMapping("/greet/{id}") - public Greeting greet(HttpServletRequest request, HttpServletResponse response, @RequestBody Greeting greeting, @PathVariable(name = "id") String id) { + public Greeting greet(HttpServletRequest request, HttpServletResponse response, @RequestBody Greeting greeting, + @PathVariable(name = "id") String id) { return greeting; } // 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, HttpServletRequest request, HttpServletResponse response) { + public ResponseEntity greetWithResponse(@RequestBody Greeting greeting, @PathVariable(name = "id") String id, + HttpServletRequest request, HttpServletResponse response) { return ResponseEntity.ok(greeting); } From f3c115a63a590d77ecfcbd3e7381431f0460ea0a Mon Sep 17 00:00:00 2001 From: Leonardo Moretti Date: Sat, 11 May 2024 11:41:00 +0200 Subject: [PATCH 4/4] Readded test scope to javax.servlet-api dep --- extension-spring/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/extension-spring/pom.xml b/extension-spring/pom.xml index abe71d654..ce959a27f 100644 --- a/extension-spring/pom.xml +++ b/extension-spring/pom.xml @@ -67,6 +67,7 @@ javax.servlet javax.servlet-api 3.1.0 + test jakarta.servlet