OpenAPITools / OpenAPITools/openapi-generator
[BUG] [typescript-fetch] oneOf with primitive type gives compilation errors
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
a oneOf schema that has a primitive type, such as string, as one of the options generates invalid typescript code that doesn't compile, (but can be fixed with a couple small manual tweaks)
(I think this also applies to anyOf)
openapi-generator version
typescript-fetch, using openapi-generator version 6.2.1
OpenAPI declaration file content or url
{
"components": {
"schemas": {
"StringSchema": {
"description": "meep ent",
"type": "string"
},
"FooEntity": {
"oneOf": [
{
"$ref": "#/components/schemas/ObjectA"
},
{
"$ref": "#/components/schemas/StringSchema"
}
]
}
}
}
}
this generates code that looks like:
import {
ObjectA,
instanceOfObjectA,
ObjectAFromJSON,
ObjectAFromJSONTyped,
ObjectAToJSON,
} from './ObjectA';
import {
string,
instanceOfstring,
stringFromJSON,
stringFromJSONTyped,
stringToJSON,
} from './string'; // <-- THIS IS INVALID
/**
* @type FooEntity
*
* @export
*/
export type FooEntity = ObjectA | string;
Also, the JSON-serialization function that gets generated (FooEntityToJSON) is invalid too.
Steps to reproduce
generate a typescript client using from the given example schemas above ☝️
Related issues/PRs
Suggest a fix
- primitive types in oneOf shouldn't have
imports generated for them. - the serialization function should use
typeof(...) === "string"instead ofinstanceOfstring - the serialization
ifchecks for primtives should be moved to be before theifs of objects
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 by reproducing the issue with the provided OpenAPI schemas using the typescript-fetch generator, then inspect the generated FooEntity imports and FooEntityToJSON function. Done means the generated TypeScript compiles, primitive oneOf handling avoids invalid imports and instanceOfstring checks, and the serialization branches work for both primitive and object options.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100