fix(typescript-fetch): import FromJSON for discriminator mapped models - #24702
fix(typescript-fetch): import FromJSON for discriminator mapped models#24702kdelay wants to merge 1 commit into
Conversation
The discriminator import line in modelGeneric.mustache omitted
{{modelName}}FromJSON. Mapped models are filtered out of tsImports so that
line is their only import, which breaks when the child model is also used as
a property type on the parent: property deserialization emits
XFromJSON(...) and tsc fails with TS2552.
Closes OpenAPITools#24654
There was a problem hiding this comment.
1 issue found across 8 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/resources/typescript-fetch/modelGeneric.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache:16">
P2: For discriminator parents whose mapped children are never referenced as property types (such as Animal.ts, where Cat/Dog only appear as subtypes), the newly added `FromJSON` import is unused. Consumers that compile generated code with `noUnusedLocals` enabled will now get a TS6133 error on the import, where previously `FromJSON` was not imported at all. This widens the pre-existing `ToJSON` unused-import situation to `FromJSON` across such models. Consider importing `FromJSON` only when the mapped model is also used as a property type, or documenting that consumers must not enable `noUnusedLocals`.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| {{#discriminator}} | ||
| {{#discriminator.mappedModels}} | ||
| import { type {{modelName}}, {{modelName}}FromJSONTyped, {{modelName}}ToJSON, {{modelName}}ToJSONTyped } from './{{modelName}}{{importFileExtension}}'; | ||
| import { type {{modelName}}, {{modelName}}FromJSON, {{modelName}}FromJSONTyped, {{modelName}}ToJSON, {{modelName}}ToJSONTyped } from './{{modelName}}{{importFileExtension}}'; |
There was a problem hiding this comment.
P2: For discriminator parents whose mapped children are never referenced as property types (such as Animal.ts, where Cat/Dog only appear as subtypes), the newly added FromJSON import is unused. Consumers that compile generated code with noUnusedLocals enabled will now get a TS6133 error on the import, where previously FromJSON was not imported at all. This widens the pre-existing ToJSON unused-import situation to FromJSON across such models. Consider importing FromJSON only when the mapped model is also used as a property type, or documenting that consumers must not enable noUnusedLocals.
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/resources/typescript-fetch/modelGeneric.mustache, line 16:
<comment>For discriminator parents whose mapped children are never referenced as property types (such as Animal.ts, where Cat/Dog only appear as subtypes), the newly added `FromJSON` import is unused. Consumers that compile generated code with `noUnusedLocals` enabled will now get a TS6133 error on the import, where previously `FromJSON` was not imported at all. This widens the pre-existing `ToJSON` unused-import situation to `FromJSON` across such models. Consider importing `FromJSON` only when the mapped model is also used as a property type, or documenting that consumers must not enable `noUnusedLocals`.</comment>
<file context>
@@ -13,7 +13,7 @@ import {
{{#discriminator}}
{{#discriminator.mappedModels}}
-import { type {{modelName}}, {{modelName}}FromJSONTyped, {{modelName}}ToJSON, {{modelName}}ToJSONTyped } from './{{modelName}}{{importFileExtension}}';
+import { type {{modelName}}, {{modelName}}FromJSON, {{modelName}}FromJSONTyped, {{modelName}}ToJSON, {{modelName}}ToJSONTyped } from './{{modelName}}{{importFileExtension}}';
{{/discriminator.mappedModels}}
{{/discriminator}}
</file context>
Fixes #24654.
Mapped models are removed from
tsImports(#15637 / #19195), so the discriminator import line inmodelGeneric.mustacheis their only import. It omitsXFromJSON, which breaks when the parent also has a property typed as that child: deserialization emitsXFromJSON(...)and tsc fails with TS2552.Adding
{{modelName}}FromJSONkeeps that line's shape; it already importsXToJSON.With the spec from the issue: TS2552 before,
tsc --noEmit --strictclean after. Regenerated typescript-fetch samples../mvnw -B -pl modules/openapi-generator -am test: 4750 tests, 0 failures.TypeScript (Fetch) technical committee: @leonyu
PR checklist
Summary by cubic
Import
FromJSONfor discriminator-mapped models in thetypescript-fetchgenerator to prevent TS2552 compile errors. Previously, discriminator imports omittedXFromJSON; now they include it, matching emittedXFromJSON(...)calls.modelGeneric.mustacheto add{{modelName}}FromJSONto the discriminator import list; regeneratedtypescript-fetchsamples. No runtime impact; codegen-only change.Rollout
typescript-fetchgenerator to pick up the fix. No other migration required.Written for commit c254c23. Summary will update on new commits.