Skip to content

Fix #23933: Added @JsonPropertyOrder annotation to Spring Boot generator#23957

Open
thesauravpoddar wants to merge 1 commit into
OpenAPITools:masterfrom
thesauravpoddar:issue23933
Open

Fix #23933: Added @JsonPropertyOrder annotation to Spring Boot generator#23957
thesauravpoddar wants to merge 1 commit into
OpenAPITools:masterfrom
thesauravpoddar:issue23933

Conversation

@thesauravpoddar

@thesauravpoddar thesauravpoddar commented Jun 5, 2026

Copy link
Copy Markdown

This PR adds the @JsonPropertyOrder annotation to Spring Boot generator to fix the problem that Jackson 3 by default sorts properties alphabetically instead of following the logical order of the generated classes. It ensures that JSON properties are serialized in the logical order defined in the OpenAPI specification, while correctly excluding enum models from the class-level annotation. I addes proper import for @JsonPropertyOrder annotation.

Updated the POJO.mustache for spring boot with @JsonPropertyOrder annotation and updated the AbstractJavaCodegen with adding import statement of @JsonPropertyOrder annotation under method processOpts .

And updated the SpringCodegen class in which i updated the method postProcessModelProperty .
Then finally adding test code for this in 'SpringCodegenTest.java' class

cc @Zomzog @martin-mfg @cachescrubber @welshm

PR checklist

  • [x ] Read the contribution guidelines.
  • [ x] 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.
  • [ x] 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

Add @JsonPropertyOrder to generated Spring Boot models so JSON follows the OpenAPI field order instead of Jackson 3’s alphabetical sort. Enum models are excluded from the class-level annotation.

  • Bug Fixes

    • Added JsonPropertyOrder import mapping in AbstractJavaCodegen.
    • Updated Spring Boot pojo.mustache to apply @JsonPropertyOrder and emit JSON_PROPERTY_* constants.
    • Updated SpringCodegen to import JsonPropertyOrder for non-enum models only.
    • Added tests in SpringCodegenTest and regenerated Spring sample models.
  • Migration

    • Regenerate Spring Boot code to pick up ordered serialization. No code changes required.

Written for commit 465a7af. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1598 files

Note: This PR contains a large number of files. cubic only reviews up to 100 files per PR, so some files may not have been reviewed. cubic prioritizes the most important files to review.
On a pro plan you can use ultrareview for larger PRs.

Re-trigger cubic

@thesauravpoddar

thesauravpoddar commented Jun 11, 2026

Copy link
Copy Markdown
Author

HI! @Zomzo can you please look at the pull request

@wing328

wing328 commented Jun 15, 2026

Copy link
Copy Markdown
Member

thanks for the pr

please resolve the merge conflicts when you've time.

cc @cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08)

@thesauravpoddar

Copy link
Copy Markdown
Author

@cubic-dev-ai ultrareview

@cubic-dev-ai

cubic-dev-ai Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai ultrareview

@thesauravpoddar Ultrareview is only available on the Pro plan. Upgrade here to get access.

@thesauravpoddar

Copy link
Copy Markdown
Author

Can You please look at this PR whenever you get time.

cc @cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08)

@thesauravpoddar

Copy link
Copy Markdown
Author

Hey can you please look at this

@thesauravpoddar

Copy link
Copy Markdown
Author

@wing328 can you please look at this

@MelleD

MelleD commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

This PR adds the @JsonPropertyOrder annotation to Spring Boot generator to fix the problem that Jackson 3 by default sorts properties alphabetically instead of following the logical order of the generated classes

If you don’t want that you have just reconfigure your JsonMapper Bean like it was before. See https://spring.io/blog/2025/10/07/introducing-jackson-3-support-in-spring

I see no reason to set an annotation here, every project can decide and configure the jsonmapper how they want

@petervandenbroek

Copy link
Copy Markdown

This PR adds the @JsonPropertyOrder annotation to Spring Boot generator to fix the problem that Jackson 3 by default sorts properties alphabetically instead of following the logical order of the generated classes

If you don’t want that you have just reconfigure your JsonMapper Bean like it was before. See https://spring.io/blog/2025/10/07/introducing-jackson-3-support-in-spring

I see no reason to set an annotation here, every project can decide and configure the jsonmapper how they want

This annotation already exists for the JAX-RS service (see #3778), all this MR does is extend the same functionality (as requested in #23933). Also, having this annotation makes the functionality for Jackson 3 exactly as it worked by default in Jackson 2, why would that be a bad thing?

I haven't tested if setting the "spring.jackson.use-jackson2-defaults" property reverts the weird alphabetical order, but manually configuring the Jackson mapper didn't do much.

@MelleD

MelleD commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

There’s no reason to change the code if the functionality already exists. It’s simply unnecessary to redevelop features that are already there. The code also needs to be maintained, etc. Why would redundancy make the situation any better?

If you don’t want an alphabetical order but rather the order of your DTO class, you just need to set a flag to false, and that’s it. With Jackson 2, you had to set it to true if you wanted that. They simply changed the default; nothing about Jackson’s functionality changed.

That’s the standard way to use Jackson—you configure the JsonMapper/ObjectMapper according to your needs.
It’s the same with wanting to handle JsonInclude for absent, empty, etc.
There are so many customization options on the JsonMapper, so I’d solve it at that level if possible. And in this case, it’s already implemented.

Hier are some good migration guides:
https://github.com/FasterXML/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md

with the default changes from spring boot 4
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-4.0-Migration-Guide#upgrading-jackson

You can just revert the default changes and should work like before.

Here are a small example for spring boot with some changes

   @Bean
   JsonMapperBuilderCustomizer jacksonCustomizer() {
      return builder -> builder.configure( MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, false )
            .constructorDetector( ConstructorDetector.USE_DELEGATING )
            .changeDefaultPropertyInclusion( incl -> incl.withValueInclusion( JsonInclude.Include.NON_ABSENT ) );
   }

For your case this make the trick like in jackson 2

builder.configure( MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, false )

@thesauravpoddar

Copy link
Copy Markdown
Author

So I looked at the codebase and i found not only JAX-RX uses jasonproperty order Micronaut too uses it
image
so was there a reason jackson config alone wasn't sufficient there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants