Skip to content

fix(typescript-fetch): import FromJSON for discriminator mapped models - #24702

Open
kdelay wants to merge 1 commit into
OpenAPITools:masterfrom
kdelay:fix/issue-24654-ts-fetch-discriminator-fromjson-import
Open

fix(typescript-fetch): import FromJSON for discriminator mapped models#24702
kdelay wants to merge 1 commit into
OpenAPITools:masterfrom
kdelay:fix/issue-24654-ts-fetch-discriminator-fromjson-import

Conversation

@kdelay

@kdelay kdelay commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Fixes #24654.

Mapped models are removed from tsImports (#15637 / #19195), so the discriminator import line in modelGeneric.mustache is their only import. It omits XFromJSON, which breaks when the parent also has a property typed as that child: deserialization emits XFromJSON(...) and tsc fails with TS2552.

Adding {{modelName}}FromJSON keeps that line's shape; it already imports XToJSON.

With the spec from the issue: TS2552 before, tsc --noEmit --strict clean after. Regenerated typescript-fetch samples. ./mvnw -B -pl modules/openapi-generator -am test: 4750 tests, 0 failures.

TypeScript (Fetch) technical committee: @leonyu

PR checklist

  • Read the contribution guidelines.
  • Built the project and regenerated the typescript-fetch samples.
  • @mentioned the technical committee above.

Summary by cubic

Import FromJSON for discriminator-mapped models in the typescript-fetch generator to prevent TS2552 compile errors. Previously, discriminator imports omitted XFromJSON; now they include it, matching emitted XFromJSON(...) calls.

  • Updated modelGeneric.mustache to add {{modelName}}FromJSON to the discriminator import list; regenerated typescript-fetch samples. No runtime impact; codegen-only change.

Rollout

  • Regenerate clients with the typescript-fetch generator to pick up the fix. No other migration required.

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

Review in cubic

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

@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 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}}';

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: 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>

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][typescript-fetch] Missing XxxFromJSON import when discriminator child is also a property type

1 participant