Skip to content

[java-spring, kotlin-spring] Enforce sort minItems/maxItems in @ValidSort - #25595

Merged
wing328 merged 4 commits into
OpenAPITools:masterfrom
Picazsoo:feature/enforce-boundaries-for-validsort
Nov 9, 2026
Merged

wing328 merged 4 commits into
OpenAPITools:masterfrom
Picazsoo:feature/enforce-boundaries-for-validsort

Conversation

@Picazsoo

@Picazsoo Picazsoo commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Enforce the sort parameter's item-count bounds in the generated @ValidSort (Spring and Kotlin Spring, generateSortValidation=true). Previously only allowed sort values were checked, so a spec allowing a single sort accepted any number of sort orders.

  • Scan: a scalar enum sort → maxItems = 1; an array sort takes minItems/maxItems from its schema, resolved through $ref chains and allOf (cycle-safe; OAS 3.1 $ref siblings via the normalizer). Contradictory bounds are dropped with a warning.
  • Emit: minItems/maxItems are added to @ValidSort(...) only when known; outputs without bounds are unchanged.
  • Templates: ValidSort gains minItems/maxItems (default unbounded; in Kotlin placed after message so positional usages still compile). The validator counts Pageable sort orders and reports a violation on sort (at least N / at most N / N..M). An unsorted Pageable stays valid. Sort properties are escaped in violation messages.
  • Docs/samples: updated generateSortValidation description, regenerated docs and the springboot-sort-validation / kotlin-springboot-sort-validation samples, with runtime tests for the bounds.

Tests run (JDK 17):

  • ./mvnw -pl modules/openapi-generator test -Dtest='ArchUnitRulesTest,SpringPageableScanUtilsTest,SpringCodegenTest,KotlinSpringServerCodegenTest': passed
  • ./mvnw -f samples/server/petstore/springboot-sort-validation/pom.xml test: passed
  • ./mvnw -f samples/server/petstore/kotlin-springboot-sort-validation/pom.xml test: passed

PR checklist

  • Read the contribution guidelines.
  • 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.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Enforces the sort parameter's item-count bounds in the generated @ValidSort annotation for Spring and Kotlin Spring. Previously only the allowed sort enum values were validated; now the number of sort orders is checked too.

  • A scalar sort gets maxItems = 1; an array sort takes its minItems/maxItems from the schema, resolved through $ref chains and allOf.
  • Bound attributes are only emitted when known, so unbounded array sorts are unchanged; Kotlin's new parameters follow message so existing positional @ValidSort(...) usages still compile.
  • An unsorted Pageable remains valid even when minItems is set; violations report at least N/at most N/min..max bounds and escape user sort properties so { } $ \ are not interpolated.
  • Contradictory bounds (minItems > maxItems) are dropped with a warning; boundary resolution is protected against cyclic allOf/$ref graphs.

Written for commit 41211a4. Summary will update on new commits.

View guided diff

Picazsoo and others added 2 commits November 8, 2026 12:30
…Sort

Scan the sort parameter's item-count bounds and pass them to @ValidSort:
a scalar enum sort gets maxItems = 1, an array sort takes minItems/maxItems
from its schema (resolved through $ref chains and allOf). Attributes are
only emitted when known, so unbounded array sorts are unchanged.

The generated ValidSort annotation gains minItems/maxItems (default
unbounded); the validator counts Pageable sort orders and reports a
violation on the sort node when out of bounds. An unsorted Pageable stays
valid.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…rule)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Picazsoo
Picazsoo marked this pull request as ready for review November 8, 2026 12:48
@Picazsoo
Picazsoo marked this pull request as draft November 8, 2026 12:49

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 22 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread docs/generators/spring.md Resolved
…m bounds

- Guard ModelUtils.resolveMin/MaxItems against cyclic allOf/$ref graphs
- Render open-ended bounds as 'at least N' / 'at most N' in violation message
- Reword docs: unsorted Pageable is always accepted, even with minItems
- Clarify generateSortValidation option description; regenerate docs
- Fix getReferencedSchemaChain Javadoc, wrap long lines, tighten tests

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Picazsoo
Picazsoo marked this pull request as ready for review November 8, 2026 13:12
@Picazsoo
Picazsoo marked this pull request as draft November 8, 2026 13:13

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

2 issues found across 22 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java:1060">
P2: The `$ref` chain resolver drops sibling constraints from OpenAPI 3.1 Schema Objects, so `minItems`/`maxItems` declared beside a sort schema `$ref` are not enforced. Preserve and merge the current schema's bounds with the referenced target while traversing the chain.</violation>
</file>

<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringPageableScanUtils.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringPageableScanUtils.java:677">
P2: `findFirst()` makes composed array schemas unsound: values allowed by the first item enum can bypass a later `allOf` item enum. Intersect all non-empty item enum constraints across the array schemas before emitting `allowedValues` (and treat an empty enum as rejecting all values).</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

if (next == schema) {
break;
}
schema = next;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2: The $ref chain resolver drops sibling constraints from OpenAPI 3.1 Schema Objects, so minItems/maxItems declared beside a sort schema $ref are not enforced. Preserve and merge the current schema's bounds with the referenced target while traversing the chain.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java, line 1060:

<comment>The `$ref` chain resolver drops sibling constraints from OpenAPI 3.1 Schema Objects, so `minItems`/`maxItems` declared beside a sort schema `$ref` are not enforced. Preserve and merge the current schema's bounds with the referenced target while traversing the chain.</comment>

<file context>
@@ -1038,6 +1039,79 @@ public static ResolvedMinBound resolveMinimumBound(OpenAPI openAPI, Schema<?> sc
+            if (next == schema) {
+                break;
+            }
+            schema = next;
+        }
+        return schema;
</file context>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Already handled: OpenAPINormalizer.normalizeReferenceSchema rewrites { $ref, maxItems } into { allOf: [$ref], maxItems } before preprocessOpenAPI, so the bound is honoured. Covered by SpringPageableScanUtilsTest.scanSortValidationEnums_refWithSiblingBounds_mergedAfterNormalization.

openAPI, array.getItems()))
.filter(items -> items != null && items.getEnum() != null
&& !items.getEnum().isEmpty())
.findFirst()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2: findFirst() makes composed array schemas unsound: values allowed by the first item enum can bypass a later allOf item enum. Intersect all non-empty item enum constraints across the array schemas before emitting allowedValues (and treat an empty enum as rejecting all values).

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringPageableScanUtils.java, line 677:

<comment>`findFirst()` makes composed array schemas unsound: values allowed by the first item enum can bypass a later `allOf` item enum. Intersect all non-empty item enum constraints across the array schemas before emitting `allowedValues` (and treat an empty enum as rejecting all values).</comment>

<file context>
@@ -625,37 +654,76 @@ public static Map<String, List<String>> scanSortValidationEnums(
+                                            openAPI, array.getItems()))
+                                    .filter(items -> items != null && items.getEnum() != null
+                                            && !items.getEnum().isEmpty())
+                                    .findFirst()
+                                    .orElse(null)
+                            : schema;
</file context>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Intentional: only the first item enum is honoured. Combining different enums via allOf is not a realistic sort spec; define the enum in one schema.

- Escape the requested sort property before building the violation
  message template, so { } $ \ are not interpolated
- Kotlin: move minItems/maxItems after message to keep positional
  @ValidSort(...) usages source compatible
- Add findPetsWithMinSortBounds (minItems: 2) sample endpoint and
  runtime tests for the lower bound and the 'at least N' message
- Test that OAS 3.1 $ref sibling bounds are merged after normalization
- Make the Java validator assertions indentation independent; sort imports

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Picazsoo
Picazsoo marked this pull request as ready for review November 8, 2026 13:53
@Picazsoo
Picazsoo marked this pull request as draft November 8, 2026 13:53

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 22 files

View guided diff | Re-trigger cubic

@Picazsoo
Picazsoo marked this pull request as ready for review November 8, 2026 14:09
@Picazsoo
Picazsoo marked this pull request as draft November 8, 2026 14:09

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 22 files

View guided diff | Re-trigger cubic

@Picazsoo Picazsoo changed the title feat(spring, kotlin-spring): enforce sort minItems/maxItems in @ValidSort [java-spring, kotlin-spring] Enforce sort minItems/maxItems in @ValidSort Nov 8, 2026
@Picazsoo
Picazsoo marked this pull request as ready for review November 8, 2026 15:09

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 22 files

View guided diff | Re-trigger cubic

@wing328
wing328 merged commit 630c258 into OpenAPITools:master Nov 9, 2026
129 checks passed
@wing328 wing328 added this to the 7.28.0 milestone Nov 9, 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