Repository navigation
[java][native] add generateJsonCreator to distinguish between oneOf types w/o discriminator - #25728
thorstenhirsch wants to merge 7 commits into
Conversation
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
|
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.
338e30b to
94f01d8
Compare
…libraries Resolve the conflict with OpenAPITools#25792 (microprofile useJackson3) in the useJackson3 option description. generateJsonCreator is only implemented for the native library. Warn and disable it for any other library instead of silently ignoring it, and say in the option description that jaxrs-spec has an option with the same name. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
@jpfinne thanks for the pointers, and I agree that the Java client options are hard to follow. I went through the templates and put together what each option does per library. It might help as a base for a cleanup discussion. What I changed in this PR (merge with master + small fixes)
Why
|
| Option | Works with | Silently ignored / broken |
|---|---|---|
useOneOfInterfaces |
shared-pojo libs, resttemplate/restclient/webclient, feign, microprofile, okhttp | jersey2, jersey3, native, okhttp-gson: class vs interface mismatch (above) |
useDeductionForOneOfInterfaces |
same as above, Jackson only | same libraries, and whenever useOneOfInterfaces=false |
useSealedOneOfInterfaces |
microprofile, restclient, webclient | others: interface is sealed, implementors are not final |
useOneOfDiscriminatorLookup |
jersey2, jersey3, native, okhttp-gson, okhttp | description omits okhttp |
generateConstructorWithAllArgs |
shared pojo, resttemplate/restclient/webclient, okhttp | feign, jersey2/3, native, okhttp-gson, microprofile |
failOnUnknownProperties |
libraries with a Jackson ObjectMapper |
resttemplate, microprofile, any Gson setup |
openApiNullable |
Jackson-based libraries | forced to false for microprofile and okhttp, without a warning |
generateJsonCreator |
native + Jackson | everything else (now warns) |
@JsonCreator for read-only properties is generated in 7 different ways across the pojo templates (annotation, required=, withXml guard all differ).
Recommendations
- One place for "option X is supported by libraries Y".
JSPECIFY_SUPPORTED_LIBRARIESalready does this for one option. AMap<option, Set<library>>could build the description text and also warn (or fail) inprocessOpts, instead of the current free-text "Only supported for..." sentences and ad-hoc checks. AsupportedLibrariesfield onCliOptionwould let the docs generator render the matrix. - Warn instead of ignoring, using the same warn-and-disable pattern for the combinations marked above (starting with
useOneOfInterfaceson jersey/okhttp-gson until they support it). - Fewer copies of the model templates. There are about 5 families, the 4
oneof_modelimplementations are independent copies, and eachpojo.mustacheis a full copy. Shared partials (e.g. for the creator constructor) would let a feature be added once. jersey2/jersey3 are only ~2 lines apart, so that would be a cheap start. generateJsonCreatorlong-term: move it toAbstractJavaCodegenso the other pojo families can use it, and consider makingtruethe default in the next major version (as jaxrs-spec does).
Happy to split any of these into separate issues/PRs if you think they are worth pursuing.
|
Too Many Requests (429) |
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.Generates a
@JsonCreator(mode = PROPERTIES)constructor holding required and read-only properties, so deserialization fails when a required property is missing instead of everyoneOfvariant matching. The option is off by default and is only implemented for the native library; other libraries get a warning and the option is disabled. 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).@JsonCreatorconstructors that chain to parent constructors for inherited required properties.Written for commit a16e8ec. Summary will update on new commits.