OpenAPITools / OpenAPITools/openapi-generator
[BUG][typescript-fetch] Bug when generating typescript-fetch with type: [object, null] together with $ref
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
Hello everyone, in our project we need annotate object as nullable. Since we use OpenApi 3.1 where nullable is no more supported. We use this syntax in yaml:
nullableChildObject:
type:
- object
- "null"
$ref: "#/components/schemas/ChildObject"
and we expect to get code in typescript where nullableChildObject will be defined like:
nullableChildObject: ChildObject | null;
but openapi generator generates definition without null like this:
nullableChildObject: ChildObject;
openapi-generator version
7.20.0 and it didn't work before either, it doesn't work with master too.
OpenAPI declaration file content or url
openapi: 3.1.0
info:
title: Demo
description: Just Demo yaml to show issue with type:[object, "null"] and $ref together
version: 0.0.1
contact:
name: Demo
servers:
- url: http://localhost
description: Demo
tags:
- name: object
description: Object operations
paths:
/object:
get:
tags:
- object
operationId: getObjects
description: Get objects
responses:
"200":
description: OK
content:
"*/*":
schema:
type: array
items:
$ref: "#/components/schemas/MainObject"
components:
schemas:
ChildObject:
type: object
properties:
someProperty:
type: string
required:
- someProperty
MainObject:
type: object
properties:
uuid:
type: string
nullableProperty:
type:
- string
- "null"
nullableChildObject:
type:
- object
- "null"
$ref: "#/components/schemas/ChildObject"
required:
- uuid
- nullableProperty
- nullableChildObject
Generation Details
java -jar openapi-generator-cli.jar generate --input-spec="api.yaml" --generator-name="typescript-fetch" --template-dir="openapi/" --output="openapi/api" --type-mappings="Date=string" --additional-properties="prefixParameterInterfaces=true,useSingleRequestParameter=false"
Steps to reproduce
Just generate output using yaml and command provided. Than check generated file in openapi/api/models/MainObject.ts if nullableChildObject is defined as union type of ChildObject |null
Related issues/PRs
#21612
Suggest a fix
I have no clue why this doesn't work but I expect that in 3.0 version of openapi, nullable was supported and this null type is new in openapi 3.1. So maybe it just wasn't implemented yet ?
Thank you in advance for any help.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the supplied openapi-generator command against the YAML reproduction and inspect openapi/api/models/MainObject.ts. Trace how the typescript-fetch generator handles the type array containing object and null alongside $ref, using related issue #21612 for context. Done when nullableChildObject is generated as ChildObject | null without regressing the existing nullableProperty behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100