Skip to content

Commit b02ca92

Browse files
committed
Merge remote-tracking branch 'origin/feature/add-enum-validation-for-pageable' into feature/add-enum-validation-for-pageable
2 parents e4f8285 + 507128f commit b02ca92

8 files changed

Lines changed: 30 additions & 2 deletions

File tree

.github/workflows/samples-spring-jdk17.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ on:
1414
- samples/server/petstore/springboot-lombok-tostring/**
1515
- samples/server/petstore/springboot-file-delegate-optional/**
1616
- samples/server/petstore/springboot-petstore-with-api-response-examples/**
17+
- samples/server/petstore/springboot-sort-validation/**
1718
- samples/openapi3/server/petstore/spring-boot-oneof-sealed/**
1819
- samples/openapi3/server/petstore/spring-boot-oneof-interface/**
1920
pull_request:
@@ -29,6 +30,7 @@ on:
2930
- samples/server/petstore/springboot-lombok-tostring/**
3031
- samples/server/petstore/springboot-file-delegate-optional/**
3132
- samples/server/petstore/springboot-petstore-with-api-response-examples/**
33+
- samples/server/petstore/springboot-sort-validation/**
3234
- samples/openapi3/server/petstore/spring-boot-oneof-sealed/**
3335
- samples/openapi3/server/petstore/spring-boot-oneof-interface/**
3436
jobs:
@@ -52,6 +54,7 @@ jobs:
5254
- samples/server/petstore/springboot-lombok-tostring
5355
- samples/server/petstore/springboot-file-delegate-optional
5456
- samples/server/petstore/springboot-petstore-with-api-response-examples
57+
- samples/server/petstore/springboot-sort-validation
5558
- samples/openapi3/server/petstore/spring-boot-oneof-sealed
5659
- samples/openapi3/server/petstore/spring-boot-oneof-interface
5760
steps:

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@
2424
import io.swagger.v3.oas.models.parameters.Parameter;
2525
import org.openapitools.codegen.utils.ModelUtils;
2626

27-
import java.math.BigDecimal;
2827
import java.util.*;
2928
import java.util.stream.Collectors;
3029

3130
/**
3231
* Language-agnostic utility methods for scanning OpenAPI specs for Spring Pageable-related
3332
* features: sort enum validation, pageable defaults, and pageable constraints (max page/size).
3433
*
35-
* <p>Used by both {@link KotlinSpringServerCodegen} and (future) Java Spring codegen to share
34+
* <p>Used by both kotlin {@link KotlinSpringServerCodegen} and java {@link SpringCodegen} to share
3635
* scan logic. Only the mustache templates and their registration remain language-specific.</p>
3736
*/
3837
public final class SpringPageableScanUtils {

modules/openapi-generator/src/main/resources/JavaSpring/validPageable.mustache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public @interface ValidPageable {
6666
return true;
6767
}
6868

69+
if (!pageable.isPaged()) {
70+
return true;
71+
}
72+
6973
boolean valid = true;
7074
context.disableDefaultConstraintViolation();
7175

modules/openapi-generator/src/main/resources/kotlin-spring/validPageable.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class PageableConstraintValidator : ConstraintValidator<ValidPageable, Pageable>
5353

5454
override fun isValid(pageable: Pageable?, context: ConstraintValidatorContext): Boolean {
5555
if (pageable == null) return true
56+
if (!pageable.isPaged) return true
5657
5758
var valid = true
5859
context.disableDefaultConstraintViolation()

samples/server/petstore/kotlin-springboot-sort-validation/src/main/kotlin/org/openapitools/configuration/ValidPageable.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class PageableConstraintValidator : ConstraintValidator<ValidPageable, Pageable>
5353

5454
override fun isValid(pageable: Pageable?, context: ConstraintValidatorContext): Boolean {
5555
if (pageable == null) return true
56+
if (!pageable.isPaged) return true
5657

5758
var valid = true
5859
context.disableDefaultConstraintViolation()

samples/server/petstore/kotlin-springboot-sort-validation/src/test/kotlin/org/openapitools/api/PetApiValidationTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ class PetApiValidationTest {
9292
}.andExpect { status { isBadRequest() } }
9393
}
9494

95+
@Test
96+
fun `ValidPageable - unpaged Pageable is allowed (no params, no PageableDefault)`() {
97+
// When no pagination parameters are supplied and no @PageableDefault is configured,
98+
// Spring resolves Pageable.unpaged(). The validator must not throw and must return valid.
99+
mockMvc.get("${PetApi.BASE_PATH}${PetApi.PATH_FIND_PETS_WITH_SIZE_CONSTRAINT}")
100+
.andExpect { status { isOk() } }
101+
}
102+
95103
// ── @ValidPageable — size and page constraints combined ───────────────────
96104
// Endpoint: GET /pet/findWithPageAndSizeConstraint maxSize = 50, maxPage = 999
97105

samples/server/petstore/springboot-sort-validation/src/main/java/org/openapitools/configuration/ValidPageable.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public boolean isValid(Pageable pageable, ConstraintValidatorContext context) {
6666
return true;
6767
}
6868

69+
if (!pageable.isPaged()) {
70+
return true;
71+
}
72+
6973
boolean valid = true;
7074
context.disableDefaultConstraintViolation();
7175

samples/server/petstore/springboot-sort-validation/src/test/java/org/openapitools/api/PetApiValidationTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ void validPageable_sizeExceedsMaximumReturns400() throws Exception {
9696
.andExpect(status().isBadRequest());
9797
}
9898

99+
@Test
100+
void validPageable_unpagedPageableIsAllowed() throws Exception {
101+
// When no pagination parameters are supplied and no @PageableDefault is configured,
102+
// Spring resolves Pageable.unpaged(). The validator must not throw and must return valid.
103+
mockMvc.perform(get(PetApi.PATH_FIND_PETS_WITH_SIZE_CONSTRAINT))
104+
.andExpect(status().isOk());
105+
}
106+
99107
// ── @ValidPageable — size and page constraints combined ───────────────────
100108
// Endpoint: GET /pet/findWithPageAndSizeConstraint maxSize = 50, maxPage = 999
101109

0 commit comments

Comments
 (0)