OpenAPITools / OpenAPITools/openapi-generator
[BUG] typescript-angular discriminator mapping ignored in interfaces
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?
Description
Hi,
It seems that the typescript-angular generator is ignoring the mapping as discriminator value on the children interfaces.
openapi-generator version
<version>5.1.0-SNAPSHOT</version>
Commit: 8cb4741248ecf0baaf3c608952290fb21c231490
Commit Date: lundi 8 mars 2021 18:11:50
source : https://github.com/openapitools/openapi-generator
docs : https://openapi-generator.tech/
OpenAPI declaration file content or url
If we take the following json file (named pets.json)
{
"openapi": "3.0.1",
"info": {
"title": "Accruals",
"version": "accruals"
},
"paths": {},
"components": {
"schemas": {
"Pet": {
"type": "object",
"required": [
"petType"
],
"properties": {
"id": {
"title": "Int32",
"type": "integer",
"format": "int32"
},
"petType": {
"type": "string"
}
},
"discriminator": {
"propertyName": "petType",
"mapping": {
"MapCatType": "#/components/schemas/Cat",
"MapDogType": "#/components/schemas/Dog",
"MapLizardType": "#/components/schemas/Lizard"
}
}
},
"Cat": {
"allOf": [
{
"$ref": "#/components/schemas/Pet"
},
{
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
]
},
"Dog": {
"allOf": [
{
"$ref": "#/components/schemas/Pet"
},
{
"type": "object",
"properties": {
"bark": {
"type": "string"
}
}
}
]
},
"Lizard": {
"x-discriminator-value": "LizardTypeX",
"allOf": [
{
"$ref": "#/components/schemas/Pet"
},
{
"type": "object",
"properties": {
"lovesRocks": {
"type": "boolean"
}
}
}
]
}
}
}
}
Generation Details
It happens with the typescript-angular generator with the additionnal-property taggedUnions=true.
In the output folder we have in the model/pet.ts file (as espected).
import { Lizard } from './lizard';
import { Cat } from './cat';
import { Dog } from './dog';
export type Pet = Lizard | Cat | Dog;
But int the model/cat.ts, we have the petType as Cat instead of MapCatType supposed.
export interface Cat {
id?: number;
petType: 'Cat';
name?: string;
}
instead of
export interface Cat {
id?: number;
petType: 'MapCatType';
name?: string;
}
Notice that in the Lizard interface, petType is setted as LizardTypeX thanks to "x-discriminator-value": "LizardTypeX" (as espected too).
export interface Lizard {
id?: number;
petType: 'LizardTypeX';
lovesRocks?: boolean;
}
Steps to reproduce
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i ./pets.json -g typescript-angular -o ./generated --additional-properties=taggedUnions=true
Suggest a fix
I've forked the repository and created a branch with a fix where I've made (on the TypeScriptAngularClientCodegen.java file).
I override the discriminatorValue property if there is a mapping to each children interface (expect if an x-discriminator-value is already setted).
https://github.com/vergerf/openapi-generator/tree/typescript-angular-discriminator-mapping
EDIT: link to the PR :
https://github.com/OpenAPITools/openapi-generator/pull/8929
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 provided pets.json and run the typescript-angular generation command with taggedUnions=true. Inspect TypeScriptAngularClientCodegen.java and compare the generated Cat interface with the discriminator mapping, while preserving the existing Lizard x-discriminator-value behavior. The linked pull request contains the proposed fix and should be reviewed before starting new work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- angular, java, typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100