Skip to content

Commit ee7927a

Browse files
30p87tofi86
andauthored
[Java][Spring] remove 'size', 'page' and 'sort' query params if using 'x-spring-paginated' (#8315) (#21016)
* [Java][Spring] remove 'size', 'page' and 'sort' query params if using 'x-spring-paginated' (#8315) * Properly implement samples, fixing build issues --------- Co-authored-by: Tobias Fischer <t.fischer@goldflam.de>
1 parent 72de5bc commit ee7927a

File tree

20 files changed

+312
-7
lines changed

20 files changed

+312
-7
lines changed

docs/generators/java-camel.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
127127
|x-class-extra-annotation|List of custom annotations to be added to model|MODEL|null
128128
|x-field-extra-annotation|List of custom annotations to be added to property|FIELD, OPERATION_PARAMETER|null
129129
|x-operation-extra-annotation|List of custom annotations to be added to operation|OPERATION|null
130-
|x-spring-paginated|Add org.springframework.data.domain.Pageable to controller method. Can be used to handle page & size query parameters|OPERATION|false
130+
|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
131131
|x-version-param|Marker property that tells that this parameter would be used for endpoint versioning. Applicable for headers & query params. true/false|OPERATION_PARAMETER|null
132132
|x-pattern-message|Add this property whenever you need to customize the invalidation error message for the regex pattern of a variable|FIELD, OPERATION_PARAMETER|null
133133

docs/generators/spring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
120120
|x-class-extra-annotation|List of custom annotations to be added to model|MODEL|null
121121
|x-field-extra-annotation|List of custom annotations to be added to property|FIELD, OPERATION_PARAMETER|null
122122
|x-operation-extra-annotation|List of custom annotations to be added to operation|OPERATION|null
123-
|x-spring-paginated|Add org.springframework.data.domain.Pageable to controller method. Can be used to handle page & size query parameters|OPERATION|false
123+
|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
124124
|x-version-param|Marker property that tells that this parameter would be used for endpoint versioning. Applicable for headers & query params. true/false|OPERATION_PARAMETER|null
125125
|x-pattern-message|Add this property whenever you need to customize the invalidation error message for the regex pattern of a variable|FIELD, OPERATION_PARAMETER|null
126126

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
@@ -10,7 +10,7 @@
1010
public enum VendorExtension {
1111

1212
X_IMPLEMENTS("x-implements", ExtensionLevel.MODEL, "Ability to specify interfaces that model must implements", "empty array"),
13-
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"),
13+
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"),
1414
X_SPRING_PROVIDE_ARGS("x-spring-provide-args", ExtensionLevel.OPERATION, "Allows adding additional hidden parameters in the API specification to allow access to content such as header values or properties", "empty array"),
1515
X_DISCRIMINATOR_VALUE("x-discriminator-value", ExtensionLevel.MODEL, "Used with model inheritance to specify value for discriminator that identifies current model", ""),
1616
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"),

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
@@ -996,6 +996,8 @@ protected boolean isConstructorWithAllArgsAllowed(CodegenModel codegenModel) {
996996
* Add dynamic imports based on the parameters and vendor extensions of an operation.
997997
* The imports are expanded by the mustache {{import}} tag available to model and api
998998
* templates.
999+
*
1000+
* #8315 Also handles removing 'size', 'page' and 'sort' query parameters if using 'x-spring-paginated'.
9991001
*/
10001002
@Override
10011003
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) {
@@ -1023,6 +1025,15 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
10231025
if (DocumentationProvider.SPRINGDOC.equals(getDocumentationProvider())) {
10241026
codegenOperation.imports.add("ParameterObject");
10251027
}
1028+
1029+
// #8315 Spring Data Web default query params recognized by Pageable
1030+
List<String> defaultPageableQueryParams = new ArrayList<>(
1031+
Arrays.asList("page", "size", "sort")
1032+
);
1033+
1034+
// #8315 Remove matching Spring Data Web default query params if 'x-spring-paginated' with Pageable is used
1035+
codegenOperation.queryParams.removeIf(param -> defaultPageableQueryParams.contains(param.baseName));
1036+
codegenOperation.allParams.removeIf(param -> param.isQueryParam && defaultPageableQueryParams.contains(param.baseName));
10261037
}
10271038
if (codegenOperation.vendorExtensions.containsKey("x-spring-provide-args") && !provideArgsClassSet.isEmpty()) {
10281039
codegenOperation.imports.addAll(provideArgsClassSet);

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

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,38 @@ paths:
171171
type: array
172172
items:
173173
type: string
174+
- description: "A test HeaderParam for issue #8315 - must NOT be removed when\
175+
\ x-spring-paginated:true is used."
176+
in: header
177+
name: size
178+
schema:
179+
type: string
180+
- description: "The number of items to return per page. Test QueryParam for\
181+
\ issue #8315 - must be removed when x-spring-paginated:true is used."
182+
in: query
183+
name: size
184+
required: true
185+
schema:
186+
default: 20
187+
minimum: 1
188+
type: integer
189+
- description: "The page to return, starting with page 0. Test QueryParam for\
190+
\ issue #8315 - must be removed when x-spring-paginated:true is used."
191+
in: query
192+
name: page
193+
required: true
194+
schema:
195+
default: 0
196+
minimum: 0
197+
type: integer
198+
- description: "The sorting to apply to the Pageable object. Test QueryParam\
199+
\ for issue #8315 - must be removed when x-spring-paginated:true is used."
200+
in: query
201+
name: sort
202+
required: true
203+
schema:
204+
default: "id,asc"
205+
type: string
174206
responses:
175207
200:
176208
description: successful operation

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,38 @@ paths:
136136
type: array
137137
items:
138138
type: string
139+
- description: "A test HeaderParam for issue #8315 - must NOT be removed when\
140+
\ x-spring-paginated:true is used."
141+
in: header
142+
name: size
143+
schema:
144+
type: string
145+
- description: "The number of items to return per page. Test QueryParam for\
146+
\ issue #8315 - must be removed when x-spring-paginated:true is used."
147+
in: query
148+
name: size
149+
required: true
150+
schema:
151+
default: 20
152+
minimum: 1
153+
type: integer
154+
- description: "The page to return, starting with page 0. Test QueryParam for\
155+
\ issue #8315 - must be removed when x-spring-paginated:true is used."
156+
in: query
157+
name: page
158+
required: true
159+
schema:
160+
default: 0
161+
minimum: 0
162+
type: integer
163+
- description: "The sorting to apply to the Pageable object. Test QueryParam\
164+
\ for issue #8315 - must be removed when x-spring-paginated:true is used."
165+
in: query
166+
name: sort
167+
required: true
168+
schema:
169+
default: "id,asc"
170+
type: string
139171
responses:
140172
200:
141173
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
@@ -138,6 +138,7 @@ ResponseEntity<List<Pet>> findPetsByStatus(
138138
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
139139
*
140140
* @param tags Tags to filter by (required)
141+
* @param size A test HeaderParam for issue #8315 - must NOT be removed when x-spring-paginated:true is used. (optional)
141142
* @return successful operation (status code 200)
142143
* or Invalid tag value (status code 400)
143144
* @deprecated
@@ -168,6 +169,7 @@ ResponseEntity<List<Pet>> findPetsByStatus(
168169

169170
ResponseEntity<List<Pet>> findPetsByTags(
170171
@NotNull @Parameter(name = "tags", description = "Tags to filter by", required = true, in = ParameterIn.QUERY) @Valid @RequestParam(value = "tags", required = true) List<String> tags,
172+
@Parameter(name = "size", description = "A test HeaderParam for issue #8315 - must NOT be removed when x-spring-paginated:true is used.", in = ParameterIn.HEADER) @RequestHeader(value = "size", required = false) String size,
171173
@ParameterObject final Pageable pageable
172174
);
173175

0 commit comments

Comments
 (0)