Skip to content

[core] Keep explicit inlineSchemaNameMappings when several inline schemas map to one name - #25181

Open
mohitduhan19 wants to merge 6 commits into
OpenAPITools:masterfrom
mohitduhan19:fix-inline-mapping-many-to-one
Open

mohitduhan19 wants to merge 6 commits into
OpenAPITools:masterfrom
mohitduhan19:fix-inline-mapping-many-to-one

Conversation

@mohitduhan19

@mohitduhan19 mohitduhan19 commented Oct 8, 2026 •

Copy link
Copy Markdown

Fixes #25179

Since 7.26.0, when several inlineSchemaNameMappings entries map different inline schemas onto the same name (e.g. Preview_balance=Money,Preview_interest=Money), only the first one gets that name and the others become Money1, Money2, ... In 7.25.0 all of them used the mapped model.

The cause is the collision re-check added to InlineModelResolver.addSchemas in #24760. It runs after the mapping lookup, so it also renames a name the user chose explicitly. This change applies the re-check only to generated names. A mapped name is used as is, which restores the 7.25.0 behaviour for explicit mappings and keeps the #24760 protection for generated names during recursive flattening.

Tests: added testInlineSchemaNameMappingManyToOne to InlineModelResolverTest with the spec from the issue (two inline objects that differ only in description, both mapped to Money). It fails on master and passes with this change: both properties reference Money and no Money1 is created. All 62 InlineModelResolverTest tests pass, and the full openapi-generator module test suite passes (5327 tests) apart from one Go test that needs network access to download a Go module.

PR checklist

Read the contribution guidelines. Built the project and checked the sample configs: none of bin/configs/*.yaml use inlineSchemaNameMappings, so no samples change.

cc @wing328


Summary by cubic

Fixes a regression where multiple inlineSchemaNameMappings entries mapping to the same name (e.g., Preview_balance=Money and Preview_interest=Money) renamed all but the first to Money_1, Money_2, etc., instead of keeping the mapped name.

  • The collision re-check now applies only to generated names, not explicit mappings.
  • Restores the 7.25.0 behavior for explicit mappings while retaining collision protection for recursive flattening.
  • Warns when a mapped name already exists in the spec, then replaces the existing schema in place.
  • Adds a test with the issue's spec verifying both properties reference Money and no Money_1 is created.

Written for commit a3bcbe1. Summary will update on new commits.

View guided diff

@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 3 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java:1187">
P2: Mapped names now bypass the `components.schemas.containsKey(name)` check entirely, so a mapped value that collides with an existing spec-defined schema silently overwrites it. `addSchemas` unconditionally calls `openAPI.getComponents().addSchemas(name, schema)` (a map put) afterward, so e.g. `inlineSchemaNameMapping: {Preview_balance: Money}` with a pre-existing `components.schemas.Money` replaces the original `Money` definition and rebinds every existing `$ref: '#/components/schemas/Money'` to the inline schema. Guard the overwrite: only allow the explicit name to win when the existing entry was itself created by an earlier mapping, otherwise fall back to `uniqueName(name)` (or at least emit a warning before overwriting).</violation>
</file>

Reply to a comment to ask cubic a question or push back. It learns from your replies.

View guided diff | Re-trigger cubic

// Recursive flattening can add a nested schema after its parent's name
// was chosen. Re-check here so the parent cannot overwrite that child.
if (openAPI.getComponents().getSchemas().containsKey(name)
} else if (openAPI.getComponents().getSchemas().containsKey(name)

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: Mapped names now bypass the components.schemas.containsKey(name) check entirely, so a mapped value that collides with an existing spec-defined schema silently overwrites it. addSchemas unconditionally calls openAPI.getComponents().addSchemas(name, schema) (a map put) afterward, so e.g. inlineSchemaNameMapping: {Preview_balance: Money} with a pre-existing components.schemas.Money replaces the original Money definition and rebinds every existing $ref: '#/components/schemas/Money' to the inline schema. Guard the overwrite: only allow the explicit name to win when the existing entry was itself created by an earlier mapping, otherwise fall back to uniqueName(name) (or at least emit a warning before overwriting).

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java, line 1187:

<comment>Mapped names now bypass the `components.schemas.containsKey(name)` check entirely, so a mapped value that collides with an existing spec-defined schema silently overwrites it. `addSchemas` unconditionally calls `openAPI.getComponents().addSchemas(name, schema)` (a map put) afterward, so e.g. `inlineSchemaNameMapping: {Preview_balance: Money}` with a pre-existing `components.schemas.Money` replaces the original `Money` definition and rebinds every existing `$ref: '#/components/schemas/Money'` to the inline schema. Guard the overwrite: only allow the explicit name to win when the existing entry was itself created by an earlier mapping, otherwise fall back to `uniqueName(name)` (or at least emit a warning before overwriting).</comment>

<file context>
@@ -1182,13 +1182,12 @@ private void copyVendorExtensions(Schema source, Schema target) {
-        // Recursive flattening can add a nested schema after its parent's name
-        // was chosen. Re-check here so the parent cannot overwrite that child.
-        if (openAPI.getComponents().getSchemas().containsKey(name)
+        } else if (openAPI.getComponents().getSchemas().containsKey(name)
                 || uniqueNames.contains(name)) {
+            // Recursive flattening can add a nested schema after its parent's name
</file context>

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Using the mapped name as is matches 7.25.0, where a mapping onto an existing schema name also replaced it, so I kept that behaviour rather than falling back to uniqueName. In 9867efd the resolver now logs a warning when a mapped name already exists in the spec and wasn't added by an earlier mapping. Several mappings onto the same name (the case from #25179) don't trigger it.

@wing328

wing328 commented Oct 9, 2026

Copy link
Copy Markdown
Member

Thanks for the PR

cc @bold84 (author of #24760)

@bold84

bold84 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Thank you @mohitduhan19

mohitduhan19 and others added 2 commits October 9, 2026 18:29
uniqueName() appends "_N", so Money_1 is the name that guards the regression.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H6L1WtRqooHYJHsCTaZE2n

@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).

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

View guided diff | Re-trigger cubic

Names already in uniqueNames were generated during flattening, so a mapping onto one of them is not replacing a spec-defined schema.

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

This branch has not been deployed

No deployments
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.

[BUG][CORE] 7.26.0: several inlineSchemaNameMappings onto one name generate numbered duplicates (Money, Money1)

3 participants