OpenAPITools / OpenAPITools/openapi-generator
[BUG] [typescript] `anyOf` properties produce malformed import statements
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 schemas specifying an anyOf property as an input, the generator produces import statements which are not valid typescript. For example if a return value is typed as being any of string or number:
"anyOf": [{ "type": "string" }, { "type": "number" }]
The following import statements are produced:
import { String | number } from '../models/string | number';
openapi-generator version
6.0.0-beta
5.4.0
OpenAPI declaration file content or url
{
"openapi": "3.0.2",
"info": {
"version": "0.0.1",
"title": "anyOf bug reproduction"
},
"paths": {
"/example-object": {
"get": {
"tags": ["object"],
"summary": "Get Example Data (Object)",
"operationId": "getExampleDataDataObject",
"parameters": [],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "anyOf bug reproduction",
"type": "object",
"additionalProperties": {
"anyOf": [{ "type": "string" }, { "type": "number" }]
}
}
}
}
}
}
}
},
"/example-array": {
"get": {
"tags": ["array"],
"summary": "Get Example Data (Array)",
"operationId": "getExampleDataDataArray",
"parameters": [],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "anyOf bug reproduction",
"type": "array",
"items": {
"anyOf": [{ "type": "string" }, { "type": "number" }]
}
}
}
}
}
}
}
}
}
}
Full reproduction here: https://github.com/phlickey/openapigenerator-ts-anyof_bug_reproduction
Generation Details
npx openapi-generator-cli generate -g typescript -o output -i spec.json
Steps to reproduce
Reproduced here: https://github.com/phlickey/openapigenerator-ts-anyof_bug_reproduction
Related issues/PRs
Suggest a fix
When defining an api value as a union of two or more primitive types (ie string | number | null) the import line could be skipped. This presumably gets more difficult if {{ className }} is being passed as a union of generated and primitive types.
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 modules/openapi-generator/src/main/resources/typescript/api/api.mustache at lines 20-22 and the provided minimal OpenAPI specification. Run the npx openapi-generator-cli command from the issue using spec.json, then inspect generated TypeScript imports for anyOf values such as string | number. Done means the generated imports are valid and primitive-only unions do not produce malformed model imports.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100