Skip to content

fix(core) discriminatorType in oneOf interfaces - #24812

Open
jpfinne wants to merge 44 commits into
OpenAPITools:masterfrom
jpfinne:bugfix/issue_24769
Open

fix(core) discriminatorType in oneOf interfaces#24812
jpfinne wants to merge 44 commits into
OpenAPITools:masterfrom
jpfinne:bugfix/issue_24769

Conversation

@jpfinne

@jpfinne jpfinne commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

fix #24769, fix #19194

Improve resolution of DefaultCodegen.getDiscriminatorType by resolving the best common type in all mapped discriminators.

Matching $ref types are used. Otherwise the types enum, integer, string, object are used.

PR checklist

  • Read the contribution guidelines.
  • 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.
  • 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 #24769 and #19194 by inferring Java oneOf discriminator getter types from all mapped schemas instead of defaulting to Object. Constant string discriminators now generate Enum getters, while Kotlin keeps its existing string behavior.

  • Resolves discriminator properties through $ref and allOf schemas.
  • Preserves shared integer, number, and referenced enum types, with String or Object fallbacks when no common type exists; Java gains an enum mapping to Enum.
  • Adds regression coverage for Java and Spring generators.

Written for commit 97909e8. Summary will update on new commits.

Review in cubic

jpfinne added 30 commits May 11, 2026 14:26
@jpfinne
jpfinne marked this pull request as ready for review August 31, 2026 15:30
@jpfinne jpfinne changed the title [Java] [Spring] issue 24769 Fix discriminatorType in oneOf interfaces Aug 31, 2026

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 issue found across 7 files

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="modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/DiscriminatorUtils.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/DiscriminatorUtils.java:343">
P2: When the discriminator is inherited through `allOf`, this check misses the parent mapping because the child has no direct discriminator. Traverse referenced/allOf parents to locate the inherited discriminator before collecting mapped schemas.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic



public static List<Schema> getMappedSchemas(OpenAPI openAPI, Schema schema) {
if (schema.getDiscriminator() != null && schema.getDiscriminator().getMapping() != null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2: When the discriminator is inherited through allOf, this check misses the parent mapping because the child has no direct discriminator. Traverse referenced/allOf parents to locate the inherited discriminator before collecting mapped schemas.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/DiscriminatorUtils.java, line 343:

<comment>When the discriminator is inherited through `allOf`, this check misses the parent mapping because the child has no direct discriminator. Traverse referenced/allOf parents to locate the inherited discriminator before collecting mapped schemas.</comment>

<file context>
@@ -322,6 +323,60 @@ private static CodegenProperty getDiscriminatorCodegenProperty(OpenAPI openAPI,
+
+
+    public static List<Schema> getMappedSchemas(OpenAPI openAPI, Schema schema) {
+        if (schema.getDiscriminator() != null && schema.getDiscriminator().getMapping() != null) {
+            return schema.getDiscriminator().getMapping().values().stream()
+                    .map(ref -> ModelUtils.getSchema(openAPI, ModelUtils.getSimpleRef(ref)))
</file context>

jpfinne and others added 3 commits August 31, 2026 17:56
…en/utils/DiscriminatorUtils.java

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
…en/DefaultCodegen.java

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
…en/DefaultCodegen.java

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

* @param openAPI
* @param schema The Schema that may contain the discriminator
* @param discPropName The String that is the discriminator propertyName in the schema
* @return "enum", "string" or "object

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The docstring here is not correct anymore it seems since it returns Schema rather than a simple type (which I assume was just String earlier)?

Map<String, Schema> properties = schema.getProperties();
if (properties != null) {
Schema property = properties.get(propertyName);
// Schema property = ModelUtils.getReferencedSchema(openAPI, );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this commented line be removed?

@Mattias-Sehlstedt

Mattias-Sehlstedt commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Could you add #19194 as an issue being fixed as well by this?

Edit: it seem that one needs to have the full syntax to get proper linking for Multiple issues. So fix #24769, fix #19194.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 1 file (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 4 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 4 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 3 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

@jpfinne jpfinne changed the title Fix discriminatorType in oneOf interfaces fix(core) discriminatorType in oneOf interfaces Sep 3, 2026

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 issue found across 2 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="modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/DiscriminatorUtils.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/DiscriminatorUtils.java:336">
P3: The new local variable `properties` is dead code: it is computed but never read anywhere in `getDistinctTypes`. It also forces a second full run of the recursive `findProperty` over every mapped schema (the same work the `return` statement repeats) and ends with a stray double semicolon. Drop the line entirely.</violation>
</file>

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

public static List<Schema> getDistinctTypes(OpenAPI openAPI, Schema schema, String discPropName) {
List<Schema> mappedSchemas = getMappedSchemas(openAPI, schema);

List<Schema> properties = mappedSchemas.stream().map(sc -> findProperty(openAPI, sc, discPropName, new HashSet<>())).collect(Collectors.toList());;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P3: The new local variable properties is dead code: it is computed but never read anywhere in getDistinctTypes. It also forces a second full run of the recursive findProperty over every mapped schema (the same work the return statement repeats) and ends with a stray double semicolon. Drop the line entirely.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/DiscriminatorUtils.java, line 336:

<comment>The new local variable `properties` is dead code: it is computed but never read anywhere in `getDistinctTypes`. It also forces a second full run of the recursive `findProperty` over every mapped schema (the same work the `return` statement repeats) and ends with a stray double semicolon. Drop the line entirely.</comment>

<file context>
@@ -332,6 +332,8 @@ private static CodegenProperty getDiscriminatorCodegenProperty(OpenAPI openAPI,
     public static List<Schema> getDistinctTypes(OpenAPI openAPI, Schema schema, String discPropName) {
         List<Schema> mappedSchemas = getMappedSchemas(openAPI, schema);
+
+        List<Schema> properties = mappedSchemas.stream().map(sc -> findProperty(openAPI, sc, discPropName, new HashSet<>())).collect(Collectors.toList());;
         return mappedSchemas.stream().map(sc -> findProperty(openAPI, sc, discPropName, new HashSet<>()))
                 .filter(Objects::nonNull)
</file context>

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.

[REQ] [Java] Allow excluding discriminator property from generated models [BUG][Spring] code generation fails if oneOf discriminator is enum

2 participants