[java-spring, kotlin-spring] Enforce sort minItems/maxItems in @ValidSort
Merged
PI
1mo ago+1187+1187+0123456789012345678901234567890123456789-96-96-01234567890123456789
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 arraysorttakesminItems/maxItemsfrom its schema, resolved through$refchains andallOf(cycle-safe; OAS 3.1$refsiblings via the normalizer). Contradictory bounds are dropped with a warning. - Emit:
minItems/maxItemsare added to@ValidSort(...)only when known; outputs without bounds are unchanged. - Templates:
ValidSortgainsminItems/maxItems(default unbounded; in Kotlin placed aftermessageso positional usages still compile). The validator countsPageablesort orders and reports a violation onsort(at least N/at most N/N..M). An unsortedPageablestays valid. Sort properties are escaped in violation messages. - Docs/samples: updated
generateSortValidationdescription, regenerated docs and thespringboot-sort-validation/kotlin-springboot-sort-validationsamples, 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:
(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. - 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
sortgetsmaxItems = 1; an arraysorttakes itsminItems/maxItemsfrom the schema, resolved through$refchains andallOf. - Bound attributes are only emitted when known, so unbounded array sorts are unchanged; Kotlin's new parameters follow
messageso existing positional@ValidSort(...)usages still compile. - An unsorted
Pageableremains valid even whenminItemsis set; violations reportat least N/at most N/min..maxbounds and escape user sort properties so{ } $ \are not interpolated. - Contradictory bounds (
minItems>maxItems) are dropped with a warning; boundary resolution is protected against cyclicallOf/$refgraphs.