Skip to content

[java-spring;kotlin-spring] - feature: validate Pageable params [size, page, sort] and preserve defaults; Add 'autoXSpringPaginated' to java-spring#23518

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

[java-spring;kotlin-spring] - feature: validate Pageable params [size, page, sort] and preserve defaults; Add 'autoXSpringPaginated' to java-spring#23518
Picazsoo wants to merge 28 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 (page, size, sort) in Kotlin and Java generators and keeps spec defaults when converting to Pageable. Auto-detects pageable operations, adds @PageableDefault for defaults, and includes spring-boot-starter-validation when useBeanValidation=true.

  • New Features

    • Options generateSortValidation and generatePageableConstraintValidation (with useBeanValidation=true, library=spring-boot) generate @ValidSort and @ValidPageable under your configPackage for Kotlin and Java.
    • autoXSpringPaginated detects page/size/sort (unless x-spring-paginated: false) and injects @PageableDefault(page = 0, size = 20) when defaults are missing.
    • Validation: @ValidSort(allowedValues=[...]) from enum (inline or $ref), works with exploded and non‑exploded arrays; @ValidPageable(maxSize=..., maxPage=...) from maximum on size/page.
    • Docs updated (spring, kotlin-spring, java-camel); new samples added for Kotlin samples/server/petstore/kotlin-springboot-sort-validation and Spring samples/server/petstore/springboot-sort-validation; CI builds updated for both.
  • 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. @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 6be3da3. 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
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.

2 issues found across 24 files (changes from recent commits).

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/springboot-sort-validation/src/main/java/org/openapitools/configuration/ValidPageable.java">

<violation number="1" location="samples/server/petstore/springboot-sort-validation/src/main/java/org/openapitools/configuration/ValidPageable.java:72">
P2: Validator does not handle `Pageable.unpaged()` before calling `getPageSize()`/`getPageNumber()`, which can throw and break request validation flow.</violation>
</file>

<file name="samples/server/petstore/springboot-sort-validation/src/main/java/org/openapitools/RFC3339DateFormat.java">

<violation number="1" location="samples/server/petstore/springboot-sort-validation/src/main/java/org/openapitools/RFC3339DateFormat.java:36">
P2: `clone()` returns `this` instead of a cloned object, creating shared-instance behavior that breaks expected `DateFormat` clone semantics and can cause state-sharing/thread-safety problems.</violation>
</file>

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

boolean valid = true;
context.disableDefaultConstraintViolation();

if (maxSize >= 0 && pageable.getPageSize() > maxSize) {
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 15, 2026

Choose a reason for hiding this comment

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

P2: Validator does not handle Pageable.unpaged() before calling getPageSize()/getPageNumber(), which can throw and break request validation flow.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/server/petstore/springboot-sort-validation/src/main/java/org/openapitools/configuration/ValidPageable.java, line 72:

<comment>Validator does not handle `Pageable.unpaged()` before calling `getPageSize()`/`getPageNumber()`, which can throw and break request validation flow.</comment>

<file context>
@@ -0,0 +1,95 @@
+            boolean valid = true;
+            context.disableDefaultConstraintViolation();
+
+            if (maxSize >= 0 && pageable.getPageSize() > maxSize) {
+                context.buildConstraintViolationWithTemplate(
+                                context.getDefaultConstraintMessageTemplate()
</file context>
Fix with Cubic


@Override
public Object clone() {
return this;
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 15, 2026

Choose a reason for hiding this comment

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

P2: clone() returns this instead of a cloned object, creating shared-instance behavior that breaks expected DateFormat clone semantics and can cause state-sharing/thread-safety problems.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/server/petstore/springboot-sort-validation/src/main/java/org/openapitools/RFC3339DateFormat.java, line 36:

<comment>`clone()` returns `this` instead of a cloned object, creating shared-instance behavior that breaks expected `DateFormat` clone semantics and can cause state-sharing/thread-safety problems.</comment>

<file context>
@@ -0,0 +1,38 @@
+
+  @Override
+  public Object clone() {
+    return this;
+  }
+}
</file context>
Fix with Cubic

@Picazsoo Picazsoo changed the title [kotlin-spring] - feature: validate Pageable params [size, page, sort] and preserve defaults [java-spring;kotlin-spring] - feature: validate Pageable params [size, page, sort] and preserve defaults; Add 'autoXSpringPaginated' to java-spring Apr 15, 2026
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