OpenAPITools / OpenAPITools/openapi-generator
[BUG] [TypeScript] - relationship between class hierarchies produces malformed import statement
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
I have following class hierarchy:
classDiagram
Vehicle <|-- Car
Vehicle <|-- Boat
Building <|-- Garage
Building <|-- DrivingSchool
Driver
Driver --* Vehicle
DrivingSchool --* Driver
Garage --* Vehicle
class Garage {
+Vehicle Content
}
class DrivingSchool {
+Driver Content
}
class Driver {
+Vehicle Vehicle
}
and generated code is wrong for Garage.
It generates following imports:
import { exists, mapValues } from '../runtime';
import type { Building } from './Building';
import {
BuildingFromJSON,
BuildingFromJSONTyped,
BuildingToJSON,
} from './Building';
import type { Car | Boat } from './Car | Boat';
import {
Car | BoatFromJSON,
Car | BoatFromJSONTyped,
Car | BoatToJSON,
} from './Car | Boat';
and following interface:
export interface Garage extends Building {
/**
*
* @type {Car | Boat}
* @memberof Garage
*/
content?: Car | Boat | null;
/**
*
* @type {string}
* @memberof Garage
*/
readonly type?: string | null;
}
What is interesting, it also generates following types:
// in file GetVehicles200ResponseInner.ts
export type GetVehicles200ResponseInner = Boat | Car;
// in file DriverVehicle .ts
export type DriverVehicle = Boat | Car;
both of these types are completely the same (except the name).
But for Driver, it correctly uses the DriverVehicle type:
export interface Driver {
/**
*
* @type {DriverVehicle}
* @memberof Driver
*/
vehicle?: DriverVehicle | null;
}
so the bug manifests itself when there is a second inheritance (Garage -> Building)
openapi-generator version
6.3.0
OpenAPI declaration file content or url
https://gist.github.com/klinki/069b02de5c4b3b5713f86f878f95165a
Generation Details
cp swagger.json ${PWD}/src/generated/api/swagger.json
docker run --rm -v "${PWD}/src/generated/api:/local" openapitools/openapi-generator-cli generate -i /local/swagger.json -g typescript-fetch -o /local --additional-properties=typescriptThreePlus=true --additional-properties=supportsES6=true
Related issues/PRs
This bug seems to be similar to https://github.com/OpenAPITools/openapi-generator/issues/12256 but it is slightly different - it is not anyOf but oneOf.
Suggest a fix
I would expect only one union type for Vehicle hierarchy to be generated and used in all places.
It should not generate DriverVehicle and GetVehicles200ResponseInner but only one type, maybe something like VehicleUnion which should be used for both Driver and Garage.
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 reported hierarchy from the linked OpenAPI declaration with the typescript-fetch generator and the command shown in the issue. Compare the generated Garage, Driver, GetVehicles200ResponseInner, and DriverVehicle files and trace how the Vehicle oneOf is named and imported. Done means the generated imports are valid and the shared union type is used consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100