Skip to content

Commit

Permalink
feat(springdoc-parameter-object): support with jakarta-query-param
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Ramp (u125015) committed May 6, 2024
1 parent e47033d commit 97d2460
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 7 deletions.
12 changes: 12 additions & 0 deletions extension-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<properties>
<version.spring>5.3.34</version.spring>
<version.springdoc>1.8.0</version.springdoc>
</properties>

<dependencyManagement>
Expand All @@ -24,6 +25,12 @@
<version>${version.spring}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-common</artifactId>
<version>${version.springdoc}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -63,6 +70,11 @@
<artifactId>spring-webmvc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-common</artifactId>
<scope>test</scope>
</dependency>

<!-- Depend on core tests -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ public class SpringConstants {
static final DotName MUTIPART_FILE = DotName.createSimple("org.springframework.web.multipart.MultipartFile");

static final DotName QUERY_PARAM = DotName.createSimple("org.springframework.web.bind.annotation.RequestParam");
static final DotName JAKARTA_QUERY_PARAM = DotName.createSimple("jakarta.ws.rs.QueryParam");
static final DotName COOKIE_PARAM = DotName.createSimple("org.springframework.web.bind.annotation.CookieValue");
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 DotName PARAMETER_OBJECT = DotName.createSimple("org.springdoc.api.annotations.ParameterObject");

public static final Set<DotName> MULTIPART_OUTPUTS = Collections
.unmodifiableSet(new HashSet<>(Arrays.asList(MUTIPART_FILE)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public enum SpringParameter {
MATRIX_PARAM(SpringConstants.MATRIX_PARAM, Parameter.In.PATH, Parameter.Style.MATRIX, Parameter.Style.MATRIX),
QUERY_PARAM(SpringConstants.QUERY_PARAM, Parameter.In.QUERY, null, Parameter.Style.FORM),
HEADER_PARAM(SpringConstants.HEADER_PARAM, Parameter.In.HEADER, null, Parameter.Style.SIMPLE),
COOKIE_PARAM(SpringConstants.COOKIE_PARAM, Parameter.In.COOKIE, null, Parameter.Style.FORM);
COOKIE_PARAM(SpringConstants.COOKIE_PARAM, Parameter.In.COOKIE, null, Parameter.Style.FORM),
PARAMETER_OBJECT(SpringConstants.PARAMETER_OBJECT, null, null, null),
JAKARTA_QUERY_PARAM(SpringConstants.JAKARTA_QUERY_PARAM, Parameter.In.QUERY, null, Parameter.Style.FORM);

//BEAN_PARAM(SpringConstants.BEAN_PARAM, null, null, null),
//FORM_PARAM(SpringConstants.FORM_PARAM, null, Parameter.Style.FORM, Parameter.Style.FORM),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import org.junit.jupiter.api.Test;

import test.io.smallrye.openapi.runtime.scanner.entities.Greeting;
import test.io.smallrye.openapi.runtime.scanner.entities.GreetingParam;
import test.io.smallrye.openapi.runtime.scanner.resources.GreetingDeleteController;
import test.io.smallrye.openapi.runtime.scanner.resources.GreetingDeleteControllerAlt;
import test.io.smallrye.openapi.runtime.scanner.resources.GreetingGetController;
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;

Expand All @@ -33,7 +33,7 @@ class SpringAnnotationScannerTest extends SpringDataObjectScannerTestBase {
*/
@Test
void testBasicGetSpringDefinitionScanning() throws IOException, JSONException {
Index i = indexOf(GreetingGetController.class, Greeting.class);
Index i = indexOf(GreetingGetController.class, Greeting.class, GreetingParam.class);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), i);

OpenAPI result = scanner.scan();
Expand All @@ -52,7 +52,7 @@ void testBasicGetSpringDefinitionScanning() throws IOException, JSONException {
*/
@Test
void testBasicSpringDefinitionScanningAlt() throws IOException, JSONException {
Index i = indexOf(GreetingGetControllerAlt.class, Greeting.class);
Index i = indexOf(GreetingGetControllerAlt.class, Greeting.class, GreetingParam.class);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), i);

OpenAPI result = scanner.scan();
Expand All @@ -71,7 +71,7 @@ void testBasicSpringDefinitionScanningAlt() throws IOException, JSONException {
*/
@Test
void testBasicSpringDefinitionScanningAlt2() throws IOException, JSONException {
Index i = indexOf(GreetingGetControllerAlt2.class, Greeting.class);
Index i = indexOf(GreetingGetControllerAlt2.class, Greeting.class, GreetingParam.class);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), i);

OpenAPI result = scanner.scan();
Expand All @@ -88,7 +88,7 @@ void testBasicSpringDefinitionScanningAlt2() throws IOException, JSONException {
*/
@Test
void testBasicPostSpringDefinitionScanning() throws IOException, JSONException {
Index i = indexOf(GreetingPostController.class, Greeting.class);
Index i = indexOf(GreetingPostController.class, Greeting.class, GreetingParam.class);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), i);

OpenAPI result = scanner.scan();
Expand All @@ -105,7 +105,7 @@ void testBasicPostSpringDefinitionScanning() throws IOException, JSONException {
*/
@Test
void testBasicPostSpringDefinitionScanningAlt() throws IOException, JSONException {
Index i = indexOf(GreetingPostControllerAlt.class, Greeting.class);
Index i = indexOf(GreetingPostController.class, Greeting.class, GreetingParam.class);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), i);

OpenAPI result = scanner.scan();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test.io.smallrye.openapi.runtime.scanner.entities;

import jakarta.ws.rs.QueryParam;

public class GreetingParam {
@QueryParam("nameQuery") // "real" spring does not require this, but quarkus-spring-web does
private final String name;

public GreetingParam(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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.springdoc.api.annotations.ParameterObject;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -16,6 +17,7 @@
import org.springframework.web.bind.annotation.RestController;

import test.io.smallrye.openapi.runtime.scanner.entities.Greeting;
import test.io.smallrye.openapi.runtime.scanner.entities.GreetingParam;

/**
* Spring.
Expand Down Expand Up @@ -52,6 +54,12 @@ public Greeting helloRequestParam(@RequestParam(value = "name", required = false
return new Greeting("Hello " + name);
}

// 4a) Basic request with parameter-object test
@GetMapping("/helloParameterObject")
public Greeting helloParameterObject(@ParameterObject() GreetingParam params) {
return new Greeting("Hello " + params.getName());
}

// 5) ResponseEntity without a type specified
@GetMapping("/helloPathVariableWithResponse/{name}")
@APIResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(ref = "#/components/schemas/Greeting")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
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.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;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import test.io.smallrye.openapi.runtime.scanner.entities.Greeting;
import test.io.smallrye.openapi.runtime.scanner.entities.GreetingParam;

/**
* Spring.
Expand Down Expand Up @@ -54,6 +57,12 @@ public Greeting helloRequestParam(@RequestParam(value = "name", required = false
return new Greeting("Hello " + name);
}

// 4a) Basic request with parameter-object test
@RequestMapping(value = "/helloParameterObject", method = RequestMethod.GET)
public Greeting helloParameterObject(@ParameterObject() GreetingParam params) {
return new Greeting("Hello " + params.getName());
}

// 5) ResponseEntity without a type specified
@RequestMapping(value = "/helloPathVariableWithResponse/{name}", method = RequestMethod.GET)
@APIResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(ref = "#/components/schemas/Greeting")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
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.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;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import test.io.smallrye.openapi.runtime.scanner.entities.Greeting;
import test.io.smallrye.openapi.runtime.scanner.entities.GreetingParam;

/**
* Spring.
Expand Down Expand Up @@ -54,6 +57,12 @@ public Greeting helloRequestParam(@RequestParam(value = "name", required = false
return new Greeting("Hello " + name);
}

// 4a) Basic request with parameter-object test
@RequestMapping(path = "/helloParameterObject", method = RequestMethod.GET)
public Greeting helloParameterObject(@ParameterObject() GreetingParam params) {
return new Greeting("Hello " + params.getName());
}

// 5) ResponseEntity without a type specified
@RequestMapping(path = "/helloPathVariableWithResponse/{name}", method = RequestMethod.GET)
@APIResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(ref = "#/components/schemas/Greeting")))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package test.io.smallrye.openapi.runtime.scanner.resources;

import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import test.io.smallrye.openapi.runtime.scanner.entities.Greeting;
import test.io.smallrye.openapi.runtime.scanner.entities.GreetingParam;

@RestController
@RequestMapping(value = "/greeting-with-params", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public class GreetingParameterobjectController {

@GetMapping("/hello")
public Greeting hello(@ParameterObject() GreetingParam params) {
return new Greeting("Hello " + params.getName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@
}
}
},
"/greeting/helloParameterObject": {
"get": {
"parameters" : [ {
"name" : "nameQuery",
"in" : "query",
"schema" : {
"type" : "string"
}
} ],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Greeting"
}
}
}
}
}
}
},
"/greeting/hellosPathVariable/{name}" : {
"get" : {
"parameters" : [ {
Expand Down

0 comments on commit 97d2460

Please sign in to comment.