Skip to content

Fix allOf with $ref to discriminator-less oneOf flattening all variant properties#23498

Open
jasperpatterson wants to merge 2 commits intoOpenAPITools:masterfrom
jasperpatterson:fix-allof-oneof-ref-flattening
Open

Fix allOf with $ref to discriminator-less oneOf flattening all variant properties#23498
jasperpatterson wants to merge 2 commits intoOpenAPITools:masterfrom
jasperpatterson:fix-allof-oneof-ref-flattening

Conversation

@jasperpatterson
Copy link
Copy Markdown

@jasperpatterson jasperpatterson commented Apr 8, 2026

When a schema uses allOf to compose with a $ref to a oneOf schema that has no discriminator, the codegen incorrectly resolves the oneOf by merging properties from all variants into the model's vars. This produces an interface requiring every variant-specific field simultaneously — an impossible conjunction.

ParentC:
  allOf:
    - type: object
      properties:
        type: { type: string }
    - $ref: "#/components/schemas/Child"

Child:
  oneOf:       # no discriminator
    - $ref: "#/components/schemas/ChildX"
    - $ref: "#/components/schemas/ChildY"

Before (broken): All variant properties merged into one flat interface, instanceOf check becomes unsatisfiable.

After (fixed): The oneOf ref is recognized as a parent. Only the model's own properties appear in vars. Generators with inheritance support emit proper type extension/delegation to the parent.

Root cause

Two independent issues compound into the bug:

  1. ModelUtils.getParentName() gates parent recognition on hasOrInheritsDiscriminator(). A oneOf without a discriminator is rejected as a parent candidate, so parentName comes back null.

  2. DefaultCodegen.updateModelForComposedSchema() — when no parent is recognized, the composition path calls addProperties() on the oneOf schema. addProperties() recurses into every oneOf variant, collecting all their properties into a single map. This flattens the disjunction into a conjunction.

Fix

ModelUtils.getParentName() / getAllParentsName() — Added a hasOneOf(s) check so that a $ref to a oneOf schema is treated as a parent candidate even without a discriminator.

DefaultCodegen.updateModelForComposedSchema() — Added a ModelUtils.hasOneOf(refSchema) guard at the top of the property-resolution chain. When a $ref resolves to a oneOf schema, addProperties() is skipped entirely to prevent variant flattening. This fires before the inheritance/composition branches, protecting all generators regardless of supportsInheritance.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    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.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Fixes the bug where allOf with a $ref to a discriminator-less oneOf merged properties from all variants into the model. The oneOf ref is now treated as a parent, so only the model’s own properties are generated and union semantics are preserved.

  • Bug Fixes
    • In ModelUtils.getParentName()/getAllParentsName(), treat a $ref to a oneOf schema as a parent even without a discriminator.
    • In DefaultCodegen.updateModelForComposedSchema(), when a $ref resolves to oneOf, skip addProperties() to prevent merging variant properties; inheritance/interfaces capture the relationship.
    • Added a 3.0 test spec and unit test to verify parent assignment and property isolation; regenerated samples to reflect the corrected model shapes.

Written for commit 2cf1fdb. Summary will update on new commits.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with Cubic

/// </summary>
/// <param name="triangle"></param>
public ShapeOrNull(Triangle triangle)
internal ShapeOrNull(Triangle triangle)
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Suggested change
internal ShapeOrNull(Triangle triangle)
public ShapeOrNull(Triangle triangle)
Fix with Cubic

/// </summary>
/// <param name="triangle"></param>
public ShapeOrNull(Triangle triangle)
internal ShapeOrNull(Triangle triangle)
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Suggested change
internal ShapeOrNull(Triangle triangle)
public ShapeOrNull(Triangle triangle)
Fix with Cubic

/// </summary>
/// <param name="triangle"></param>
public ShapeOrNull(Triangle triangle)
internal ShapeOrNull(Triangle triangle)
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with Cubic

/// </summary>
/// <param name="triangle"></param>
public Shape(Triangle triangle)
internal Shape(Triangle triangle)
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Suggested change
internal Shape(Triangle triangle)
public Shape(Triangle triangle)
Fix with Cubic

@jasperpatterson jasperpatterson force-pushed the fix-allof-oneof-ref-flattening branch from e2babc3 to 2cf1fdb Compare April 8, 2026 22:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant