-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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-fetch] correct oneOf model to or
not concat
when handling enums
#10017
Changes from 10 commits
505ceaa
eee43b4
39b6299
cf1b7b1
59c117e
9df3375
200a6c0
45b537a
3503ef7
b45cf8f
4b40ca2
2f5fb8d
e68fe47
bf89f40
5f90822
27abd20
3fb4b52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
{{>modelEnumInterfaces}} | ||
|
||
export function {{classname}}FromJSON(json: any): {{classname}} { | ||
export function {{classname}}FromJSON(json: any): {{classname}} | null { | ||
if ((json === undefined) || (json === null)) { | ||
return json; | ||
} | ||
if(!Object.values({{classname}}).includes(json as {{classname}})) { | ||
return null; | ||
} | ||
Comment on lines
+4
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these runtime checks allowed? |
||
return {{classname}}FromJSONTyped(json, false); | ||
} | ||
|
||
export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boolean): {{classname}} { | ||
export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boolean): {{classname}} | null { | ||
return json as {{classname}}; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
/** | ||
* Enum test | ||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) | ||
* | ||
* The version of the OpenAPI document: 1.0.0 | ||
* | ||
* | ||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
* https://openapi-generator.tech | ||
* Do not edit the class manually. | ||
*/ | ||
|
||
import { exists, mapValues } from '../runtime'; | ||
import { | ||
StringOneOf, | ||
StringOneOfFromJSON, | ||
StringOneOfFromJSONTyped, | ||
StringOneOfToJSON, | ||
} from './'; | ||
|
||
/** | ||
* | ||
* @export | ||
* @interface ObjectCombinedEnum | ||
*/ | ||
export interface ObjectCombinedEnum { | ||
/** | ||
* | ||
* @type {StringOneOf} | ||
* @memberof ObjectCombinedEnum | ||
*/ | ||
combinedStringEnum?: StringOneOf; | ||
} | ||
|
||
export function ObjectCombinedEnumFromJSON(json: any): ObjectCombinedEnum { | ||
return ObjectCombinedEnumFromJSONTyped(json, false); | ||
} | ||
|
||
export function ObjectCombinedEnumFromJSONTyped(json: any, ignoreDiscriminator: boolean): ObjectCombinedEnum { | ||
if ((json === undefined) || (json === null)) { | ||
return json; | ||
} | ||
return { | ||
|
||
'combinedStringEnum': !exists(json, 'combined-string-enum') ? undefined : StringOneOfFromJSON(json['combined-string-enum']), | ||
}; | ||
} | ||
|
||
export function ObjectCombinedEnumToJSON(value?: ObjectCombinedEnum | null): any { | ||
if (value === undefined) { | ||
return undefined; | ||
} | ||
if (value === null) { | ||
return null; | ||
} | ||
return { | ||
|
||
'combined-string-enum': StringOneOfToJSON(value.combinedStringEnum), | ||
}; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
/** | ||
* Enum test | ||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) | ||
* | ||
* The version of the OpenAPI document: 1.0.0 | ||
* | ||
* | ||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
* https://openapi-generator.tech | ||
* Do not edit the class manually. | ||
*/ | ||
|
||
import { exists, mapValues } from '../runtime'; | ||
import { | ||
StringEnum, | ||
StringEnumFromJSON, | ||
StringEnumFromJSONTyped, | ||
StringEnumToJSON, | ||
StringEnumTwo, | ||
StringEnumTwoFromJSON, | ||
StringEnumTwoFromJSONTyped, | ||
StringEnumTwoToJSON, | ||
} from './'; | ||
|
||
/** | ||
* | ||
* @export | ||
* @interface ObjectOneOfEnum | ||
*/ | ||
export interface ObjectOneOfEnum { | ||
/** | ||
* | ||
* @type {StringEnum | StringEnumTwo} | ||
* @memberof ObjectOneOfEnum | ||
*/ | ||
combinedStringEnum?: StringEnum | StringEnumTwo; | ||
} | ||
|
||
export function ObjectOneOfEnumFromJSON(json: any): ObjectOneOfEnum { | ||
return ObjectOneOfEnumFromJSONTyped(json, false); | ||
} | ||
|
||
export function ObjectOneOfEnumFromJSONTyped(json: any, ignoreDiscriminator: boolean): ObjectOneOfEnum { | ||
if ((json === undefined) || (json === null)) { | ||
return json; | ||
} | ||
return { | ||
|
||
'combinedStringEnum': !exists(json, 'combined-string-enum') ? undefined : StringEnum | StringEnumTwoFromJSON(json['combined-string-enum']), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still incorrectly generated |
||
}; | ||
} | ||
|
||
export function ObjectOneOfEnumToJSON(value?: ObjectOneOfEnum | null): any { | ||
if (value === undefined) { | ||
return undefined; | ||
} | ||
if (value === null) { | ||
return null; | ||
} | ||
return { | ||
|
||
'combined-string-enum': StringEnum | StringEnumTwoToJSON(value.combinedStringEnum), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still incorrectly generated |
||
}; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
/** | ||
* Enum test | ||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) | ||
* | ||
* The version of the OpenAPI document: 1.0.0 | ||
* | ||
* | ||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
* https://openapi-generator.tech | ||
* Do not edit the class manually. | ||
*/ | ||
|
||
/** | ||
* | ||
* @export | ||
* @enum {string} | ||
*/ | ||
export enum StringEnumTwo { | ||
Twoone = 'twoone', | ||
Twotwo = 'twotwo', | ||
Twothree = 'twothree' | ||
} | ||
|
||
export function StringEnumTwoFromJSON(json: any): StringEnumTwo { | ||
return StringEnumTwoFromJSONTyped(json, false); | ||
} | ||
|
||
export function StringEnumTwoFromJSONTyped(json: any, ignoreDiscriminator: boolean): StringEnumTwo { | ||
return json as StringEnumTwo; | ||
} | ||
|
||
export function StringEnumTwoToJSON(value?: StringEnumTwo | null): any { | ||
return value as any; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
/** | ||
* Enum test | ||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) | ||
* | ||
* The version of the OpenAPI document: 1.0.0 | ||
* | ||
* | ||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
* https://openapi-generator.tech | ||
* Do not edit the class manually. | ||
*/ | ||
|
||
import { | ||
StringEnum, | ||
StringEnumTwo, | ||
StringEnumFromJSONTyped, | ||
StringEnumToJSON, | ||
StringEnumTwoFromJSONTyped, | ||
StringEnumTwoToJSON, | ||
} from './'; | ||
|
||
/** | ||
* @type StringOneOf | ||
* | ||
* @export | ||
*/ | ||
export type StringOneOf = StringEnum | StringEnumTwo; | ||
|
||
export function StringOneOfFromJSON(json: any): StringOneOf { | ||
return StringOneOfFromJSONTyped(json, false); | ||
} | ||
|
||
export function StringOneOfFromJSONTyped(json: any, ignoreDiscriminator: boolean): StringOneOf { | ||
if ((json === undefined) || (json === null)) { | ||
return json; | ||
} | ||
return StringEnumFromJSONTyped(json, true) || StringEnumTwoFromJSONTyped(json, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously this came out as a object spread of the wrong type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it valid for an enum to include an empty string ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. however, apart from an empty string, this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I wrongly assumed casting |
||
} | ||
|
||
export function StringOneOfToJSON(value?: StringOneOf | null): any { | ||
if (value === undefined) { | ||
return undefined; | ||
} | ||
if (value === null) { | ||
return null; | ||
} | ||
return StringEnumToJSON(value as StringEnum) || StringEnumTwoToJSON(value as StringEnumTwo); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the second part after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It didn't cross my mind, our docs only use string enums. I test it out tomorrow and see! |
||
} | ||
|
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 gonna be a breaking change. if an enum property is marked as required / non-nullable in the spec, all consumers expect that it's defined.
after this change some apps will likely stop compiling.
why do yo need it anyway? you can keep the original code here - return the enum value as is, without any processing
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.
Typescript gave a me a warning about the implicit return null. I believe it can be removed
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.
that's because this PR changed the code inside the function, narrowing down
json
type tonull
before returning itThere 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.
Playing around with this more... it previously returned a value that was not valid. so if you passed
7
and your enum was[0, 1, 2]
you would get back 7 which is not the right type.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.
well, we trust the api response matches the spec. otherwise the code is gonna break anyway, since we don't do full shape verification
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 challenge is older clients being used with newer backends which just weren't aware of the existence of such values
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 would say,
null
is as unexpected as a bogus enum value (if the property is not marked as nullable).Again, i honestly believe it's way out of scope of the generated api client.