Skip to content

[kotlin-spring] - feature: validate Pageable params [size, page, sort] and preserve defaults#23518

Open
Picazsoo wants to merge 24 commits intoOpenAPITools:masterfrom
Picazsoo:feature/add-enum-validation-for-pageable
Open

[kotlin-spring] - feature: validate Pageable params [size, page, sort] and preserve defaults#23518
Picazsoo wants to merge 24 commits intoOpenAPITools:masterfrom
Picazsoo:feature/add-enum-validation-for-pageable

Conversation

@Picazsoo
Copy link
Copy Markdown
Contributor

@Picazsoo Picazsoo commented Apr 10, 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.

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 validation for Spring Pageable params in Kotlin and Java generators. Generates @ValidSort and @ValidPageable, preserves page/size defaults with @PageableDefault, auto-detects pageable operations, and adds spring-boot-starter-validation when useBeanValidation=true.

  • New Features

    • New options generateSortValidation and generatePageableConstraintValidation (requires useBeanValidation=true, library=spring-boot) generate ValidSort and ValidPageable in configPackage for Kotlin and Java.
    • autoXSpringPaginated auto-detects page/size/sort unless x-spring-paginated: false; adds @PageableDefault(page = 0, size = 20) when needed.
    • Validation: @ValidSort(allowedValues=[...]) from enum (inline or $ref), supports exploded and non-exploded arrays; @ValidPageable(maxSize=..., maxPage=...) from maximum on size/page. Adds spring-boot-starter-validation to Gradle/Maven when useBeanValidation=true. Docs updated; new sample samples/server/petstore/kotlin-springboot-sort-validation; CI runs it; tests added for Kotlin and Java.
  • Migration

    • Enable with generateSortValidation: "true" and/or generatePageableConstraintValidation: "true", plus useBeanValidation: "true" and library: spring-boot (optionally autoXSpringPaginated: "true").
    • Define enum values on sort and set maximum on page/size to activate. ValidSort/ValidPageable are generated under your configPackage.
    • Use useBeanValidation (not beanValidations) in configs/CLI. @PageableDefault(page = 0, size = 20) is added when pageable defaults are missing.

Written for commit bf56873. Summary will update on new commits.

@Picazsoo Picazsoo marked this pull request as ready for review April 14, 2026 17:31
@Picazsoo
Copy link
Copy Markdown
Contributor Author

Picazsoo commented Apr 14, 2026

@PageableDefault(page = 0, size = 20) is now added automatically for paginated operations without explicit page/size.

This is not true - the defaults are set only when they are specified in API. When not specified, they do not appear (and basically fall back to the spring defaults which are typically page = 0, size = 10. But no annotation is needed for that). Right @cubic-dev-ai ?

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.

1 issue found across 37 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/server/petstore/kotlin-springboot-sort-validation/pom.xml">

<violation number="1" location="samples/server/petstore/kotlin-springboot-sort-validation/pom.xml:133">
P2: Validation runtime provider is missing in Maven config; only Bean Validation API is declared, so custom `@ValidSort` constraints may not execute.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread samples/server/petstore/kotlin-springboot-sort-validation/pom.xml
@Picazsoo Picazsoo changed the title Feature/add enum validation for pageable [kotlin-spring] - feature: validate Pageable params [size, page, sort] and preserve defaults Apr 14, 2026
@Picazsoo Picazsoo force-pushed the feature/add-enum-validation-for-pageable branch from 2257b7c to 9728c9a Compare April 14, 2026 21:29
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