Skip to content

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

Description

@mega-csgi

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 [BUG][typescript-fetch] duplicate imports for models with a discriminator #15637 / [FIX][typescript-fetch] Fix duplicate imports for models with a discriminator  #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

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions