OpenAPITools / OpenAPITools/openapi-generator
[BUG] [typescript] Invalid typescript generated for inheriting component with union typed children
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 (example)?
- 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
When generating from a schema having a component with allOf and one of its properties is an array of oneOf, the name-resolution for files creates invalid file-names. When removing the allOf of the components schema and adding a definition of the fields, it works as expected.
openapi-generator version
Tested using 6.4.0 and 6.5.0
OpenAPI declaration file content or url
{
"openapi": "3.0.1",
"info": {
"title": "Media Api",
"version": "v1"
},
"paths": {
"/browse": {
"get": {
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/AlbumModel"
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"AlbumModel": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/IAlbumModel"
}
],
"properties": {
"children": {
"type": "array",
"items": {
"oneOf": [
{
"$ref": "#/components/schemas/TrackWithTitleModel"
},
{
"$ref": "#/components/schemas/TrackModel"
}
]
},
"nullable": true
}
},
"additionalProperties": false
},
"IAlbumModel": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int32"
}
},
"additionalProperties": false
},
"IDocumentModel": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int32"
}
},
"additionalProperties": false
},
"TrackWithTitleModel": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/TrackModel"
}
],
"properties": {
"title": {
"type": "string"
}
},
"additionalProperties": false
},
"TrackModel": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/IDocumentModel"
}
],
"additionalProperties": false
}
}
}
}
Generation Details
Generating using node package @openapitools/openapi-generator-cli. The bug seems to be present in at least many of the typescript packages. I've tested rsjs, fetch and node.
The invalid file is models/AlbumModel.ts, which starts with import-statements like:
import { exists, mapValues } from '../runtime';
import type { TrackWithTitleModel | TrackModel } from './TrackWithTitleModel | TrackModel';
import {
TrackWithTitleModel | TrackModelFromJSON,
TrackWithTitleModel | TrackModelFromJSONTyped,
TrackWithTitleModel | TrackModelToJSON,
} from './TrackWithTitleModel | TrackModel';
Steps to reproduce
java -jar "6.5.0.jar" generate --input-spec="./openapi.json" --generator-name="typescript-fetch" --output="./src"
Related issues/PRs
Strongly related to:
Maybe also related to:
- https://github.com/OpenAPITools/openapi-generator/issues/12256
- https://github.com/OpenAPITools/openapi-generator/issues/5202
Suggest a fix
Produce the same output as if I would define the properties of IAlbumModel, of which AlbumModel inherits, inside it.
Here's a sample definition which will produce the expected result of the one above:
{
"openapi": "3.0.1",
"info": {
"title": "Media Api",
"version": "v1"
},
"paths": {
"/browse": {
"get": {
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/AlbumModel"
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"AlbumModel": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"children": {
"type": "array",
"items": {
"oneOf": [
{
"$ref": "#/components/schemas/TrackWithTitleModel"
},
{
"$ref": "#/components/schemas/TrackModel"
}
]
},
"nullable": true
}
},
"additionalProperties": false
},
"IDocumentModel": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int32"
}
},
"additionalProperties": false
},
"TrackWithTitleModel": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/TrackModel"
}
],
"properties": {
"title": {
"type": "string"
}
},
"additionalProperties": false
},
"TrackModel": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/IDocumentModel"
}
],
"additionalProperties": false
}
}
}
}
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
Start with the provided Java generation command and inspect the generated models/AlbumModel.ts output for the invalid union-based import paths and identifiers. Reproduce with the supplied OpenAPI declaration and verify that the TypeScript models generate valid imports and match the expected output when inherited properties are handled.
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
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100