Skip to content

Emit a clear error instead of crashing on duplicate generated schema names#907

Open
adityasingh2400 wants to merge 1 commit into
apple:mainfrom
adityasingh2400:fix-duplicate-schema-name-crash
Open

Emit a clear error instead of crashing on duplicate generated schema names#907
adityasingh2400 wants to merge 1 commit into
apple:mainfrom
adityasingh2400:fix-duplicate-schema-name-crash

Conversation

@adityasingh2400
Copy link
Copy Markdown

When the idiomatic naming strategy maps two distinct OpenAPI schema names to the same generated Swift type name, the generator crashes instead of reporting the problem. This is the crash tracked in #854: a document with both NullTime and nullTime under #/components/schemas produces

Swift/NativeDictionary.swift: Fatal error: Duplicate values for key: 'NullTime'

Root cause is in boxRecursiveTypes, which builds a name-to-node lookup for recursion detection with Dictionary(uniqueKeysWithValues:). That initializer traps on duplicate keys, and the idiomatic strategy can produce two declarations sharing a name even though the OpenAPI names were distinct. The defensive strategy keeps them distinct, which is why the crash only shows up with idiomatic naming.

The fix detects the collision before building the lookup and emits an error diagnostic that names the conflicting type and points at the documented workarounds (switch namingStrategy to defensive, or add a nameOverrides entry). The Configuring-the-generator article already notes that idiomatic naming "might produce name conflicts (in that case, switch back to defensive)", so this turns an undiagnosed trap into the guidance that already exists. The lookup construction now also tolerates duplicate keys, so the error diagnostic is the failure path rather than a fatal trap, independent of which diagnostic collector is in use.

Verification: running the generator with namingStrategy: idiomatic on a spec containing NullTime and nullTime previously crashed; it now exits non-zero with

error: Multiple schemas in '#/components/schemas' map to the same generated Swift type name 'NullTime', which is not supported. ...

I added a regression test in Test_translateSchemas that translates two schemas colliding under the idiomatic strategy and asserts the error is emitted. With the previous code that test aborts with the Duplicate values for key trap (signal 5); with the fix it passes. A second test confirms that distinct idiomatic names emit no error. The full OpenAPIGeneratorCoreTests target (115 tests) and the reference tests (114 tests) pass, so generated output for valid documents is unchanged, including recursive-type boxing.

…names

When the idiomatic naming strategy maps two distinct OpenAPI schema names
(for example NullTime and nullTime) to the same Swift type name, the
generator trapped while boxing recursive types. The boxing code built a
name-to-node lookup with Dictionary(uniqueKeysWithValues:), which calls
fatalError on duplicate keys, surfacing as

    Fatal error: Duplicate values for key: 'NullTime'

with no indication of which document or strategy caused it.

Detect the collision before constructing the lookup and emit an error
diagnostic that names the conflicting type and points at the documented
workarounds (switch namingStrategy to defensive, or add a nameOverrides
entry). The lookup now also tolerates duplicate keys so the diagnostic is
the failure path rather than a trap, regardless of the diagnostic
collector in use.

Adds a regression test that translates two schemas which collide under the
idiomatic strategy and asserts the error is emitted; it crashed before this
change. Also covers that distinct idiomatic names emit no error.
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