Skip to content

Commit 9d3de9c

Browse files
committed
[Java][Spring] remove 'size', 'page' and 'sort' query params if using 'x-spring-paginated' (#8315)
1 parent 1f7824c commit 9d3de9c

18 files changed

Lines changed: 212 additions & 7 deletions

File tree

docs/generators/java-camel.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
112112
|x-content-type|Specify custom value for 'Content-Type' header for operation|OPERATION|null
113113
|x-class-extra-annotation|List of custom annotations to be added to model|MODEL|null
114114
|x-field-extra-annotation|List of custom annotations to be added to property|FIELD|null
115-
|x-spring-paginated|Add org.springframework.data.domain.Pageable to controller method. Can be used to handle page & size query parameters|OPERATION|false
115+
|x-spring-paginated|Add `org.springframework.data.domain.Pageable` to controller method. Can be used to handle `page`, `size` and `sort` query parameters. If these query parameters are also specified in the operation spec, they will be removed from the controller method as their values can be obtained from the `Pageable` object.|OPERATION|false
116116

117117

118118
## IMPORT MAPPING

docs/generators/spring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
105105
|x-content-type|Specify custom value for 'Content-Type' header for operation|OPERATION|null
106106
|x-class-extra-annotation|List of custom annotations to be added to model|MODEL|null
107107
|x-field-extra-annotation|List of custom annotations to be added to property|FIELD|null
108-
|x-spring-paginated|Add org.springframework.data.domain.Pageable to controller method. Can be used to handle page & size query parameters|OPERATION|false
108+
|x-spring-paginated|Add `org.springframework.data.domain.Pageable` to controller method. Can be used to handle `page`, `size` and `sort` query parameters. If these query parameters are also specified in the operation spec, they will be removed from the controller method as their values can be obtained from the `Pageable` object.|OPERATION|false
109109

110110

111111
## IMPORT MAPPING

modules/openapi-generator/src/main/java/org/openapitools/codegen/VendorExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public enum VendorExtension {
77

88
X_IMPLEMENTS("x-implements", ExtensionLevel.MODEL, "Ability to specify interfaces that model must implements", "empty array"),
9-
X_SPRING_PAGINATED("x-spring-paginated", ExtensionLevel.OPERATION, "Add org.springframework.data.domain.Pageable to controller method. Can be used to handle page & size query parameters", "false"),
9+
X_SPRING_PAGINATED("x-spring-paginated", ExtensionLevel.OPERATION, "Add `org.springframework.data.domain.Pageable` to controller method. Can be used to handle `page`, `size` and `sort` query parameters. If these query parameters are also specified in the operation spec, they will be removed from the controller method as their values can be obtained from the `Pageable` object.", "false"),
1010
X_DISCRIMINATOR_VALUE("x-discriminator-value", ExtensionLevel.MODEL, "Used with model inheritance to specify value for discriminator that identifies current model", ""),
1111
X_SETTER_EXTRA_ANNOTATION("x-setter-extra-annotation", ExtensionLevel.FIELD, "Custom annotation that can be specified over java setter for specific field", "When field is array & uniqueItems, then this extension is used to add `@JsonDeserialize(as = LinkedHashSet.class)` over setter, otherwise no value"),
1212
X_WEBCLIENT_BLOCKING("x-webclient-blocking", ExtensionLevel.OPERATION, "Specifies if method for specific operation should be blocking or non-blocking(ex: return `Mono<T>/Flux<T>` or `return T/List<T>/Set<T>` & execute `.block()` inside generated method)", "false"),

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,8 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
10751075
* Add dynamic imports based on the parameters and vendor extensions of an operation.
10761076
* The imports are expanded by the mustache {{import}} tag available to model and api
10771077
* templates.
1078+
*
1079+
* #8315 Also handles removing 'size', 'page' and 'sort' query parameters if using 'x-spring-paginated'.
10781080
*/
10791081
@Override
10801082
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) {
@@ -1093,6 +1095,15 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
10931095
if (DocumentationProvider.SPRINGDOC.equals(getDocumentationProvider())) {
10941096
codegenOperation.imports.add("ParameterObject");
10951097
}
1098+
1099+
// #8315 Spring Data Web default query params recognized by Pageable
1100+
List<String> defaultPageableQueryParams = new ArrayList<>(
1101+
Arrays.asList("page", "size", "sort")
1102+
);
1103+
1104+
// #8315 Remove matching Spring Data Web default query params if 'x-spring-paginated' with Pageable is used
1105+
codegenOperation.queryParams.removeIf(param -> defaultPageableQueryParams.contains(param.baseName));
1106+
codegenOperation.allParams.removeIf(param -> param.isQueryParam && defaultPageableQueryParams.contains(param.baseName));
10961107
}
10971108

10981109
if (reactive) {

modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing-with-spring-pageable.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,31 @@ paths:
137137
items:
138138
type: string
139139
collectionFormat: csv
140+
- name: size
141+
in: header
142+
description: 'A test HeaderParam for issue #8315 - must NOT be removed when x-spring-paginated:true is used.'
143+
required: false
144+
type: string
145+
- name: size
146+
in: query
147+
description: 'The number of items to return per page. Test QueryParam for issue #8315 - must be removed when x-spring-paginated:true is used.'
148+
required: true
149+
type: integer
150+
minimum: 1
151+
default: 20
152+
- name: page
153+
in: query
154+
description: 'The page to return, starting with page 0. Test QueryParam for issue #8315 - must be removed when x-spring-paginated:true is used.'
155+
required: true
156+
type: integer
157+
minimum: 0
158+
default: 0
159+
- name: sort
160+
in: query
161+
description: 'The sorting to apply to the Pageable object. Test QueryParam for issue #8315 - must be removed when x-spring-paginated:true is used.'
162+
required: true
163+
type: string
164+
default: id,asc
140165
responses:
141166
'200':
142167
description: successful operation

modules/openapi-generator/src/test/resources/2_0/petstore-with-spring-pageable.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,31 @@ paths:
133133
items:
134134
type: string
135135
collectionFormat: csv
136+
- name: size
137+
in: header
138+
description: 'A test HeaderParam for issue #8315 - must NOT be removed when x-spring-paginated:true is used.'
139+
required: false
140+
type: string
141+
- name: size
142+
in: query
143+
description: 'The number of items to return per page. Test QueryParam for issue #8315 - must be removed when x-spring-paginated:true is used.'
144+
required: true
145+
type: integer
146+
minimum: 1
147+
default: 20
148+
- name: page
149+
in: query
150+
description: 'The page to return, starting with page 0. Test QueryParam for issue #8315 - must be removed when x-spring-paginated:true is used.'
151+
required: true
152+
type: integer
153+
minimum: 0
154+
default: 0
155+
- name: sort
156+
in: query
157+
description: 'The sorting to apply to the Pageable object. Test QueryParam for issue #8315 - must be removed when x-spring-paginated:true is used.'
158+
required: true
159+
type: string
160+
default: id,asc
136161
responses:
137162
'200':
138163
description: successful operation

samples/client/petstore/spring-cloud-spring-pageable/src/main/java/org/openapitools/api/PetApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ ResponseEntity<List<Pet>> findPetsByStatus(
135135
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
136136
*
137137
* @param tags Tags to filter by (required)
138+
* @param size A test HeaderParam for issue #8315 - must NOT be removed when x-spring-paginated:true is used. (optional)
138139
* @return successful operation (status code 200)
139140
* or Invalid tag value (status code 400)
140141
* @deprecated
@@ -165,6 +166,7 @@ ResponseEntity<List<Pet>> findPetsByStatus(
165166
)
166167
ResponseEntity<List<Pet>> findPetsByTags(
167168
@NotNull @ApiParam(value = "Tags to filter by", required = true) @Valid @RequestParam(value = "tags", required = true) List<String> tags,
169+
@ApiParam(value = "A test HeaderParam for issue #8315 - must NOT be removed when x-spring-paginated:true is used.") @RequestHeader(value = "size", required = false) String size,
168170
@ApiIgnore final Pageable pageable
169171
);
170172

samples/openapi3/client/petstore/spring-cloud-spring-pageable/src/main/java/org/openapitools/api/PetApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ ResponseEntity<List<Pet>> findPetsByStatus(
131131
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
132132
*
133133
* @param tags Tags to filter by (required)
134+
* @param size A test HeaderParam for issue #8315 - must NOT be removed when x-spring-paginated:true is used. (optional)
134135
* @return successful operation (status code 200)
135136
* or Invalid tag value (status code 400)
136137
* @deprecated
@@ -158,6 +159,7 @@ ResponseEntity<List<Pet>> findPetsByStatus(
158159
)
159160
ResponseEntity<List<Pet>> findPetsByTags(
160161
@NotNull @Parameter(name = "tags", description = "Tags to filter by", required = true) @Valid @RequestParam(value = "tags", required = true) List<String> tags,
162+
@Parameter(name = "size", description = "A test HeaderParam for issue #8315 - must NOT be removed when x-spring-paginated:true is used.") @RequestHeader(value = "size", required = false) String size,
161163
@ParameterObject final Pageable pageable
162164
);
163165

samples/server/petstore/springboot-spring-pageable-delegatePattern-without-j8/src/main/java/org/openapitools/api/PetApi.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ default ResponseEntity<List<Pet>> findPetsByStatus(
145145
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
146146
*
147147
* @param tags Tags to filter by (required)
148+
* @param size A test HeaderParam for issue #8315 - must NOT be removed when x-spring-paginated:true is used. (optional)
148149
* @return successful operation (status code 200)
149150
* or Invalid tag value (status code 400)
150151
* @deprecated
@@ -175,9 +176,10 @@ default ResponseEntity<List<Pet>> findPetsByStatus(
175176
)
176177
default ResponseEntity<List<Pet>> findPetsByTags(
177178
@NotNull @ApiParam(value = "Tags to filter by", required = true) @Valid @RequestParam(value = "tags", required = true) List<String> tags,
179+
@ApiParam(value = "A test HeaderParam for issue #8315 - must NOT be removed when x-spring-paginated:true is used.") @RequestHeader(value = "size", required = false) String size,
178180
@ApiIgnore final Pageable pageable
179181
) {
180-
return getDelegate().findPetsByTags(tags, pageable);
182+
return getDelegate().findPetsByTags(tags, size, pageable);
181183
}
182184

183185

samples/server/petstore/springboot-spring-pageable-delegatePattern-without-j8/src/main/java/org/openapitools/api/PetApiDelegate.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@ default ResponseEntity<List<Pet>> findPetsByStatus(List<String> status, final Pa
8787
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
8888
*
8989
* @param tags Tags to filter by (required)
90+
* @param size A test HeaderParam for issue #8315 - must NOT be removed when x-spring-paginated:true is used. (optional)
9091
* @return successful operation (status code 200)
9192
* or Invalid tag value (status code 400)
9293
* @deprecated
9394
* @see PetApi#findPetsByTags
9495
*/
9596
@Deprecated
96-
default ResponseEntity<List<Pet>> findPetsByTags(List<String> tags, final Pageable pageable) {
97+
default ResponseEntity<List<Pet>> findPetsByTags(List<String> tags,
98+
String size, final Pageable pageable) {
9799
getRequest().ifPresent(request -> {
98100
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
99101
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {

0 commit comments

Comments
 (0)