[Java] Fix issue 22209 oneof pojo #23594
Conversation
… of abstract wrapper and ensure real POJO model is produced - Add useOneOfPojo option to generate real POJO classes for oneOf schemas instead of wrapper models - Override updateModelForComposedSchema() to restore original vars when option is enabled - Process the option in processOpts() method with default value false for backward compatibility fixes OpenAPITools#22209
0d98002 to
09ed65d
Compare
- Added bin/configs/java-jersey3-oneOf-pojo.yaml config with useOneOfPojo: true - Generated samples/client/petstore/java/jersey3-oneOf-pojo/ - Demonstrates how useOneOfPojo=true generates real POJO classes instead of wrapper models
There was a problem hiding this comment.
7 issues found across 47 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="samples/client/petstore/java/jersey3-oneOf-pojo/src/main/java/org/openapitools/client/Configuration.java">
<violation number="1" location="samples/client/petstore/java/jersey3-oneOf-pojo/src/main/java/org/openapitools/client/Configuration.java:39">
P2: `AtomicReference.updateAndGet` callback invokes a potentially side-effectful supplier, which may run multiple times under contention.</violation>
</file>
<file name="samples/client/petstore/java/jersey3-oneOf-pojo/README.md">
<violation number="1" location="samples/client/petstore/java/jersey3-oneOf-pojo/README.md:18">
P2: README minimum Java version is incorrect for the Jersey 3.1 dependency baseline.</violation>
</file>
<file name="samples/client/petstore/java/jersey3-oneOf-pojo/src/main/java/org/openapitools/client/RFC3339JavaTimeModule.java">
<violation number="1" location="samples/client/petstore/java/jersey3-oneOf-pojo/src/main/java/org/openapitools/client/RFC3339JavaTimeModule.java:32">
P2: Custom deserializers are registered after `super.setupModule(context)`, so they may not be applied to the mapper during module registration.</violation>
</file>
<file name="samples/client/petstore/java/jersey3-oneOf-pojo/git_push.sh">
<violation number="1" location="samples/client/petstore/java/jersey3-oneOf-pojo/git_push.sh:42">
P2: The script checks for any remote instead of specifically checking for `origin`, which can cause pull/push failures when only non-`origin` remotes exist.</violation>
<violation number="2" location="samples/client/petstore/java/jersey3-oneOf-pojo/git_push.sh:48">
P2: Access token is embedded in the Git remote URL, risking plaintext secret exposure in `.git/config` and related tooling.</violation>
<violation number="3" location="samples/client/petstore/java/jersey3-oneOf-pojo/git_push.sh:57">
P2: `git push` failures can be masked because the script returns the pipeline/`grep` status instead of the push command status.</violation>
</file>
<file name="samples/client/petstore/java/jersey3-oneOf-pojo/src/main/java/org/openapitools/client/ApiClient.java">
<violation number="1" location="samples/client/petstore/java/jersey3-oneOf-pojo/src/main/java/org/openapitools/client/ApiClient.java:851">
P2: Form-urlencoded serialization overwrites accumulated parameters, causing only the last field to be sent.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
|
@Chhida I don't think the generated code will work. The inline oneOf complexifies the generation. This is simpler: The spring generator has an option For example: It generates: I would implement USE_DEDUCTION_FOR_ONE_OF_INTERFACES for the java generators:
|
*@jpfinne - Thank you for your suggestion. Here's my analysis: Test with our Schema in the issue #22209I tested "SimulationDto": {
"discriminator": {
"propertyName": "type"
},
"oneOf": [
{ "$ref": "#/components/schemas/SimulationSatdDto" },
{ "$ref": "#/components/schemas/SimulationMedDto" },
{ "$ref": "#/components/schemas/SimulationPcaDto" }
]
}Result with
|
|
@Chhida Your new sample is totally different. USE_DEDUCTION_FOR_ONE_OF_INTERFACES is used when there is NO discriminator (as in your first sample). I think my PR #23543 solves your issue. It introduces a new REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING normalizer so available for all generators. I don't find the contract in #22209 so I can't test it. Can you? |
|
|
@Chhida using the REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING, I get But... the contract should be simplified first.
|
[@jpfinne] - Thank you for your analysis. You're correct that our schema has structural issues (inline enums, property redefinitions). However, we have a constraint to not modify the schema as it's a production API contract. this PR (
|
|
Note: I am only a contributor, so everything is my own opinions. Hi, could we updated the issue with details regarding what exactly that we are generating and with what settings, so it is possible to reason around what is possible for your scenario? I would assume it is a java client with Could we also extend the generated samples (i.e., the tests) to be something more complex/representative? Currently it does not give a good intuition of what we are trying to achieve, as highlighted also by questions asked previously as well. What is the reason for the interface being insufficient due to lacking getters/setters? If it is the fact that we cannot act on the inteface reference when we pass it around (e.g., shared value X is not present), then would it not be somewhat solved with I would also like to state that I am generally against this change. The generator already have a lot of discriminator settings, that all cover one of the odd cases of possible ways to express polymorphism in OAS. The generator should not own the "corner-case fixes" for these scenarios. And if you consider a scenario to not be a corner case. Then please showcase this with sources and tests that illustrate that this is a meaningful general behavior. Being able to inject a |
|
@Mattias-Sehlstedt @Chhida my PR #23543 takes the approach of an OpenApiNormalizer rule. for example: The normalizer removes the oneOf: There was a bug in InlineModelResolver, the enum in the SimulationDto was not converted. The generator detects inheritance and generates a normal class: |
fixes #22209
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.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)"fixes #123"present in the PR description)Summary by cubic
Adds a
useOneOfPojooption to Java generators to emit real POJO classes for oneOf schemas instead of abstract wrapper models, fixing oneOf inheritance and serialization issues. Fixes #22209. Adds a Jersey3 sample and config to demonstrate the option.New Features
useOneOfPojo(default: false).updateModelForComposedSchema()to skip merging for oneOf and restore original vars; option handled inprocessOpts(); CLI option added; docs updated across Java generators.bin/configs/java-jersey3-oneOf-pojo.yamlandsamples/client/petstore/java/jersey3-oneOf-pojo/to validate output.Migration
--additional-properties useOneOfPojo=true(or set in config). No breaking changes by default.Written for commit 7830618. Summary will update on new commits.