[JAVA-SPRING,KOTLIN-SPRING] - feature: validate Pageable params [size, page, sort] and preserve defaults; Add 'autoXSpringPaginated' to java-spring
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
Version of this PR with more extra functionality (a couple more commits) is available here: #23575
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:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
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(upcoming7.x.0minor 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 validation for Spring Pageable params (page, size, sort) in Kotlin and Java generators and preserves spec defaults when converting to Pageable. Also adds autoXSpringPaginated for Java and Kotlin, updates docs, and includes new samples and CI updates.
-
New Features
- Options
generateSortValidationandgeneratePageableConstraintValidation(withuseBeanValidation=true,library=spring-boot) generate@ValidSortand@ValidPageableunder yourconfigPackagefor Kotlin and Java;@ValidSort(allowedValues=[...])supports exploded and non‑exploded arrays;@ValidPageable(maxSize=..., maxPage=...)readsmaximumfromsize/pageand safely ignoresPageable.unpaged(). autoXSpringPaginateddetectspage/size/sort(unlessx-spring-paginated: false) and injects@PageableDefault(page = 0, size = 20)when defaults are missing.- Adds
spring-boot-starter-validationwhenuseBeanValidation=true. Docs updated (spring,kotlin-spring,java-camel); new Kotlin and Java sort/pageable validation samples; CI builds include the new samples and add exponential retry toapt-getinstalls.
- Options
-
Migration
- Enable with
generateSortValidation: "true"and/orgeneratePageableConstraintValidation: "true", plususeBeanValidation: "true"andlibrary: spring-boot(optionallyautoXSpringPaginated: "true"). - Define enum values on
sortand setmaximumonpage/size.@ValidSort/@ValidPageableare generated under yourconfigPackage. - Use
useBeanValidation(notbeanValidations) in configs/CLI.@PageableDefault(page = 0, size = 20)is added when pageable defaults are missing.
- Enable with