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

Merged
PI
1mo ago
+1187-96

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.