Skip to content

fix(typescript-fetch): filter Null type from oneOf to prevent missing model references#23179

Draft
guizmaii wants to merge 4 commits intoOpenAPITools:masterfrom
guizmaii:fix/typescript-fetch-null-model-reference
Draft

fix(typescript-fetch): filter Null type from oneOf to prevent missing model references#23179
guizmaii wants to merge 4 commits intoOpenAPITools:masterfrom
guizmaii:fix/typescript-fetch-null-model-reference

Conversation

@guizmaii
Copy link
Contributor

@guizmaii guizmaii commented Mar 9, 2026

Problem

In OpenAPI 3.1, oneOf can include type: 'null' to represent nullable unions:

NullableResult:
  oneOf:
    - $ref: '#/components/schemas/FileLocation'
    - $ref: '#/components/schemas/DetailedLocation'
    - type: 'null'

The codegen maps type: 'null' to a model named "Null" and adds it to the oneOf variants list. However, no Null.ts model file is ever generated, causing TypeScript import errors:

error TS2305: Module '"../models/index"' has no exported member 'Null'.
error TS2724: '"../models/index"' has no exported member named 'NullFromJSON'.
error TS2724: '"../models/index"' has no exported member named 'NullToJSON'.

Solution

Filter "Null" from cm.oneOf and cm.oneOfModels during model post-processing in TypeScriptFetchClientCodegen.processCodeGenModel(), and set cm.isNullable = true instead.

This follows the same pattern already used by the C#, Java, and PowerShell generators:

  • CSharpClientCodegen.java:1670
  • JavaClientCodegen.java:1207-1210
  • PowerShellClientCodegen.java:1098-1101

Test plan

  • Added testNullableOneOfDoesNotImportNullModel test with a new spec (nullable-oneOf.yaml) that uses type: 'null' in a oneOf
  • Verifies no Null imports/references in the generated model file
  • Verifies no Null.ts model file is generated
  • Verifies the union type only contains valid model types
  • All 23 TypeScriptFetchClientCodegenTest tests pass

Summary by cubic

Fixes typescript-fetch codegen to handle OpenAPI 3.1 type: 'null' correctly. oneOf unions now use | null, and operations that returned Null are generated as void, removing broken imports.

  • Bug Fixes
    • Filter "Null" from cm.oneOf/oneOfModels; set cm.isNullable = true and update modelOneOfInterfaces.mustache to append | null.
    • Convert operations with returnType === 'Null' to void and remove Null from operation and file-level imports.
    • Add tests: nullable-oneOf.yaml to verify unions include only real models plus | null with no Null.ts, and null-response.yaml to ensure null responses become void.

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

… model references

In OpenAPI 3.1, oneOf can include type: 'null' to represent nullable
unions. The codegen maps this to a "Null" model name and adds it to the
oneOf variants list and imports. However, no Null.ts model file is
generated, causing TypeScript import errors like:
  Module '"../models/index"' has no exported member 'Null'

Fix: remove "Null" from cm.oneOf and filter it from oneOfModels during
model post-processing, following the same pattern used by the C#, Java,
and PowerShell generators. The model is marked as nullable instead.
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

guizmaii added 2 commits March 9, 2026 15:50
When a oneOf includes type: 'null', the Null model reference is
removed to prevent broken imports. The union type should include
| null to preserve the nullable semantics from the original spec.
When an API endpoint returns type: 'null' (OpenAPI 3.1), the
codegen maps it to a Null model that doesn't exist as a generated
file. Convert these operations to void return type and remove the
Null import from both operation-level and operations-level imports.
@guizmaii guizmaii marked this pull request as draft March 9, 2026 05:20
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file (changes from recent commits).

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/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java:721">
P2: Unconditional `"Null"` return-type rewrite can erase legitimate user-defined `Null` response models and remove needed imports.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Verify that OpenAPI 3.1 responses with `type: 'null'` are converted
to void return types instead of generating a broken Null model import.
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