Skip to content
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

Closed
wants to merge 17 commits into from
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 {
Copy link
Contributor

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

Copy link
Author

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

Copy link
Contributor

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 to null before returning it

Copy link
Author

@prince-chrismc prince-chrismc Aug 13, 2021

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.

Copy link
Contributor

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

Copy link
Author

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

Copy link
Contributor

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.

if ((json === undefined) || (json === null)) {
return json;
}
if(!Object.values({{classname}}).includes(json as {{classname}})) {
return null;
}
Comment on lines +4 to +9
Copy link
Author

Choose a reason for hiding this comment

The 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}};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
}
{{/discriminator}}
{{^discriminator}}
return { {{#oneOf}}...{{{.}}}FromJSONTyped(json, true){{^-last}}, {{/-last}}{{/oneOf}} };
return {{#oneOf}}{{{.}}}FromJSONTyped(json, true){{^-last}} || {{/-last}}{{/oneOf}};
prince-chrismc marked this conversation as resolved.
Show resolved Hide resolved
{{/discriminator}}
}

Expand All @@ -53,6 +53,6 @@ export function {{classname}}ToJSON(value?: {{classname}} | null): any {
}
{{/discriminator}}
{{^discriminator}}
return { {{#oneOf}}...{{{.}}}ToJSON(value){{^-last}}, {{/-last}}{{/oneOf}} };
return {{#oneOf}}{{{.}}}ToJSON(value as {{{.}}}){{^-last}} ?? {{/-last}}{{/oneOf}};
{{/discriminator}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,35 @@ components:
- one
- two
- three
StringEnumTwo:
type: string
enum:
- twoone
- twotwo
- twothree
StringOneOf:
type: string
oneOf:
- $ref: "#/components/schemas/StringEnum"
- $ref: "#/components/schemas/StringEnumTwo"
NumberEnum:
type: number
enum:
- 0
- 1
- 2
- 3
NumberEnumTwo:
type: number
enum:
- 4
- 5
- 6
NumberOneOf:
type: number
oneOf:
- $ref: "#/components/schemas/NumberEnum"
- $ref: "#/components/schemas/NumberEnumTwo"
EnumPatternObject:
type: object
properties:
Expand All @@ -227,3 +250,15 @@ components:
nullable: true
allOf:
- $ref: "#/components/schemas/NumberEnum"
ObjectOneOfEnum:
type: "object"
properties:
combined-string-enum:
oneOf:
- $ref: '#/components/schemas/StringEnum'
- $ref: '#/components/schemas/StringEnumTwo'
ObjectCombinedEnum:
type: "object"
properties:
combined-string-enum:
$ref: '#/components/schemas/StringOneOf'
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface Capitalization {
sCAETHFlowPoints?: string;
/**
* Name of the pet

* @type {string}
* @memberof Capitalization
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ models/EnumPatternObject.ts
models/InlineObject.ts
models/InlineResponse200.ts
models/NumberEnum.ts
models/ObjectCombinedEnum.ts
models/ObjectOneOfEnum.ts
models/StringEnum.ts
models/StringEnumTwo.ts
models/StringOneOf.ts
models/index.ts
runtime.ts
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']),
Copy link
Author

Choose a reason for hiding this comment

The 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),
Copy link
Author

Choose a reason for hiding this comment

The 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);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously this came out as a object spread of the wrong type {...FromJSONTyped(json, true), ...FromJSONTyped(json, true) }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it valid for an enum to include an empty string ("") as a value? If so, we might want to be stricter here and use ?? instead of ||

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

however, apart from an empty string, this || will never work at all. StringEnumFromJSONTyped always returns some value (because a null check is done just before it - line 36), so the second part, StringEnumTwoFromJSONTyped will never be executed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"" are valid in OAS so ?? is required ... it's also a problem for boolean and numbers so good catch.

I wrongly assumed casting json as {{classname}} would return falsy but that's not the case!

}

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the second part after || will never work.
It's not a big deal for string enums, which don't really convert values and only do type assertion. However, for more complex data types it's a potential problem - right?

Copy link
Author

Choose a reason for hiding this comment

The 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!

}

Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ export * from './EnumPatternObject';
export * from './InlineObject';
export * from './InlineResponse200';
export * from './NumberEnum';
export * from './ObjectCombinedEnum';
export * from './ObjectOneOfEnum';
export * from './StringEnum';
export * from './StringEnumTwo';
export * from './StringOneOf';