OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript-fetch] Missing XxxFromJSON import when discriminator child is also a property type

Open Beginner friendly
#24,654 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

For typescript-fetch, when a discriminator parent model also has a property (or array property) typed as one of its own discriminator children, the generated parent file imports only XxxFromJSONTyped / XxxToJSON / XxxToJSONTyped for that child, but property deserialization still calls XxxFromJSON(...).

This produces a TypeScript compile error:

error TS2552: Cannot find name 'RemoteInventoryFromJSON'. Did you mean 'RemoteInventoryToJSON'?

Root cause appears to be the combination of:

  1. TypeScriptFetchClientCodegen.postProcessAllModels filtering discriminator mapped models out of normal tsImports (to avoid duplicate imports; introduced for #15637 / #19195).
  2. modelGeneric.mustache emitting a compact discriminator import that omits {{modelName}}FromJSON.
  3. Property mapping still generating {{datatype}}FromJSON(...).

When the child is only used as a discriminator mapping (not also as a property), this is fine. When it is also a property type on the same parent, the missing FromJSON import breaks the build.

openapi-generator version

Confirmed with 7.24.0 (openapi-generator-cli).
The buggy import line is still present on master in modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache.

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Disc FromJSON bug
  version: 1.0.0
paths: {}
components:
  schemas:
    InventoryEntity:
      type: object
      required: [$type]
      properties:
        $type:
          type: string
        remoteInventories:
          type: array
          items:
            $ref: '#/components/schemas/RemoteInventory'
      discriminator:
        propertyName: $type
        mapping:
          RemoteInventory: '#/components/schemas/RemoteInventory'
    RemoteInventory:
      allOf:
        - $ref: '#/components/schemas/InventoryEntity'
        - type: object
          properties:
            externalId:
              type: string
Generation Details
openapi-generator-cli generate \
  -g typescript-fetch \
  -i disc-fromjson-bug.yaml \
  -o /tmp/disc-fromjson-out \
  --additional-properties=supportsES6=true
Steps to reproduce
  1. Save the minimal spec above.
  2. Generate with typescript-fetch as shown.
  3. Open models/InventoryEntity.ts.
  4. Observe that RemoteInventoryFromJSON is used but not imported.
  5. tsc fails with TS2552.
Actual output
import { type RemoteInventory, RemoteInventoryFromJSONTyped, RemoteInventoryToJSON, RemoteInventoryToJSONTyped } from './RemoteInventory';

// ...
'remoteInventories': json['remoteInventories'] == null
  ? undefined
  : ((json['remoteInventories'] as Array<any>).map(RemoteInventoryFromJSON)),
Expected output
import { type RemoteInventory, RemoteInventoryFromJSON, RemoteInventoryFromJSONTyped, RemoteInventoryToJSON, RemoteInventoryToJSONTyped } from './RemoteInventory';
Related issues/PRs
  • #15637 / #19195 — duplicate discriminator imports; mapped models were removed from tsImports, which sets up this failure mode.
  • #21441 / #21477 — missing imports for oneOf fields with discriminator (modelOneOf.mustache). Related symptom, different template/path.
  • #15736 / #19524 — discriminator ToJSON / hierarchy handling. Not this import bug.

This specific case (inheritance discriminator parent + property typed as child → missing FromJSON import in modelGeneric.mustache) does not appear to have an open dedicated issue.

Suggest a fix

In modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache, include {{modelName}}FromJSON in the discriminator mapped-model import:

{{#discriminator}}
{{#discriminator.mappedModels}}
import { type {{modelName}}, {{modelName}}FromJSON, {{modelName}}FromJSONTyped, {{modelName}}ToJSON, {{modelName}}ToJSONTyped } from './{{modelName}}{{importFileExtension}}';
{{/discriminator.mappedModels}}
{{/discriminator}}

Alternative (also valid): emit property deserialization with XxxFromJSONTyped(json, false) instead of XxxFromJSON(json), so it matches the symbols already imported for discriminator children.

The first option is the smallest template change and matches how normal tsImports already import both FromJSON and FromJSONTyped.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache and reproduce the issue using the provided OpenAPI spec and generation command. Inspect the generated models/InventoryEntity.ts, then verify that the child’s FromJSON import is present and that the generated TypeScript compiles without TS2552.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.