Skip to content

Commit 32b6c4a

Browse files
committed
simplify implementation
1 parent 767f864 commit 32b6c4a

10 files changed

Lines changed: 31 additions & 11 deletions

File tree

docs/generators/java-camel.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3131
|artifactUrl|artifact URL in generated pom.xml| |https://github.com/openapitools/openapi-generator|
3232
|artifactVersion|artifact version in generated pom.xml. This also becomes part of the generated library's filename. If not provided, uses the version from the OpenAPI specification file. If that's also not present, uses the default value of the artifactVersion option.| |1.0.0|
3333
|async|use async Callable controllers| |false|
34+
|autoXSpringPaginated|Automatically add x-spring-paginated to operations that have 'page', 'size', and 'sort' query parameters. When enabled, operations with all three parameters will have Pageable support automatically applied. Operations with x-spring-paginated explicitly set to false will not be auto-detected. Only applies when library=spring-boot.| |false|
3435
|basePackage|base package (invokerPackage) for generated code| |org.openapitools|
3536
|bigDecimalAsString|Treat BigDecimal values as Strings to avoid precision loss.| |false|
3637
|booleanGetterPrefix|Set booleanGetterPrefix| |get|
@@ -62,6 +63,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
6263
|generateBuilders|Whether to generate builders for models| |false|
6364
|generateConstructorWithAllArgs|whether to generate a constructor for all arguments| |false|
6465
|generateGenericResponseEntity|Use a generic type for the `ResponseEntity` wrapping return values of generated API methods. If enabled, method are generated with return type ResponseEntity<?>| |false|
66+
|generatePageableConstraintValidation|Generate a @ValidPageable annotation and PageableConstraintValidator class, and apply @ValidPageable to paginated operations whose 'page' or 'size' parameter has a maximum constraint. Requires useBeanValidation=true and library=spring-boot.| |false|
67+
|generateSortValidation|Generate a @ValidSort annotation and SortValidator class, and apply @ValidSort to paginated operations whose 'sort' parameter has enum values. Requires useBeanValidation=true and library=spring-boot.| |false|
6568
|generatedConstructorWithRequiredArgs|Whether to generate constructors with required args for models| |true|
6669
|groupId|groupId in generated pom.xml| |org.openapitools|
6770
|hateoas|Use Spring HATEOAS library to allow adding HATEOAS links| |false|

docs/generators/spring.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3131
|artifactUrl|artifact URL in generated pom.xml| |https://github.com/openapitools/openapi-generator|
3232
|artifactVersion|artifact version in generated pom.xml. This also becomes part of the generated library's filename. If not provided, uses the version from the OpenAPI specification file. If that's also not present, uses the default value of the artifactVersion option.| |1.0.0|
3333
|async|use async Callable controllers| |false|
34+
|autoXSpringPaginated|Automatically add x-spring-paginated to operations that have 'page', 'size', and 'sort' query parameters. When enabled, operations with all three parameters will have Pageable support automatically applied. Operations with x-spring-paginated explicitly set to false will not be auto-detected. Only applies when library=spring-boot.| |false|
3435
|basePackage|base package (invokerPackage) for generated code| |org.openapitools|
3536
|bigDecimalAsString|Treat BigDecimal values as Strings to avoid precision loss.| |false|
3637
|booleanGetterPrefix|Set booleanGetterPrefix| |get|
@@ -55,6 +56,8 @@ These options may be applied as additional-properties (cli) or configOptions (pl
5556
|generateBuilders|Whether to generate builders for models| |false|
5657
|generateConstructorWithAllArgs|whether to generate a constructor for all arguments| |false|
5758
|generateGenericResponseEntity|Use a generic type for the `ResponseEntity` wrapping return values of generated API methods. If enabled, method are generated with return type ResponseEntity<?>| |false|
59+
|generatePageableConstraintValidation|Generate a @ValidPageable annotation and PageableConstraintValidator class, and apply @ValidPageable to paginated operations whose 'page' or 'size' parameter has a maximum constraint. Requires useBeanValidation=true and library=spring-boot.| |false|
60+
|generateSortValidation|Generate a @ValidSort annotation and SortValidator class, and apply @ValidSort to paginated operations whose 'sort' parameter has enum values. Requires useBeanValidation=true and library=spring-boot.| |false|
5861
|generatedConstructorWithRequiredArgs|Whether to generate constructors with required args for models| |true|
5962
|groupId|groupId in generated pom.xml| |org.openapitools|
6063
|hateoas|Use Spring HATEOAS library to allow adding HATEOAS links| |false|

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,11 +1258,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
12581258
List<String> sortEntries = defaults.sortDefaults.stream()
12591259
.map(sf -> "@SortDefault(sort = {\"" + sf.field + "\"}, direction = Sort.Direction." + sf.direction + ")")
12601260
.collect(Collectors.toList());
1261-
if (sortEntries.size() == 1) {
1262-
pageableAnnotations.add("@SortDefault.SortDefaults(" + sortEntries.get(0) + ")");
1263-
} else {
1264-
pageableAnnotations.add("@SortDefault.SortDefaults({" + String.join(", ", sortEntries) + "})");
1265-
}
1261+
pageableAnnotations.add("@SortDefault.SortDefaults({" + String.join(", ", sortEntries) + "})");
12661262
codegenOperation.imports.add("SortDefault");
12671263
codegenOperation.imports.add("Sort");
12681264
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6888,7 +6888,7 @@ public void sortDefaultAnnotationApplied() throws IOException {
68886888

68896889
// findPetsWithSortDefaultOnly: sort default "name,desc" → @SortDefault.SortDefaults generated
68906890
JavaFileAssert.assertThat(files.get("PetApi.java"))
6891-
.fileContains("@SortDefault.SortDefaults(");
6891+
.fileContains("@SortDefault.SortDefaults({@SortDefault(sort = {\"name\"}, direction = Sort.Direction.DESC)})");
68926892
}
68936893

68946894
@Test
@@ -6906,6 +6906,6 @@ public void sortDefaultAndPageableDefaultBothApplied() throws IOException {
69066906
// → @PageableDefault + @SortDefault.SortDefaults both present
69076907
JavaFileAssert.assertThat(files.get("PetApi.java"))
69086908
.fileContains("@PageableDefault(page = 0, size = 10)")
6909-
.fileContains("@SortDefault.SortDefaults(");
6909+
.fileContains("@SortDefault.SortDefaults({@SortDefault(sort = {\"name\"}, direction = Sort.Direction.DESC), @SortDefault(sort = {\"id\"}, direction = Sort.Direction.ASC)})");
69106910
}
69116911
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
import org.openapitools.model.ModelApiResponse;
99
import org.springframework.lang.Nullable;
1010
import org.springframework.data.domain.Pageable;
11+
import org.springframework.data.web.PageableDefault;
1112
import org.springdoc.core.annotations.ParameterObject;
1213
import org.openapitools.model.Pet;
14+
import org.springframework.data.domain.Sort;
15+
import org.springframework.data.web.SortDefault;
1316
import io.swagger.v3.oas.annotations.ExternalDocumentation;
1417
import io.swagger.v3.oas.annotations.Operation;
1518
import io.swagger.v3.oas.annotations.Parameter;
@@ -182,7 +185,7 @@ default ResponseEntity<List<Pet>> findPetsByStatus(
182185
default ResponseEntity<List<Pet>> findPetsByTags(
183186
@NotNull @Parameter(name = "tags", description = "Tags to filter by", required = true, in = ParameterIn.QUERY) @Valid @RequestParam(value = "tags", required = true) List<String> tags,
184187
@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) @Nullable String size,
185-
@ParameterObject final Pageable pageable
188+
@PageableDefault(page = 0, size = 20) @SortDefault.SortDefaults({@SortDefault(sort = {"id"}, direction = Sort.Direction.ASC)}) @ParameterObject final Pageable pageable
186189
) {
187190
return getDelegate().findPetsByTags(tags, size, pageable);
188191
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import org.openapitools.model.ModelApiResponse;
44
import org.springframework.lang.Nullable;
55
import org.springframework.data.domain.Pageable;
6+
import org.springframework.data.web.PageableDefault;
67
import org.springdoc.core.annotations.ParameterObject;
78
import org.openapitools.model.Pet;
9+
import org.springframework.data.domain.Sort;
10+
import org.springframework.data.web.SortDefault;
811
import org.springframework.http.HttpStatus;
912
import org.springframework.http.MediaType;
1013
import org.springframework.http.ResponseEntity;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
import org.openapitools.model.ModelApiResponse;
99
import org.springframework.lang.Nullable;
1010
import org.springframework.data.domain.Pageable;
11+
import org.springframework.data.web.PageableDefault;
1112
import org.springdoc.core.annotations.ParameterObject;
1213
import org.openapitools.model.Pet;
14+
import org.springframework.data.domain.Sort;
15+
import org.springframework.data.web.SortDefault;
1316
import io.swagger.v3.oas.annotations.ExternalDocumentation;
1417
import io.swagger.v3.oas.annotations.Operation;
1518
import io.swagger.v3.oas.annotations.Parameter;
@@ -182,7 +185,7 @@ default ResponseEntity<List<Pet>> findPetsByStatus(
182185
default ResponseEntity<List<Pet>> findPetsByTags(
183186
@NotNull @Parameter(name = "tags", description = "Tags to filter by", required = true, in = ParameterIn.QUERY) @Valid @RequestParam(value = "tags", required = true) List<String> tags,
184187
@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) @Nullable String size,
185-
@ParameterObject final Pageable pageable
188+
@PageableDefault(page = 0, size = 20) @SortDefault.SortDefaults({@SortDefault(sort = {"id"}, direction = Sort.Direction.ASC)}) @ParameterObject final Pageable pageable
186189
) {
187190
return getDelegate().findPetsByTags(tags, size, pageable);
188191
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import org.openapitools.model.ModelApiResponse;
44
import org.springframework.lang.Nullable;
55
import org.springframework.data.domain.Pageable;
6+
import org.springframework.data.web.PageableDefault;
67
import org.springdoc.core.annotations.ParameterObject;
78
import org.openapitools.model.Pet;
9+
import org.springframework.data.domain.Sort;
10+
import org.springframework.data.web.SortDefault;
811
import org.springframework.http.HttpStatus;
912
import org.springframework.http.MediaType;
1013
import org.springframework.http.ResponseEntity;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
import org.openapitools.model.ModelApiResponse;
99
import org.springframework.lang.Nullable;
1010
import org.springframework.data.domain.Pageable;
11+
import org.springframework.data.web.PageableDefault;
1112
import org.springdoc.core.annotations.ParameterObject;
1213
import org.openapitools.model.Pet;
14+
import org.springframework.data.domain.Sort;
15+
import org.springframework.data.web.SortDefault;
1316
import io.swagger.v3.oas.annotations.ExternalDocumentation;
1417
import io.swagger.v3.oas.annotations.Operation;
1518
import io.swagger.v3.oas.annotations.Parameter;
@@ -203,7 +206,7 @@ default ResponseEntity<List<Pet>> findPetsByStatus(
203206
default ResponseEntity<List<Pet>> findPetsByTags(
204207
@NotNull @Parameter(name = "tags", description = "Tags to filter by", required = true, in = ParameterIn.QUERY) @Valid @RequestParam(value = "tags", required = true) List<String> tags,
205208
@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) @Nullable String size,
206-
@ParameterObject final Pageable pageable
209+
@PageableDefault(page = 0, size = 20) @SortDefault.SortDefaults({@SortDefault(sort = {"id"}, direction = Sort.Direction.ASC)}) @ParameterObject final Pageable pageable
207210
) {
208211
getRequest().ifPresent(request -> {
209212
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
import org.openapitools.model.ModelApiResponse;
99
import org.springframework.lang.Nullable;
1010
import org.springframework.data.domain.Pageable;
11+
import org.springframework.data.web.PageableDefault;
1112
import org.springdoc.core.annotations.ParameterObject;
1213
import org.openapitools.model.Pet;
14+
import org.springframework.data.domain.Sort;
15+
import org.springframework.data.web.SortDefault;
1316
import io.swagger.v3.oas.annotations.ExternalDocumentation;
1417
import io.swagger.v3.oas.annotations.Operation;
1518
import io.swagger.v3.oas.annotations.Parameter;
@@ -203,7 +206,7 @@ default ResponseEntity<List<Pet>> findPetsByStatus(
203206
default ResponseEntity<List<Pet>> findPetsByTags(
204207
@NotNull @Parameter(name = "tags", description = "Tags to filter by", required = true, in = ParameterIn.QUERY) @Valid @RequestParam(value = "tags", required = true) List<String> tags,
205208
@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) @Nullable String size,
206-
@ParameterObject final Pageable pageable
209+
@PageableDefault(page = 0, size = 20) @SortDefault.SortDefaults({@SortDefault(sort = {"id"}, direction = Sort.Direction.ASC)}) @ParameterObject final Pageable pageable
207210
) {
208211
getRequest().ifPresent(request -> {
209212
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {

0 commit comments

Comments
 (0)