feat: Make model property generation deterministic by sorting alphabetically#22836
Conversation
|
thanks for the PR what about adding this functionality in openapi normalizer via a new rule (e.g. SORT_MODEL_PROPOERTIES) so that this will work with all generators? a good starting point is https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java#L883 (update the method signature to accept schema instead of schema.getProperties()) |
|
alright, will do. would this be okay to enable |
4cd430c to
779e1a8
Compare
I prefer not to at this stage as this will "change" the output for all generators. generally we advise the users to put the properties in whatever order they prefer in the source (openapi spec) instead. |
|
please update the doc as well: https://github.com/OpenAPITools/openapi-generator/blob/master/docs/customization.md#openapi-normalizer |
|
for new rules, we also prefer a test or 2 in https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java let me know if you need help adding tests 🙏 |
…tput Add new OpenAPINormalizer rule SORT_MODEL_PROPERTIES that sorts schema properties alphabetically by name. This ensures deterministic code generation output regardless of property ordering in the source spec. The rule: - Uses TreeMap to sort properties by natural string order - Applies at the OpenAPI normalization stage, working for all generators - Is opt-in (defaults to false) to maintain backward compatibility Usage: openapi-generator generate --openapi-normalizer SORT_MODEL_PROPERTIES=true ... Fixes non-deterministic property ordering that could cause spurious diffs in generated code when the source schema order varies.
779e1a8 to
fa9c0cd
Compare
|
applied requested changes and force pushed; lemme know if something looks off! |
|
looks good at the first glance 👍 to confirm, you did test this locally with your use cases and it works as expected, right? |
|
did some tests locally and the result works as expected |



Summary
added
SORT_MODEL_PROPERTIESrule to the OpenAPI Normalizer that sorts schema properties alphabetically by name. This ensures deterministic code generation output regardless of property ordering in the source spec, preventing unnecessary diffs like:this PR was generated by claude opus 4.5
Changes
OpenAPINormalizer.java: Added new
SORT_MODEL_PROPERTIESrule that:docs/customization.md: Added documentation for the new rule
OpenAPINormalizerTest.java: Added tests for:
Usage
PR checklist
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.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)"fixes #123"present in the PR description)