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

Merged
PI
6mo ago
+10309-35

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.