Fix allOf with $ref to discriminator-less oneOf flattening all variant properties#23498
Closed
jasperpatterson wants to merge 4 commits into
Closed
Fix allOf with $ref to discriminator-less oneOf flattening all variant properties#23498jasperpatterson wants to merge 4 commits into
allOf with $ref to discriminator-less oneOf flattening all variant properties#23498jasperpatterson wants to merge 4 commits into
Conversation
Contributor
There was a problem hiding this comment.
6 issues found across 178 files
Note: This PR contains a large number of files. cubic only reviews up to 75 files per PR, so some files may not have been reviewed. cubic prioritises the most important files to review.
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/csharp/generichost/net10/NullReferenceTypes/src/Org.OpenAPITools/Model/ShapeOrNull.cs">
<violation number="1" location="samples/client/petstore/csharp/generichost/net10/NullReferenceTypes/src/Org.OpenAPITools/Model/ShapeOrNull.cs:38">
P2: Making the union constructors internal leaves the public ShapeOrNull type without any public constructor, preventing external consumers from creating instances.</violation>
</file>
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java:2805">
P2: Skipping addProperties for any refSchema with oneOf drops wrapper-level properties/required fields, not just variant properties, so shared required fields on a oneOf wrapper will never be added to allProperties/allRequired and disappear from generated models.</violation>
</file>
<file name="samples/client/petstore/csharp/generichost/net4.8/Petstore/src/Org.OpenAPITools/Model/ShapeOrNull.cs">
<violation number="1" location="samples/client/petstore/csharp/generichost/net4.8/Petstore/src/Org.OpenAPITools/Model/ShapeOrNull.cs:36">
P2: Changing the only constructors to internal makes the public ShapeOrNull type non-instantiable for consumers, preventing outbound creation of ShapeOrNull payloads.</violation>
</file>
<file name="samples/client/petstore/csharp/generichost/net8/Petstore/src/Org.OpenAPITools/Model/ShapeOrNull.cs">
<violation number="1" location="samples/client/petstore/csharp/generichost/net8/Petstore/src/Org.OpenAPITools/Model/ShapeOrNull.cs:36">
P2: ShapeOrNull is a public model but both constructors were changed to internal, leaving no public instantiation path for SDK consumers.</violation>
</file>
<file name="samples/client/petstore/csharp/generichost/latest/UseDateTimeOffset/src/Org.OpenAPITools/Model/ShapeOrNull.cs">
<violation number="1" location="samples/client/petstore/csharp/generichost/latest/UseDateTimeOffset/src/Org.OpenAPITools/Model/ShapeOrNull.cs:37">
P2: ShapeOrNull no longer has any public constructor or factory; changing the union constructors to internal makes the public model non-instantiable for SDK consumers.</violation>
</file>
<file name="samples/client/petstore/csharp/generichost/net8/FormModels/src/Org.OpenAPITools/Model/Shape.cs">
<violation number="1" location="samples/client/petstore/csharp/generichost/net8/FormModels/src/Org.OpenAPITools/Model/Shape.cs:36">
P2: Making the only constructors internal leaves public Shape with no public instantiation path for SDK consumers, creating a breaking API regression.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
e2babc3 to
2cf1fdb
Compare
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.
When a schema uses
allOfto compose with a$refto aoneOfschema that has no discriminator, the codegen incorrectly resolves theoneOfby merging properties from all variants into the model'svars. This produces an interface requiring every variant-specific field simultaneously — an impossible conjunction.Before (broken): All variant properties merged into one flat interface,
instanceOfcheck becomes unsatisfiable.After (fixed): The
oneOfref is recognized as a parent. Only the model's own properties appear invars. Generators with inheritance support emit proper type extension/delegation to the parent.Root cause
Two independent issues compound into the bug:
ModelUtils.getParentName()gates parent recognition onhasOrInheritsDiscriminator(). AoneOfwithout a discriminator is rejected as a parent candidate, soparentNamecomes backnull.DefaultCodegen.updateModelForComposedSchema()— when no parent is recognized, the composition path callsaddProperties()on theoneOfschema.addProperties()recurses into everyoneOfvariant, collecting all their properties into a single map. This flattens the disjunction into a conjunction.Fix
ModelUtils.getParentName()/getAllParentsName()— Added ahasOneOf(s)check so that a$refto aoneOfschema is treated as a parent candidate even without a discriminator.DefaultCodegen.updateModelForComposedSchema()— Added aModelUtils.hasOneOf(refSchema)guard at the top of the property-resolution chain. When a$refresolves to aoneOfschema,addProperties()is skipped entirely to prevent variant flattening. This fires before the inheritance/composition branches, protecting all generators regardless ofsupportsInheritance.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
Fixes the bug where
allOfwith a$refto a discriminator-lessoneOfmerged all variant properties into the model. TheoneOfref is now treated as a parent and union semantics are preserved; wrapper-level properties are inherited correctly.ModelUtils.getParentName()/getAllParentsName(), treat a$refto aoneOfschema as a parent even without a discriminator.DefaultCodegen.updateModelForComposedSchema(), pre-seedvisitedwithoneOfvariants beforeaddProperties()to suppress flattening variant fields while still inheriting the wrapper’s own properties and requireds; works with or without inheritance.oneOf/anyOfwrappers as mutable and keep union constructors public inAbstractCSharpCodegen; regenerated samples.Written for commit 398e391. Summary will update on new commits.