Fix allOf with $ref to discriminator-less oneOf flattening all variant properties#23498
Fix allOf with $ref to discriminator-less oneOf flattening all variant properties#23498jasperpatterson wants to merge 2 commits intoOpenAPITools:masterfrom
allOf with $ref to discriminator-less oneOf flattening all variant properties#23498Conversation
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.
| /// </summary> | ||
| /// <param name="triangle"></param> | ||
| public ShapeOrNull(Triangle triangle) | ||
| internal ShapeOrNull(Triangle triangle) |
There was a problem hiding this comment.
P2: Making the union constructors internal leaves the public ShapeOrNull type without any public constructor, preventing external consumers from creating instances.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/csharp/generichost/net10/NullReferenceTypes/src/Org.OpenAPITools/Model/ShapeOrNull.cs, line 38:
<comment>Making the union constructors internal leaves the public ShapeOrNull type without any public constructor, preventing external consumers from creating instances.</comment>
<file context>
@@ -35,7 +35,7 @@ public partial class ShapeOrNull : IValidatableObject
/// </summary>
/// <param name="triangle"></param>
- public ShapeOrNull(Triangle triangle)
+ internal ShapeOrNull(Triangle triangle)
{
Triangle = triangle;
</file context>
modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java
Show resolved
Hide resolved
| /// </summary> | ||
| /// <param name="triangle"></param> | ||
| public ShapeOrNull(Triangle triangle) | ||
| internal ShapeOrNull(Triangle triangle) |
There was a problem hiding this comment.
P2: Changing the only constructors to internal makes the public ShapeOrNull type non-instantiable for consumers, preventing outbound creation of ShapeOrNull payloads.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/csharp/generichost/net4.8/Petstore/src/Org.OpenAPITools/Model/ShapeOrNull.cs, line 36:
<comment>Changing the only constructors to internal makes the public ShapeOrNull type non-instantiable for consumers, preventing outbound creation of ShapeOrNull payloads.</comment>
<file context>
@@ -33,7 +33,7 @@ public partial class ShapeOrNull : IValidatableObject
/// </summary>
/// <param name="triangle"></param>
- public ShapeOrNull(Triangle triangle)
+ internal ShapeOrNull(Triangle triangle)
{
Triangle = triangle;
</file context>
| internal ShapeOrNull(Triangle triangle) | |
| public ShapeOrNull(Triangle triangle) |
| /// </summary> | ||
| /// <param name="triangle"></param> | ||
| public ShapeOrNull(Triangle triangle) | ||
| internal ShapeOrNull(Triangle triangle) |
There was a problem hiding this comment.
P2: ShapeOrNull is a public model but both constructors were changed to internal, leaving no public instantiation path for SDK consumers.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/csharp/generichost/net8/Petstore/src/Org.OpenAPITools/Model/ShapeOrNull.cs, line 36:
<comment>ShapeOrNull is a public model but both constructors were changed to internal, leaving no public instantiation path for SDK consumers.</comment>
<file context>
@@ -33,7 +33,7 @@ public partial class ShapeOrNull : IValidatableObject
/// </summary>
/// <param name="triangle"></param>
- public ShapeOrNull(Triangle triangle)
+ internal ShapeOrNull(Triangle triangle)
{
Triangle = triangle;
</file context>
| internal ShapeOrNull(Triangle triangle) | |
| public ShapeOrNull(Triangle triangle) |
| /// </summary> | ||
| /// <param name="triangle"></param> | ||
| public ShapeOrNull(Triangle triangle) | ||
| internal ShapeOrNull(Triangle triangle) |
There was a problem hiding this comment.
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.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/csharp/generichost/latest/UseDateTimeOffset/src/Org.OpenAPITools/Model/ShapeOrNull.cs, line 37:
<comment>ShapeOrNull no longer has any public constructor or factory; changing the union constructors to internal makes the public model non-instantiable for SDK consumers.</comment>
<file context>
@@ -34,7 +34,7 @@ public partial class ShapeOrNull : IValidatableObject
/// </summary>
/// <param name="triangle"></param>
- public ShapeOrNull(Triangle triangle)
+ internal ShapeOrNull(Triangle triangle)
{
Triangle = triangle;
</file context>
| /// </summary> | ||
| /// <param name="triangle"></param> | ||
| public Shape(Triangle triangle) | ||
| internal Shape(Triangle triangle) |
There was a problem hiding this comment.
P2: Making the only constructors internal leaves public Shape with no public instantiation path for SDK consumers, creating a breaking API regression.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/csharp/generichost/net8/FormModels/src/Org.OpenAPITools/Model/Shape.cs, line 36:
<comment>Making the only constructors internal leaves public Shape with no public instantiation path for SDK consumers, creating a breaking API regression.</comment>
<file context>
@@ -33,7 +33,7 @@ public partial class Shape : IValidatableObject
/// </summary>
/// <param name="triangle"></param>
- public Shape(Triangle triangle)
+ internal Shape(Triangle triangle)
{
Triangle = triangle;
</file context>
| internal Shape(Triangle triangle) | |
| public Shape(Triangle triangle) |
e2babc3 to
2cf1fdb
Compare
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 properties from all variants into the model. TheoneOfref is now treated as a parent, so only the model’s own properties are generated and union semantics are preserved.ModelUtils.getParentName()/getAllParentsName(), treat a$refto aoneOfschema as a parent even without a discriminator.DefaultCodegen.updateModelForComposedSchema(), when a$refresolves tooneOf, skipaddProperties()to prevent merging variant properties; inheritance/interfaces capture the relationship.Written for commit 2cf1fdb. Summary will update on new commits.