OpenAPITools / OpenAPITools/openapi-generator
[BUG] exotic discriminator property name leads to broken typescript-fetch client behaviour
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 use a "exotic" discriminator property name: @type. This leads to polymorphism issues when generating a typescript-fetch client, because the @ is swallowed by something.
openapi-generator version
generator version 6.2.0
OpenAPI declaration file content or url
The openapi spec of the bad example:
https://github.com/helt/openapi-generator-discriminator-quirks/blob/9b8dbebb8168991b855c3c7b864548a9c6142ec3/middleware/openapi-bad.json#L10
Openapi spec of the good example:
https://github.com/helt/openapi-generator-discriminator-quirks/blob/9b8dbebb8168991b855c3c7b864548a9c6142ec3/middleware/openapi-good.json#L10
Generation Details
go into the middleware directory of the repro repo and run
Steps to reproduce
npm ci
npm run generate
look into https://github.com/helt/openapi-generator-discriminator-quirks/blob/master/middleware/modules/typescript-fetch-bad/src/models/Animal.ts which has a portion like this:
For easier repro, I also added the generating spring boot + openapi app.
Related issues/PRs
Suggest a fix
I think the discriminator property has been altered by the code generator.
export function AnimalFromJSONTyped(
json: any,
ignoreDiscriminator: boolean
): Animal {
if (json === undefined || json === null) {
return json;
}
if (!ignoreDiscriminator) {
if (json["fantatsticClass"] === "fish") { // 👈 should read json["@fantatsticClass"]
return FishFromJSONTyped(json, true);
}
if (json["fantatsticClass"] === "mammal") { // 👈 this one too.
return MammalFromJSONTyped(json, true);
}
}
return {
fantatsticClass: json["@fantatsticClass"],
label: !exists(json, "label") ? undefined : json["label"],
};
}
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 linked bad and good OpenAPI examples, run npm ci and npm run generate in the repro's middleware directory, and compare the generated modules/typescript-fetch-bad/src/models/Animal.ts. Trace how the discriminator property is represented in the typescript-fetch generator and verify that the generated discriminator checks preserve the @ name.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100