Skip to content

feat: Make model property generation deterministic by sorting alphabetically#22836

Merged
wing328 merged 1 commit intoOpenAPITools:masterfrom
scarf005:fix/typescript-ordering
Jan 28, 2026
Merged

feat: Make model property generation deterministic by sorting alphabetically#22836
wing328 merged 1 commit intoOpenAPITools:masterfrom
scarf005:fix/typescript-ordering

Conversation

@scarf005
Copy link
Copy Markdown
Contributor

@scarf005 scarf005 commented Jan 28, 2026

Summary

added SORT_MODEL_PROPERTIES rule 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:

-    'paged'?: boolean;
     'unpaged'?: boolean;
+    'paged'?: boolean;

this PR was generated by claude opus 4.5

Changes

  1. OpenAPINormalizer.java: Added new SORT_MODEL_PROPERTIES rule that:

    • 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
  2. docs/customization.md: Added documentation for the new rule

  3. OpenAPINormalizerTest.java: Added tests for:

    • Verifying properties are sorted when rule is enabled
    • Verifying properties retain original order when rule is disabled (default)

Usage

openapi-generator generate \
  -g java \
  -i petstore.yaml \
  -o /tmp/java-client/ \
  --openapi-normalizer SORT_MODEL_PROPERTIES=true

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.

@wing328
Copy link
Copy Markdown
Member

wing328 commented Jan 28, 2026

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())

@scarf005
Copy link
Copy Markdown
Contributor Author

alright, will do. would this be okay to enable SORT_MODEL_PROPOERTIES by default?

@scarf005 scarf005 force-pushed the fix/typescript-ordering branch from 4cd430c to 779e1a8 Compare January 28, 2026 07:40
@wing328
Copy link
Copy Markdown
Member

wing328 commented Jan 28, 2026

alright, will do. would this be okay to enable SORT_MODEL_PROPOERTIES by default?

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.

@wing328
Copy link
Copy Markdown
Member

wing328 commented Jan 28, 2026

@wing328
Copy link
Copy Markdown
Member

wing328 commented Jan 28, 2026

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 🙏

@wing328 wing328 added Enhancement: Feature OpenAPI Normalizer Normalize the spec for easier processing labels Jan 28, 2026
@wing328 wing328 added this to the 7.20.0 milestone Jan 28, 2026
…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.
@scarf005 scarf005 force-pushed the fix/typescript-ordering branch from 779e1a8 to fa9c0cd Compare January 28, 2026 08:22
@scarf005 scarf005 changed the title feat(typescript): Make model property generation deterministic by sorting alphabetically feat: Make model property generation deterministic by sorting alphabetically Jan 28, 2026
@scarf005 scarf005 marked this pull request as ready for review January 28, 2026 08:30
@scarf005
Copy link
Copy Markdown
Contributor Author

applied requested changes and force pushed; lemme know if something looks off!

@wing328
Copy link
Copy Markdown
Member

wing328 commented Jan 28, 2026

looks good at the first glance 👍

to confirm, you did test this locally with your use cases and it works as expected, right?

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.

No issues found across 3 files

@scarf005
Copy link
Copy Markdown
Contributor Author

looks good at the first glance 👍

to confirm, you did test this locally with your use cases and it works as expected, right?

checked locally, typescript-axios seems to work well

Details image

Before

image

After

image

@wing328
Copy link
Copy Markdown
Member

wing328 commented Jan 28, 2026

did some tests locally and the result works as expected

@wing328 wing328 merged commit f7ac63a into OpenAPITools:master Jan 28, 2026
15 checks passed
@scarf005 scarf005 deleted the fix/typescript-ordering branch January 28, 2026 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement: Feature OpenAPI Normalizer Normalize the spec for easier processing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants