Skip to content

[BUG][openapi-yaml] Preserve field order in auto-generated object-level examples (#23664)#23674

Open
swapneswarsundarray wants to merge 2 commits intoOpenAPITools:masterfrom
swapneswarsundarray:fix/openapi-yaml-example-order
Open

[BUG][openapi-yaml] Preserve field order in auto-generated object-level examples (#23664)#23674
swapneswarsundarray wants to merge 2 commits intoOpenAPITools:masterfrom
swapneswarsundarray:fix/openapi-yaml-example-order

Conversation

@swapneswarsundarray
Copy link
Copy Markdown

@swapneswarsundarray swapneswarsundarray commented May 2, 2026

Fixes #23664

Problem

When the openapi-yaml generator constructs object-level examples from property-level examples, the resulting field order is unstable and does not match the property declaration order in the source specification.

Root Cause

The aggregated example is built using an unordered Map (HashMap), which does not preserve insertion order.

Solution

Use LinkedHashMap when constructing the example and iterate over schema properties in declaration order to ensure deterministic output.

Changes

  • Replace HashMap with LinkedHashMap in ExampleGenerator when aggregating object-level examples
  • Add test to verify that property order is preserved

Verification

  • All existing tests pass
  • Added test confirms that generated examples maintain declaration order (e.g., zebra, apple, mango, cherry, banana)

Impact

Generated examples are now deterministic and aligned with the source specification. This improves readability and consistency in downstream tools such as Swagger UI.

Test

  • Added a schema with non-alphabetical property order
  • Verified generated example preserves the same order

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.

Summary by cubic

Preserves property order in auto-generated object examples in openapi-yaml, so outputs are deterministic and match the spec’s declaration order. This improves readability in Swagger UI and other tools.

  • Bug Fixes
    • Replaced HashMap with LinkedHashMap in ExampleGenerator to keep insertion order.
    • Added a unit test that asserts example fields appear in schema order.

Written for commit ab188a8. Summary will update on new commits.

The openapi-yaml generator was using HashMap when building object-level
examples from property examples, which caused unstable field ordering
that didn't match the source spec declaration order.

Switch to LinkedHashMap to preserve the order as defined in the
OpenAPI spec. This improves readability in Swagger UI and other
downstream tools that render the auto-generated examples.

Add test to verify property order preservation.
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 2 files

@wing328
Copy link
Copy Markdown
Member

wing328 commented May 2, 2026

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

…enAPITools#23664)

The openapi-yaml generator was using HashMap when building object-level
examples from property examples, which caused unstable field ordering
that didn't match the source spec declaration order.

Switch to LinkedHashMap to preserve the order as defined in the
OpenAPI spec. This improves readability in Swagger UI and other
downstream tools that render the auto-generated examples.

Add test to verify property order preservation.
@swapneswarsundarray swapneswarsundarray force-pushed the fix/openapi-yaml-example-order branch from aa50335 to ab188a8 Compare May 2, 2026 04:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][openapi-yaml] Auto-generated object-level example has unstable field order due to non-ordered Map

2 participants