Repository navigation
[java][native] add generateJsonCreator to distinguish between oneOf types w/o discriminator - #25728
Open
thorstenhirsch wants to merge 3 commits into
Open
thorstenhirsch wants to merge 3 commits into
thorstenhirsch wants to merge 3 commits into
Conversation
Contributor
There was a problem hiding this comment.
All reported issues were addressed across 44 files
Tip: Get a Slack DM when cubic finishes reviewing your PRs. Connect Slack
View guided diff | Re-trigger cubic
Contributor
Author
|
The failing jobs are caused by |
…reator Address review feedback: a required nullable property must still be present when deserializing, otherwise it cannot distinguish oneOf variants. Also assert the generated XML/JAXB build dependencies in the XML regression test.
thorstenhirsch
force-pushed
the
java-generate-json-creator
branch
from
November 18, 2026 08:35
338e30b to
94f01d8
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes #25727. Besides the example in the issue I've also tested the fix successfully against a real-world openapi spec that triggered the problem.
This PR introduces a new additional property
generateJsonCreator, which is false by default, because I do not dare to change the default behavior. When set to true it will add@JsonCreatorconstructor code for the parser to check for required properties, especially to distinguish between multiple types in anoneOfarray. Actually, this might be what most people expect: a parser should fail when trying to parse a type in aoneOfarray and the input does not contain the properties that are listed in therequiredarray of said type. Unfortunately, this is not the case (at least not when unknown properties are ignored, which is the default behavior). Instead, the parser is happy with all possible types in theoneOfarray and fails with this error:So, with this PR this problem is being resolved, only 1 class will match - as long as the
requiredfields differ between the possibleoneOftypes, which is probably the case in most openapi specs that don't come with a discriminator.Please feel free not only to approve this PR, but also to change the default behavior to
generateJsonCreator=true, as this is probably the expected behavior of most users.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.
@bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08) @KannaKim (2026/07) @AndreyVMarkelov (2026/11)
Summary by cubic
Fixes #25727 by adding an opt-in
generateJsonCreatoroption for the Java native library sooneOfvariants without a discriminator no longer deserialize into multiple classes.Adds a
@JsonCreatorconstructor holding required and read-only properties to generated models, making deserialization fail when a required property is missing. This is needed when Jackson is configured to ignore unknown properties—without the creator, every variant in aoneOfarray matches and the parser errors with "2 classes match result, expected 1". The option is off by default. Models that override an inherited property with an incompatible type fall back to the no-arg constructor.Bug Fixes
oneOfvariants.jacksonPackagevalue for XML annotation imports so Jackson 3 resolvestools.jackson.dataformatcorrectly.build.gradleandpom.xmltemplates forwithXmlbuilds.New Features
generateJsonCreatorconfiguration option (native library only, Jackson 2 and 3).@JsonCreator(mode = PROPERTIES)constructors that chain to parent constructors for inherited required properties.Written for commit 5011624. Summary will update on new commits.