Repository navigation
[core] Keep explicit inlineSchemaNameMappings when several inline schemas map to one name - #25181
mohitduhan19 wants to merge 6 commits into
Conversation
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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.
|
Thank you @mohitduhan19 |
… spec Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01H6L1WtRqooHYJHsCTaZE2n
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
There was a problem hiding this comment.
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>
Fixes #25179
Since 7.26.0, when several
inlineSchemaNameMappingsentries 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 becomeMoney1,Money2, ... In 7.25.0 all of them used the mapped model.The cause is the collision re-check added to
InlineModelResolver.addSchemasin #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
testInlineSchemaNameMappingManyToOnetoInlineModelResolverTestwith the spec from the issue (two inline objects that differ only indescription, both mapped toMoney). It fails on master and passes with this change: both properties referenceMoneyand noMoney1is created. All 62InlineModelResolverTesttests pass, and the fullopenapi-generatormodule 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/*.yamluseinlineSchemaNameMappings, so no samples change.cc @wing328
Summary by cubic
Fixes a regression where multiple
inlineSchemaNameMappingsentries mapping to the same name (e.g.,Preview_balance=MoneyandPreview_interest=Money) renamed all but the first toMoney_1,Money_2, etc., instead of keeping the mapped name.Moneyand noMoney_1is created.Written for commit a3bcbe1. Summary will update on new commits.