Skip to content

[JAVA-SPRING,KOTLIN-SPRING] - feature: validate Pageable params [size, page, sort] and preserve defaults; add PagedModel<T> support; Add 'autoXSpringPaginated' to java-spring#23575

Merged
wing328 merged 48 commits intoOpenAPITools:masterfrom
Picazsoo:feature/add-enum-validation-for-pageable-paged-model
Apr 21, 2026
Merged

[JAVA-SPRING,KOTLIN-SPRING] - feature: validate Pageable params [size, page, sort] and preserve defaults; add PagedModel<T> support; Add 'autoXSpringPaginated' to java-spring#23575
wing328 merged 48 commits intoOpenAPITools:masterfrom
Picazsoo:feature/add-enum-validation-for-pageable-paged-model

Conversation

@Picazsoo
Copy link
Copy Markdown
Contributor

@Picazsoo Picazsoo commented Apr 16, 2026

The idea behind this PR is the fact that currently when page, size, sort are substituted with Pageable, then the default values for page, size, sort are lost and Pageable defaults to global spring defaults (size = 10; page = 0). The native spring annotation @PageableDefault makes it possible to respect the defaults specified in the api spec. Native spring annotation @SortDefault.SortDefaults(...) on the other hand preserves the sort default value if it exists.

Another issue stemming from conversion of page, size, sort to Pageableis the loss of query param validations. This PR implements two custom validators (@ValidPageable and @ValidSort) that verify the validity of the passed values and fail if the values are out of spec. These validators should be easily replaceable with custom implementations via importMapping - this might be useful if one has some non-standard sort params and wants to handle the validation in a specific way.

So all together this PR makes sure that the convenience provided by conversion from separate query params page, size, sort to Pageable does not result in less strict contract in the generated api.

Some of the logic is extracted to separate class on purpose. Since I would like to implement this also in java-spring generator for feature parity.

Edit: This PR now supports java-spring as well. For feature parity I also added the autoXSpringPaginated, which can automatically convert all cases of page, size, sort params to a single Pageable (and can be disabled per operation by setting x-spring-paginated: false

Edit2: This PR now also supports replacement of DTOs matching PagedModel shape like:

{
  "content": [],
  "page": {
    "size": 10,
    "number": 0,
    "totalElements": 1,
    "totalPages": 1
  }
}

to generic version of the PagedModel<type-of-content-array> in both java-spring and kotlin-spring generator

Everything besides @PageableDefault in this PR is opt-in

This is a version of #23518 with the PagedModel functionality added.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request. - @karismann @Zomzog @andrewemery @4brunu @yutaka0m @stefankoppier @e5l @dennisameling @wing328

Summary by cubic

Adds opt‑in validation for Spring Pageable and sort params, auto‑detects paginated endpoints, preserves defaults via @PageableDefault, and substitutes generic PagedModel<T> for paged responses in spring and kotlin-spring.

  • New Features

    • Generate @ValidPageable to enforce page/size max constraints from the OpenAPI schema (generatePageableConstraintValidation), safely skipping Pageable.unpaged().
    • Generate @ValidSort to validate sort values against an enum, supporting exploded and non‑exploded array styles (generateSortValidation).
    • Auto‑detect pageable operations and add x-spring-paginated when page + size + sort are present (autoXSpringPaginated), respecting per‑operation opt‑out (x-spring-paginated: false) and available in java-spring.
    • Substitute detected paged response shapes with PagedModel<T> via schema scanning (substituteGenericPagedModel), preserving the original schema when needed for @ApiResponse/springdoc.
    • Add @PageableDefault when defaults are defined for page/size.
    • New tests and samples (Java and Kotlin) for sort validation, pageable constraints, auto‑pagination, and paged model substitution; CI workflows updated to build them.
  • Migration

    • Use useBeanValidation instead of deprecated beanValidations; docs and samples updated.
    • When useBeanValidation=true, add org.springframework.boot:spring-boot-starter-validation in Kotlin Spring Boot templates.
    • Kotlin Spring Boot samples/templates updated to Spring Boot 3.3.13.

Written for commit 8aefea4. Summary will update on new commits.

Picazsoo and others added 30 commits April 10, 2026 19:31
   Calling getPageSize()/getPageNumber() on Pageable.unpaged() throws
   UnsupportedOperationException. Add an isPaged() check so unpaged
   requests pass validation without error.
@Picazsoo Picazsoo changed the title Feature/add enum validation for pageable paged model [JAVA-SPRING,KOTLIN-SPRING] - feature: validate Pageable params [size, page, sort] and preserve defaults; add PagedModel<T> support; Add 'autoXSpringPaginated' to java-spring Apr 19, 2026
@Picazsoo Picazsoo marked this pull request as ready for review April 19, 2026 20:45
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 198 files

Note: This PR contains a large number of files. cubic only reviews up to 75 files per PR, so some files may not have been reviewed. cubic prioritises the most important files to review.

…aged-model

# Conflicts:
#	modules/openapi-generator/src/main/resources/kotlin-spring/libraries/spring-boot/buildGradle-sb3-Kts.mustache
#	samples/server/petstore/kotlin-spring-sealed-interfaces/build.gradle.kts
#	samples/server/petstore/kotlin-springboot-3-no-response-entity/build.gradle.kts
#	samples/server/petstore/kotlin-springboot-3/build.gradle.kts
#	samples/server/petstore/kotlin-springboot-additionalproperties/build.gradle.kts
#	samples/server/petstore/kotlin-springboot-delegate-nodefaults/build.gradle.kts
#	samples/server/petstore/kotlin-springboot-integer-enum/build.gradle.kts
#	samples/server/petstore/kotlin-springboot-request-cookie/build.gradle.kts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants