OpenAPITools / OpenAPITools/openapi-generator
[BUG][typescript-angular] `anyOf` generates empty `interface` model
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
anyOf schema definitions generates an empty interface model.
{
"components": {
"schemas": {
"foo": {
"type": "object",
"properties": {
"bar": {
"anyOf": [{ "type": "number" }, { "type": "string" }] // <------- this
}
}
}
}
}
}
Generates this model
export interface FooBar { // <--- this doesn't represent type `number | string`
}
import { FooBar } from './fooBar';
export interface Foo {
bar?: FooBar;
}
openapi-generator version
This is a regression starting from v6.0.0, it worked in v5.4.0
OpenAPI declaration file content or url
https://github.com/snebjorn/openapi-gen-bug/blob/master/anyof-bug.json
{
"openapi": "3.0.0",
"info": {
"title": "",
"version": ""
},
"paths": {
"/Foos": {
"get": {
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/foo"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"foo": {
"type": "object",
"properties": {
"bar": {
"anyOf": [{ "type": "number" }, { "type": "string" }],
"description": "This is the description for bar"
}
}
}
}
}
}
Generation Details
Generated using only default values, see https://github.com/snebjorn/openapi-gen-bug/blob/59eb4ba55e91f8444add450016fbd5666c04e2ee/openapitools.json#L25-L29
Steps to reproduce
- Clone https://github.com/snebjorn/openapi-gen-bug
- Run yarn install
- Run yarn gen
- Observe empty interface in https://github.com/snebjorn/openapi-gen-bug/blob/master/output-6.1.0/anyof-bug/model/fooBar.ts
Related issues/PRs
Couldn't find any related to empty interfaces but a lot of issues popped up regarding anyOf, allOf and oneOf
---- UPDATED ----
This issue was just reported:
- #13538
Suggest a fix
Not exactly sure why bar?: number | string | null; was changed to bar?: FooBar;. Interfaces can't extend primitive types.
// An interface cannot extend a primitive type like 'string'; an interface can only extend named types and classes ts(2840)
interface FooBar extends string, number {}
It could be turned into a type
export type FooBar = string | number;
However there is also the issue of the description. The description is moved to the FooBar interface/type, but the description is describing the property bar which now looks like this
export interface Foo {
bar?: FooBar; // <--- note the missing description. The description is on `FooBar` which I believe is incorrect.
}
It should look like this
export interface Foo {
/**
* This is the description for bar
*/
bar?: FooBar;
}
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 anyof-bug.json and reproduce the generation using yarn install followed by yarn gen, then inspect output-6.1.0/anyof-bug/model/fooBar.ts and the generated Foo model. Compare the result with output-5.4.0 and confirm that anyOf produces a number|string type while retaining the bar property description on Foo.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- angular, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100