-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
[typescript] fix: enum can not receive stringified class name
#19481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[typescript] fix: enum can not receive stringified class name
#19481
Conversation
| {{/discriminatorValue}} | ||
| {{/allVars}} | ||
| {{#discriminatorName}} | ||
| {{^discriminator.isEnum}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the meaningful change of this PR.
I can see here:
openapi-generator/modules/openapi-generator/src/main/resources/Java/pojo.mustache
Lines 95 to 99 in b4357ed
| {{#discriminator}} | |
| {{#discriminator.isEnum}} | |
| this.{{{discriminatorName}}} = this.getClass().getSimpleName(); | |
| {{/discriminator.isEnum}} | |
| {{/discriminator}} |
that other generators guard the opposite case. If I misunderstand what the fix is, please let me know. An alternative could also be to make the discriminator type always a string/symbol and only assign the stringified value of each enum value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so what would be the discriminator.isEnum case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The discriminator is the type member and type is of type InteractionTypeEnum, thus the template string discriminator.isEnum would evaluate to true, hence with the template negation, it would not emit the guarded class name assignment (e.g. it would omit this.{{discriminatorName}} = "{{classname}}";)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
basically before the change we'd produce code that looks like this:
export class Interaction {
'type': InteractionTypeEnum;
static readonly discriminator: string | undefined = "type";
public constructor() {
this.type = "Interaction";
}
}
export enum InteractionTypeEnum {
Call = 'call',
Email = 'email',
Meeting = 'meeting',
ChatMessage = 'chat-message'
}(note how the string "Interaction" is not a valid value of InteractionTypeEnum)
After the changes in this PR it would be:
export class Interaction {
'type': InteractionTypeEnum;
static readonly discriminator: string | undefined = "type";
public constructor() {
}
}
export enum InteractionTypeEnum {
Call = 'call',
Email = 'email',
Meeting = 'meeting',
ChatMessage = 'chat-message'
}E.g. the meaningful diff is:
export class Interaction {
'type': InteractionTypeEnum;
static readonly discriminator: string | undefined = "type";
public constructor() {
- this.type = "Interaction";
}
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the explanation! I wonder why there even is such a default value assigned, do you happen to know?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not, sorry, I am assuming it is a remnant from making sure each class has a sane default value to discriminate on, however the implementation of it doesn't make sense. It works fine when the discriminator is of string type, which is the majority of implementations I've seen, but it obviously doesn't work any more as soon as it's a fixed enum.
| @@ -0,0 +1,77 @@ | |||
| openapi: 3.0.0 | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is an extract of the OpenAPI spec of https://developer.affinity.co/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for the noise that introducing a new sample fixture produces, I couldn't find a sample OpenAPI spec that shows the issue I am trying to fix. If you know of one that has a discriminator which is an enum, please feel free to point me to it, then I might be able to use it instead and reduce the footprint of this PR a bit.
| public constructor() { | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the meaningful part of the fixture (the omission of this.type = "Interaction")
…me (OpenAPITools#19481)" This reverts commit 4238f17.
* Add oneOf model for Typescript generator * Update import procces: For oneOfs only import $refs within the oneOf * Remove new line after description * Update samples * Typescript: Update model.mustache * Typescript: Remove emun from oneOf models * Update samples * Typescript oneOf: add discriminator and update deserialize procces * Typescript: update tests * Typescript oneOf: move type mapping to models * Typescript: Update samples * Typescript: Update type of mappig * Typescript: Update type of mappig * Typescript oneOf: update deserializing logic * Typescript: Update samples * Revert "[typescript] fix: `enum` can not receive stringified class name (#19481)" This reverts commit 4238f17. * [typescript] chore: update fixtures --------- Co-authored-by: ksvirkou-hubspot <[email protected]>
When a discriminator is a pure enum, currently the


typescriptgenerator assigns a string to it, which is the name of the class (this.discriminator = "<ClassName>").See:
and:
Because the stringified classname and the enum type are usually incompatible (unless there is an enum value which happens to be the class name) a type error is produced.
This pull request wraps this line in an additional guard.
Please note that the fix does not do any introspection on a potential overlap of the classname with valid enum values as any such assignment, whilst syntactically valid, would most likely not make any semantical sense.
PR checklist
master(upcoming 7.6.0 minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)