From 505ceaa7fedc9e67b1f956845f6dcbf32a4ed204 Mon Sep 17 00:00:00 2001 From: Chris Mc Date: Thu, 22 Jul 2021 14:05:13 -0400 Subject: [PATCH 01/17] [typescript-fetch] correct one of model This was report in 2019 https://github.com/OpenAPITools/openapi-generator/pull/4387#issuecomment-557541270 shortly after the original PR the author noted it was not tested --- .../src/main/resources/typescript-fetch/modelOneOf.mustache | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache index 12d60088b42c..0e306cb50ac8 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache @@ -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}}; {{/discriminator}} } @@ -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}} } From eee43b414c47c8b1f2623f30b1d4564d1a1b29b5 Mon Sep 17 00:00:00 2001 From: cmcarthu Date: Thu, 22 Jul 2021 15:09:28 -0400 Subject: [PATCH 02/17] following build steps --- .../builds/default-v3.0/models/Capitalization.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/Capitalization.ts b/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/Capitalization.ts index 72152fe04620..dbb703aad5ee 100644 --- a/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/Capitalization.ts +++ b/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/Capitalization.ts @@ -51,6 +51,7 @@ export interface Capitalization { sCAETHFlowPoints?: string; /** * Name of the pet + * @type {string} * @memberof Capitalization */ From 39b62992a5ecd15941b09ec75ec09f7ee2599718 Mon Sep 17 00:00:00 2001 From: cmcarthu Date: Thu, 22 Jul 2021 15:17:35 -0400 Subject: [PATCH 03/17] following build steps --- .../builds/enum/.openapi-generator/FILES | 2 + .../builds/enum/models/StringEnumTwo.ts | 37 ++++++++++++++ .../builds/enum/models/StringOneOf.ts | 51 +++++++++++++++++++ .../builds/enum/models/index.ts | 2 + 4 files changed, 92 insertions(+) create mode 100644 samples/client/petstore/typescript-fetch/builds/enum/models/StringEnumTwo.ts create mode 100644 samples/client/petstore/typescript-fetch/builds/enum/models/StringOneOf.ts diff --git a/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES b/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES index 336206328ced..bb742781acea 100644 --- a/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES +++ b/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES @@ -6,5 +6,7 @@ models/InlineObject.ts models/InlineResponse200.ts models/NumberEnum.ts models/StringEnum.ts +models/StringEnumTwo.ts +models/StringOneOf.ts models/index.ts runtime.ts diff --git a/samples/client/petstore/typescript-fetch/builds/enum/models/StringEnumTwo.ts b/samples/client/petstore/typescript-fetch/builds/enum/models/StringEnumTwo.ts new file mode 100644 index 000000000000..e75c1aac1431 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/StringEnumTwo.ts @@ -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; +} + diff --git a/samples/client/petstore/typescript-fetch/builds/enum/models/StringOneOf.ts b/samples/client/petstore/typescript-fetch/builds/enum/models/StringOneOf.ts new file mode 100644 index 000000000000..7b077dea45c4 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/StringOneOf.ts @@ -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); +} + +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) }; +} + diff --git a/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts b/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts index 9772c0c85221..95d94c4fe9a8 100644 --- a/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts @@ -3,3 +3,5 @@ export * from './InlineObject'; export * from './InlineResponse200'; export * from './NumberEnum'; export * from './StringEnum'; +export * from './StringEnumTwo'; +export * from './StringOneOf'; From cf1b7b17334c8d60c62ffb23c88e4ec8b1cebde2 Mon Sep 17 00:00:00 2001 From: Chris Mc Date: Thu, 22 Jul 2021 15:19:04 -0400 Subject: [PATCH 04/17] Update modelOneOf.mustache --- .../src/main/resources/typescript-fetch/modelOneOf.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache index 0e306cb50ac8..cc9a6549a867 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache @@ -53,6 +53,6 @@ export function {{classname}}ToJSON(value?: {{classname}} | null): any { } {{/discriminator}} {{^discriminator}} - return {{#oneOf}}{{{.}}}ToJSON(value as {{{.}}}){{^-last}} || {{/-last}}{{/oneOf}} }; + return {{#oneOf}}{{{.}}}ToJSON(value as {{{.}}}){{^-last}} || {{/-last}}{{/oneOf}}; {{/discriminator}} } From 59c117ea7a89cd75e031c060f86d4dc8d4ff427a Mon Sep 17 00:00:00 2001 From: cmcarthu Date: Thu, 22 Jul 2021 15:24:38 -0400 Subject: [PATCH 05/17] following build steps --- .../petstore/typescript-fetch/builds/enum/models/StringOneOf.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/client/petstore/typescript-fetch/builds/enum/models/StringOneOf.ts b/samples/client/petstore/typescript-fetch/builds/enum/models/StringOneOf.ts index 7b077dea45c4..bfb45e5c59ca 100644 --- a/samples/client/petstore/typescript-fetch/builds/enum/models/StringOneOf.ts +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/StringOneOf.ts @@ -46,6 +46,6 @@ export function StringOneOfToJSON(value?: StringOneOf | null): any { if (value === null) { return null; } - return StringEnumToJSON(value as StringEnum) || StringEnumTwoToJSON(value as StringEnumTwo) }; + return StringEnumToJSON(value as StringEnum) || StringEnumTwoToJSON(value as StringEnumTwo); } From 9df3375127d422fe2c07dd06900f7672f5e1d133 Mon Sep 17 00:00:00 2001 From: cmcarthu Date: Thu, 22 Jul 2021 15:55:16 -0400 Subject: [PATCH 06/17] missing file --- .../src/test/resources/3_0/typescript-fetch/enum.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml index 006b12050dd6..1d6482301192 100644 --- a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml @@ -206,6 +206,17 @@ 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: From 200a6c029108cdaa36b70a6d6e1674df28d22587 Mon Sep 17 00:00:00 2001 From: cmcarthu Date: Thu, 22 Jul 2021 16:03:03 -0400 Subject: [PATCH 07/17] exmaple with bad generation --- .../modelOneOfInterfaces.mustache | 2 +- .../resources/3_0/typescript-fetch/enum.yaml | 7 ++ .../builds/enum/.openapi-generator/FILES | 1 + .../builds/enum/models/ObjectOneOfEnum.ts | 68 +++++++++++++++++++ .../builds/enum/models/index.ts | 1 + 5 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 samples/client/petstore/typescript-fetch/builds/enum/models/ObjectOneOfEnum.ts diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOfInterfaces.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOfInterfaces.mustache index ee4d1fccd8eb..8700004e6cff 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOfInterfaces.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOfInterfaces.mustache @@ -3,4 +3,4 @@ * {{#lambda.indented_star_1}}{{{unescapedDescription}}}{{/lambda.indented_star_1}} * @export */ -export type {{classname}} = {{#discriminator}}{{#mappedModels}}{ {{discriminator.propertyName}}: '{{mappingName}}' } & {{modelName}}{{^-last}} | {{/-last}}{{/mappedModels}}{{/discriminator}}{{^discriminator}}{{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}}{{/discriminator}}; \ No newline at end of file +export type {{classname}} = {{#discriminator}}{{#mappedModels}}{ {{discriminator.propertyName}}: '{{mappingName}}' } & {{.}}{{^-last}} | {{/-last}}{{/mappedModels}}{{/discriminator}}{{^discriminator}}{{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}}{{/discriminator}}; \ No newline at end of file diff --git a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml index 1d6482301192..dc023524f663 100644 --- a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml @@ -238,3 +238,10 @@ components: nullable: true allOf: - $ref: "#/components/schemas/NumberEnum" + ObjectOneOfEnum: + type: "object" + properties: + combined-string-enum: + oneOf: + - $ref: '#/components/schemas/StringEnum' + - $ref: '#/components/schemas/StringEnumTwo' diff --git a/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES b/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES index bb742781acea..22261a554aa8 100644 --- a/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES +++ b/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES @@ -5,6 +5,7 @@ models/EnumPatternObject.ts models/InlineObject.ts models/InlineResponse200.ts models/NumberEnum.ts +models/ObjectOneOfEnum.ts models/StringEnum.ts models/StringEnumTwo.ts models/StringOneOf.ts diff --git a/samples/client/petstore/typescript-fetch/builds/enum/models/ObjectOneOfEnum.ts b/samples/client/petstore/typescript-fetch/builds/enum/models/ObjectOneOfEnum.ts new file mode 100644 index 000000000000..68ef36059f9d --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/ObjectOneOfEnum.ts @@ -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']), + }; +} + +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), + }; +} + + diff --git a/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts b/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts index 95d94c4fe9a8..7434b56ee50f 100644 --- a/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts @@ -2,6 +2,7 @@ export * from './EnumPatternObject'; export * from './InlineObject'; export * from './InlineResponse200'; export * from './NumberEnum'; +export * from './ObjectOneOfEnum'; export * from './StringEnum'; export * from './StringEnumTwo'; export * from './StringOneOf'; From 45b537afac83aa3e307b291324cfaf5a5d18e84a Mon Sep 17 00:00:00 2001 From: cmcarthu Date: Thu, 22 Jul 2021 16:23:51 -0400 Subject: [PATCH 08/17] revert bad change --- .../resources/typescript-fetch/modelOneOfInterfaces.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOfInterfaces.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOfInterfaces.mustache index 8700004e6cff..ee4d1fccd8eb 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOfInterfaces.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOfInterfaces.mustache @@ -3,4 +3,4 @@ * {{#lambda.indented_star_1}}{{{unescapedDescription}}}{{/lambda.indented_star_1}} * @export */ -export type {{classname}} = {{#discriminator}}{{#mappedModels}}{ {{discriminator.propertyName}}: '{{mappingName}}' } & {{.}}{{^-last}} | {{/-last}}{{/mappedModels}}{{/discriminator}}{{^discriminator}}{{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}}{{/discriminator}}; \ No newline at end of file +export type {{classname}} = {{#discriminator}}{{#mappedModels}}{ {{discriminator.propertyName}}: '{{mappingName}}' } & {{modelName}}{{^-last}} | {{/-last}}{{/mappedModels}}{{/discriminator}}{{^discriminator}}{{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}}{{/discriminator}}; \ No newline at end of file From 3503ef76603dcb08eb933da91c654c03b2b3ced5 Mon Sep 17 00:00:00 2001 From: cmcarthu Date: Thu, 22 Jul 2021 16:42:53 -0400 Subject: [PATCH 09/17] proposed work around --- .../resources/3_0/typescript-fetch/enum.yaml | 5 ++ .../builds/enum/.openapi-generator/FILES | 1 + .../builds/enum/models/ObjectCombinedEnum.ts | 64 +++++++++++++++++++ .../builds/enum/models/index.ts | 1 + 4 files changed, 71 insertions(+) create mode 100644 samples/client/petstore/typescript-fetch/builds/enum/models/ObjectCombinedEnum.ts diff --git a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml index dc023524f663..15034b0ab42c 100644 --- a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml @@ -245,3 +245,8 @@ components: oneOf: - $ref: '#/components/schemas/StringEnum' - $ref: '#/components/schemas/StringEnumTwo' + ObjectCombinedEnum: + type: "object" + properties: + combined-string-enum: + $ref: '#/components/schemas/StringOneOf' diff --git a/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES b/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES index 22261a554aa8..36b7b6e29f8c 100644 --- a/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES +++ b/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES @@ -5,6 +5,7 @@ models/EnumPatternObject.ts models/InlineObject.ts models/InlineResponse200.ts models/NumberEnum.ts +models/ObjectCombinedEnum.ts models/ObjectOneOfEnum.ts models/StringEnum.ts models/StringEnumTwo.ts diff --git a/samples/client/petstore/typescript-fetch/builds/enum/models/ObjectCombinedEnum.ts b/samples/client/petstore/typescript-fetch/builds/enum/models/ObjectCombinedEnum.ts new file mode 100644 index 000000000000..a84abb0e775a --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/ObjectCombinedEnum.ts @@ -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), + }; +} + + diff --git a/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts b/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts index 7434b56ee50f..eb958d257a85 100644 --- a/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts @@ -2,6 +2,7 @@ export * from './EnumPatternObject'; export * from './InlineObject'; export * from './InlineResponse200'; export * from './NumberEnum'; +export * from './ObjectCombinedEnum'; export * from './ObjectOneOfEnum'; export * from './StringEnum'; export * from './StringEnumTwo'; From b45cf8fd7b8adcf974e034ceca45742318fa9176 Mon Sep 17 00:00:00 2001 From: cmcarthu Date: Thu, 29 Jul 2021 19:56:38 -0400 Subject: [PATCH 10/17] make sure one of works with falsy --- .../resources/typescript-fetch/modelEnum.mustache | 10 ++++++++-- .../resources/typescript-fetch/modelOneOf.mustache | 2 +- .../test/resources/3_0/typescript-fetch/enum.yaml | 12 ++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/modelEnum.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/modelEnum.mustache index 55a39a4ff3de..c911a6d17181 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/modelEnum.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/modelEnum.mustache @@ -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; + } 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}}; } diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache index cc9a6549a867..6fd04bcca74c 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache @@ -53,6 +53,6 @@ export function {{classname}}ToJSON(value?: {{classname}} | null): any { } {{/discriminator}} {{^discriminator}} - return {{#oneOf}}{{{.}}}ToJSON(value as {{{.}}}){{^-last}} || {{/-last}}{{/oneOf}}; + return {{#oneOf}}{{{.}}}ToJSON(value as {{{.}}}){{^-last}} ?? {{/-last}}{{/oneOf}}; {{/discriminator}} } diff --git a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml index 15034b0ab42c..8382bac36ead 100644 --- a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml @@ -220,9 +220,21 @@ components: 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: From 4b40ca203d3d47b2814559ccab76bcda6a5b73bf Mon Sep 17 00:00:00 2001 From: cmcarthu Date: Thu, 29 Jul 2021 19:57:03 -0400 Subject: [PATCH 11/17] test object enum --- .../test/resources/3_0/typescript-fetch/enum.yaml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml index 8382bac36ead..80a76f0e7e55 100644 --- a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml @@ -235,6 +235,21 @@ components: oneOf: - $ref: "#/components/schemas/NumberEnum" - $ref: "#/components/schemas/NumberEnumTwo" + ObjectEnum: + type: object + enum: + - { value: 1, type: "foo" } + - { value: 2, type: "bar" } + ObjectEnumReverse: + type: object + enum: + - { value: 1, type: "bar" } + - { value: 2, type: "for" } + ObjectOneOf: + type: object + oneOf: + - $ref: "#/components/schemas/ObjectEnum" + - $ref: "#/components/schemas/ObjectEnumReverse" EnumPatternObject: type: object properties: From 2f5fb8d92f9dc4196feb69dd8458bc703a1bfb11 Mon Sep 17 00:00:00 2001 From: cmcarthu Date: Thu, 29 Jul 2021 19:57:12 -0400 Subject: [PATCH 12/17] Revert "test object enum" This reverts commit 4b40ca203d3d47b2814559ccab76bcda6a5b73bf. --- .../test/resources/3_0/typescript-fetch/enum.yaml | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml index 80a76f0e7e55..8382bac36ead 100644 --- a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/enum.yaml @@ -235,21 +235,6 @@ components: oneOf: - $ref: "#/components/schemas/NumberEnum" - $ref: "#/components/schemas/NumberEnumTwo" - ObjectEnum: - type: object - enum: - - { value: 1, type: "foo" } - - { value: 2, type: "bar" } - ObjectEnumReverse: - type: object - enum: - - { value: 1, type: "bar" } - - { value: 2, type: "for" } - ObjectOneOf: - type: object - oneOf: - - $ref: "#/components/schemas/ObjectEnum" - - $ref: "#/components/schemas/ObjectEnumReverse" EnumPatternObject: type: object properties: From e68fe47e873e1a5f7e8f5bb3bbce35dbd1f764dd Mon Sep 17 00:00:00 2001 From: cmcarthu Date: Thu, 29 Jul 2021 20:03:42 -0400 Subject: [PATCH 13/17] following build steps --- .../builds/default-v3.0/models/EnumClass.ts | 10 +++- .../builds/default-v3.0/models/OuterEnum.ts | 10 +++- .../models/OuterEnumDefaultValue.ts | 10 +++- .../default-v3.0/models/OuterEnumInteger.ts | 10 +++- .../models/OuterEnumIntegerDefaultValue.ts | 10 +++- .../builds/enum/.openapi-generator/FILES | 2 + .../builds/enum/models/NumberEnum.ts | 11 +++- .../builds/enum/models/NumberEnumTwo.ts | 43 ++++++++++++++++ .../builds/enum/models/NumberOneOf.ts | 51 +++++++++++++++++++ .../builds/enum/models/StringEnum.ts | 10 +++- .../builds/enum/models/StringEnumTwo.ts | 10 +++- .../builds/enum/models/StringOneOf.ts | 2 +- .../builds/enum/models/index.ts | 2 + .../src/models/BehaviorType.ts | 10 +++- .../src/models/DeploymentRequestStatus.ts | 10 +++- .../sagas-and-records/src/models/ErrorCode.ts | 10 +++- .../src/models/PetPartType.ts | 10 +++- .../src/models/WarningCode.ts | 10 +++- 18 files changed, 204 insertions(+), 27 deletions(-) create mode 100644 samples/client/petstore/typescript-fetch/builds/enum/models/NumberEnumTwo.ts create mode 100644 samples/client/petstore/typescript-fetch/builds/enum/models/NumberOneOf.ts diff --git a/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/EnumClass.ts b/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/EnumClass.ts index a9543c7e8ec3..7c5a79b03c4c 100644 --- a/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/EnumClass.ts +++ b/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/EnumClass.ts @@ -23,11 +23,17 @@ export enum EnumClass { Xyz = '(xyz)' } -export function EnumClassFromJSON(json: any): EnumClass { +export function EnumClassFromJSON(json: any): EnumClass | null { + if ((json === undefined) || (json === null)) { + return json; + } + if(!Object.values(EnumClass).includes(json as EnumClass)) { + return null; + } return EnumClassFromJSONTyped(json, false); } -export function EnumClassFromJSONTyped(json: any, ignoreDiscriminator: boolean): EnumClass { +export function EnumClassFromJSONTyped(json: any, ignoreDiscriminator: boolean): EnumClass | null { return json as EnumClass; } diff --git a/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnum.ts b/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnum.ts index 8ecc66517bc8..2c8622eec165 100644 --- a/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnum.ts +++ b/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnum.ts @@ -23,11 +23,17 @@ export enum OuterEnum { Delivered = 'delivered' } -export function OuterEnumFromJSON(json: any): OuterEnum { +export function OuterEnumFromJSON(json: any): OuterEnum | null { + if ((json === undefined) || (json === null)) { + return json; + } + if(!Object.values(OuterEnum).includes(json as OuterEnum)) { + return null; + } return OuterEnumFromJSONTyped(json, false); } -export function OuterEnumFromJSONTyped(json: any, ignoreDiscriminator: boolean): OuterEnum { +export function OuterEnumFromJSONTyped(json: any, ignoreDiscriminator: boolean): OuterEnum | null { return json as OuterEnum; } diff --git a/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnumDefaultValue.ts b/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnumDefaultValue.ts index abf59c068668..8f25d5fd94dc 100644 --- a/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnumDefaultValue.ts +++ b/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnumDefaultValue.ts @@ -23,11 +23,17 @@ export enum OuterEnumDefaultValue { Delivered = 'delivered' } -export function OuterEnumDefaultValueFromJSON(json: any): OuterEnumDefaultValue { +export function OuterEnumDefaultValueFromJSON(json: any): OuterEnumDefaultValue | null { + if ((json === undefined) || (json === null)) { + return json; + } + if(!Object.values(OuterEnumDefaultValue).includes(json as OuterEnumDefaultValue)) { + return null; + } return OuterEnumDefaultValueFromJSONTyped(json, false); } -export function OuterEnumDefaultValueFromJSONTyped(json: any, ignoreDiscriminator: boolean): OuterEnumDefaultValue { +export function OuterEnumDefaultValueFromJSONTyped(json: any, ignoreDiscriminator: boolean): OuterEnumDefaultValue | null { return json as OuterEnumDefaultValue; } diff --git a/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnumInteger.ts b/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnumInteger.ts index c19c51a8d423..f92d0a99ce99 100644 --- a/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnumInteger.ts +++ b/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnumInteger.ts @@ -23,11 +23,17 @@ export enum OuterEnumInteger { NUMBER_2 = 2 } -export function OuterEnumIntegerFromJSON(json: any): OuterEnumInteger { +export function OuterEnumIntegerFromJSON(json: any): OuterEnumInteger | null { + if ((json === undefined) || (json === null)) { + return json; + } + if(!Object.values(OuterEnumInteger).includes(json as OuterEnumInteger)) { + return null; + } return OuterEnumIntegerFromJSONTyped(json, false); } -export function OuterEnumIntegerFromJSONTyped(json: any, ignoreDiscriminator: boolean): OuterEnumInteger { +export function OuterEnumIntegerFromJSONTyped(json: any, ignoreDiscriminator: boolean): OuterEnumInteger | null { return json as OuterEnumInteger; } diff --git a/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnumIntegerDefaultValue.ts b/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnumIntegerDefaultValue.ts index 91ea94c993a3..f886f13f75b9 100644 --- a/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnumIntegerDefaultValue.ts +++ b/samples/client/petstore/typescript-fetch/builds/default-v3.0/models/OuterEnumIntegerDefaultValue.ts @@ -23,11 +23,17 @@ export enum OuterEnumIntegerDefaultValue { NUMBER_2 = 2 } -export function OuterEnumIntegerDefaultValueFromJSON(json: any): OuterEnumIntegerDefaultValue { +export function OuterEnumIntegerDefaultValueFromJSON(json: any): OuterEnumIntegerDefaultValue | null { + if ((json === undefined) || (json === null)) { + return json; + } + if(!Object.values(OuterEnumIntegerDefaultValue).includes(json as OuterEnumIntegerDefaultValue)) { + return null; + } return OuterEnumIntegerDefaultValueFromJSONTyped(json, false); } -export function OuterEnumIntegerDefaultValueFromJSONTyped(json: any, ignoreDiscriminator: boolean): OuterEnumIntegerDefaultValue { +export function OuterEnumIntegerDefaultValueFromJSONTyped(json: any, ignoreDiscriminator: boolean): OuterEnumIntegerDefaultValue | null { return json as OuterEnumIntegerDefaultValue; } diff --git a/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES b/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES index 36b7b6e29f8c..20fad62015d3 100644 --- a/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES +++ b/samples/client/petstore/typescript-fetch/builds/enum/.openapi-generator/FILES @@ -5,6 +5,8 @@ models/EnumPatternObject.ts models/InlineObject.ts models/InlineResponse200.ts models/NumberEnum.ts +models/NumberEnumTwo.ts +models/NumberOneOf.ts models/ObjectCombinedEnum.ts models/ObjectOneOfEnum.ts models/StringEnum.ts diff --git a/samples/client/petstore/typescript-fetch/builds/enum/models/NumberEnum.ts b/samples/client/petstore/typescript-fetch/builds/enum/models/NumberEnum.ts index 2ef3b74c7f45..71273e53846f 100644 --- a/samples/client/petstore/typescript-fetch/builds/enum/models/NumberEnum.ts +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/NumberEnum.ts @@ -18,16 +18,23 @@ * @enum {string} */ export enum NumberEnum { + NUMBER_0 = 0, NUMBER_1 = 1, NUMBER_2 = 2, NUMBER_3 = 3 } -export function NumberEnumFromJSON(json: any): NumberEnum { +export function NumberEnumFromJSON(json: any): NumberEnum | null { + if ((json === undefined) || (json === null)) { + return json; + } + if(!Object.values(NumberEnum).includes(json as NumberEnum)) { + return null; + } return NumberEnumFromJSONTyped(json, false); } -export function NumberEnumFromJSONTyped(json: any, ignoreDiscriminator: boolean): NumberEnum { +export function NumberEnumFromJSONTyped(json: any, ignoreDiscriminator: boolean): NumberEnum | null { return json as NumberEnum; } diff --git a/samples/client/petstore/typescript-fetch/builds/enum/models/NumberEnumTwo.ts b/samples/client/petstore/typescript-fetch/builds/enum/models/NumberEnumTwo.ts new file mode 100644 index 000000000000..9091ab32978a --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/NumberEnumTwo.ts @@ -0,0 +1,43 @@ +/* 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 NumberEnumTwo { + NUMBER_4 = 4, + NUMBER_5 = 5, + NUMBER_6 = 6 +} + +export function NumberEnumTwoFromJSON(json: any): NumberEnumTwo | null { + if ((json === undefined) || (json === null)) { + return json; + } + if(!Object.values(NumberEnumTwo).includes(json as NumberEnumTwo)) { + return null; + } + return NumberEnumTwoFromJSONTyped(json, false); +} + +export function NumberEnumTwoFromJSONTyped(json: any, ignoreDiscriminator: boolean): NumberEnumTwo | null { + return json as NumberEnumTwo; +} + +export function NumberEnumTwoToJSON(value?: NumberEnumTwo | null): any { + return value as any; +} + diff --git a/samples/client/petstore/typescript-fetch/builds/enum/models/NumberOneOf.ts b/samples/client/petstore/typescript-fetch/builds/enum/models/NumberOneOf.ts new file mode 100644 index 000000000000..9e575b37cb6e --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/NumberOneOf.ts @@ -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 { + NumberEnum, + NumberEnumTwo, + NumberEnumFromJSONTyped, + NumberEnumToJSON, + NumberEnumTwoFromJSONTyped, + NumberEnumTwoToJSON, +} from './'; + +/** + * @type NumberOneOf + * + * @export + */ +export type NumberOneOf = NumberEnum | NumberEnumTwo; + +export function NumberOneOfFromJSON(json: any): NumberOneOf { + return NumberOneOfFromJSONTyped(json, false); +} + +export function NumberOneOfFromJSONTyped(json: any, ignoreDiscriminator: boolean): NumberOneOf { + if ((json === undefined) || (json === null)) { + return json; + } + return NumberEnumFromJSONTyped(json, true) || NumberEnumTwoFromJSONTyped(json, true); +} + +export function NumberOneOfToJSON(value?: NumberOneOf | null): any { + if (value === undefined) { + return undefined; + } + if (value === null) { + return null; + } + return NumberEnumToJSON(value as NumberEnum) ?? NumberEnumTwoToJSON(value as NumberEnumTwo); +} + diff --git a/samples/client/petstore/typescript-fetch/builds/enum/models/StringEnum.ts b/samples/client/petstore/typescript-fetch/builds/enum/models/StringEnum.ts index 8c984c44ba86..81223a199470 100644 --- a/samples/client/petstore/typescript-fetch/builds/enum/models/StringEnum.ts +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/StringEnum.ts @@ -23,11 +23,17 @@ export enum StringEnum { Three = 'three' } -export function StringEnumFromJSON(json: any): StringEnum { +export function StringEnumFromJSON(json: any): StringEnum | null { + if ((json === undefined) || (json === null)) { + return json; + } + if(!Object.values(StringEnum).includes(json as StringEnum)) { + return null; + } return StringEnumFromJSONTyped(json, false); } -export function StringEnumFromJSONTyped(json: any, ignoreDiscriminator: boolean): StringEnum { +export function StringEnumFromJSONTyped(json: any, ignoreDiscriminator: boolean): StringEnum | null { return json as StringEnum; } diff --git a/samples/client/petstore/typescript-fetch/builds/enum/models/StringEnumTwo.ts b/samples/client/petstore/typescript-fetch/builds/enum/models/StringEnumTwo.ts index e75c1aac1431..d8e7638a2bc0 100644 --- a/samples/client/petstore/typescript-fetch/builds/enum/models/StringEnumTwo.ts +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/StringEnumTwo.ts @@ -23,11 +23,17 @@ export enum StringEnumTwo { Twothree = 'twothree' } -export function StringEnumTwoFromJSON(json: any): StringEnumTwo { +export function StringEnumTwoFromJSON(json: any): StringEnumTwo | null { + if ((json === undefined) || (json === null)) { + return json; + } + if(!Object.values(StringEnumTwo).includes(json as StringEnumTwo)) { + return null; + } return StringEnumTwoFromJSONTyped(json, false); } -export function StringEnumTwoFromJSONTyped(json: any, ignoreDiscriminator: boolean): StringEnumTwo { +export function StringEnumTwoFromJSONTyped(json: any, ignoreDiscriminator: boolean): StringEnumTwo | null { return json as StringEnumTwo; } diff --git a/samples/client/petstore/typescript-fetch/builds/enum/models/StringOneOf.ts b/samples/client/petstore/typescript-fetch/builds/enum/models/StringOneOf.ts index bfb45e5c59ca..1dcbc960dafd 100644 --- a/samples/client/petstore/typescript-fetch/builds/enum/models/StringOneOf.ts +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/StringOneOf.ts @@ -46,6 +46,6 @@ export function StringOneOfToJSON(value?: StringOneOf | null): any { if (value === null) { return null; } - return StringEnumToJSON(value as StringEnum) || StringEnumTwoToJSON(value as StringEnumTwo); + return StringEnumToJSON(value as StringEnum) ?? StringEnumTwoToJSON(value as StringEnumTwo); } diff --git a/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts b/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts index eb958d257a85..228ed989b131 100644 --- a/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/index.ts @@ -2,6 +2,8 @@ export * from './EnumPatternObject'; export * from './InlineObject'; export * from './InlineResponse200'; export * from './NumberEnum'; +export * from './NumberEnumTwo'; +export * from './NumberOneOf'; export * from './ObjectCombinedEnum'; export * from './ObjectOneOfEnum'; export * from './StringEnum'; diff --git a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/BehaviorType.ts b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/BehaviorType.ts index f10324a47750..e1a9d621442e 100644 --- a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/BehaviorType.ts +++ b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/BehaviorType.ts @@ -23,11 +23,17 @@ export enum BehaviorType { Overt = 'Overt' } -export function BehaviorTypeFromJSON(json: any): BehaviorType { +export function BehaviorTypeFromJSON(json: any): BehaviorType | null { + if ((json === undefined) || (json === null)) { + return json; + } + if(!Object.values(BehaviorType).includes(json as BehaviorType)) { + return null; + } return BehaviorTypeFromJSONTyped(json, false); } -export function BehaviorTypeFromJSONTyped(json: any, ignoreDiscriminator: boolean): BehaviorType { +export function BehaviorTypeFromJSONTyped(json: any, ignoreDiscriminator: boolean): BehaviorType | null { return json as BehaviorType; } diff --git a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/DeploymentRequestStatus.ts b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/DeploymentRequestStatus.ts index 0a24b4b493f3..8c7083acde76 100644 --- a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/DeploymentRequestStatus.ts +++ b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/DeploymentRequestStatus.ts @@ -32,11 +32,17 @@ export enum DeploymentRequestStatus { Paired = 'Paired' } -export function DeploymentRequestStatusFromJSON(json: any): DeploymentRequestStatus { +export function DeploymentRequestStatusFromJSON(json: any): DeploymentRequestStatus | null { + if ((json === undefined) || (json === null)) { + return json; + } + if(!Object.values(DeploymentRequestStatus).includes(json as DeploymentRequestStatus)) { + return null; + } return DeploymentRequestStatusFromJSONTyped(json, false); } -export function DeploymentRequestStatusFromJSONTyped(json: any, ignoreDiscriminator: boolean): DeploymentRequestStatus { +export function DeploymentRequestStatusFromJSONTyped(json: any, ignoreDiscriminator: boolean): DeploymentRequestStatus | null { return json as DeploymentRequestStatus; } diff --git a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/ErrorCode.ts b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/ErrorCode.ts index 101539269e4c..c3fa330fd1c8 100644 --- a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/ErrorCode.ts +++ b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/ErrorCode.ts @@ -38,11 +38,17 @@ export enum ErrorCode { MaximumVolumeBlocksMusicVolumeIncrease = 'Maximum_Volume_Blocks_Music_Volume_Increase' } -export function ErrorCodeFromJSON(json: any): ErrorCode { +export function ErrorCodeFromJSON(json: any): ErrorCode | null { + if ((json === undefined) || (json === null)) { + return json; + } + if(!Object.values(ErrorCode).includes(json as ErrorCode)) { + return null; + } return ErrorCodeFromJSONTyped(json, false); } -export function ErrorCodeFromJSONTyped(json: any, ignoreDiscriminator: boolean): ErrorCode { +export function ErrorCodeFromJSONTyped(json: any, ignoreDiscriminator: boolean): ErrorCode | null { return json as ErrorCode; } diff --git a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/PetPartType.ts b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/PetPartType.ts index 4b7c05fbd4b9..25b31d524d64 100644 --- a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/PetPartType.ts +++ b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/PetPartType.ts @@ -23,11 +23,17 @@ export enum PetPartType { Long = 'Long' } -export function PetPartTypeFromJSON(json: any): PetPartType { +export function PetPartTypeFromJSON(json: any): PetPartType | null { + if ((json === undefined) || (json === null)) { + return json; + } + if(!Object.values(PetPartType).includes(json as PetPartType)) { + return null; + } return PetPartTypeFromJSONTyped(json, false); } -export function PetPartTypeFromJSONTyped(json: any, ignoreDiscriminator: boolean): PetPartType { +export function PetPartTypeFromJSONTyped(json: any, ignoreDiscriminator: boolean): PetPartType | null { return json as PetPartType; } diff --git a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/WarningCode.ts b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/WarningCode.ts index f0b6dfe654a0..d226f7ce8a2a 100644 --- a/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/WarningCode.ts +++ b/samples/client/petstore/typescript-fetch/builds/sagas-and-records/src/models/WarningCode.ts @@ -23,11 +23,17 @@ export enum WarningCode { NoVolumeRangeSpecified = 'No_Volume_Range_Specified' } -export function WarningCodeFromJSON(json: any): WarningCode { +export function WarningCodeFromJSON(json: any): WarningCode | null { + if ((json === undefined) || (json === null)) { + return json; + } + if(!Object.values(WarningCode).includes(json as WarningCode)) { + return null; + } return WarningCodeFromJSONTyped(json, false); } -export function WarningCodeFromJSONTyped(json: any, ignoreDiscriminator: boolean): WarningCode { +export function WarningCodeFromJSONTyped(json: any, ignoreDiscriminator: boolean): WarningCode | null { return json as WarningCode; } From bf89f40263c6a57a76697c551d2c30c651812f7a Mon Sep 17 00:00:00 2001 From: cmcarthu Date: Thu, 5 Aug 2021 20:19:17 -0400 Subject: [PATCH 14/17] following build steps --- bin/configs/typescript-fetch-one-of.yaml | 3 + .../3_0/typescript-fetch/one-of.yaml | 55 +++ .../builds/one-of/.openapi-generator/FILES | 9 + .../builds/one-of/.openapi-generator/VERSION | 1 + .../builds/one-of/apis/DefaultApi.ts | 83 +++++ .../builds/one-of/apis/index.ts | 1 + .../typescript-fetch/builds/one-of/index.ts | 3 + .../one-of/models/ObjectCombinedOneOf.ts | 68 ++++ .../builds/one-of/models/PatternObject.ts | 73 ++++ .../builds/one-of/models/PatternObjectTwo.ts | 57 ++++ .../builds/one-of/models/index.ts | 3 + .../typescript-fetch/builds/one-of/runtime.ts | 319 ++++++++++++++++++ 12 files changed, 675 insertions(+) create mode 100644 bin/configs/typescript-fetch-one-of.yaml create mode 100644 modules/openapi-generator/src/test/resources/3_0/typescript-fetch/one-of.yaml create mode 100644 samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator/FILES create mode 100644 samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator/VERSION create mode 100644 samples/client/petstore/typescript-fetch/builds/one-of/apis/DefaultApi.ts create mode 100644 samples/client/petstore/typescript-fetch/builds/one-of/apis/index.ts create mode 100644 samples/client/petstore/typescript-fetch/builds/one-of/index.ts create mode 100644 samples/client/petstore/typescript-fetch/builds/one-of/models/ObjectCombinedOneOf.ts create mode 100644 samples/client/petstore/typescript-fetch/builds/one-of/models/PatternObject.ts create mode 100644 samples/client/petstore/typescript-fetch/builds/one-of/models/PatternObjectTwo.ts create mode 100644 samples/client/petstore/typescript-fetch/builds/one-of/models/index.ts create mode 100644 samples/client/petstore/typescript-fetch/builds/one-of/runtime.ts diff --git a/bin/configs/typescript-fetch-one-of.yaml b/bin/configs/typescript-fetch-one-of.yaml new file mode 100644 index 000000000000..076de141a17d --- /dev/null +++ b/bin/configs/typescript-fetch-one-of.yaml @@ -0,0 +1,3 @@ +generatorName: typescript-fetch +outputDir: samples/client/petstore/typescript-fetch/builds/one-of +inputSpec: modules/openapi-generator/src/test/resources/3_0/typescript-fetch/one-of.yaml diff --git a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/one-of.yaml b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/one-of.yaml new file mode 100644 index 000000000000..8b74069eb9cf --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/one-of.yaml @@ -0,0 +1,55 @@ +openapi: 3.0.0 +info: + version: 1.0.0 + title: Enum test +servers: + - url: http://localhost:3000 +paths: + /fake/enum-request-ref: + get: + operationId: fake-enum-request-get-ref + responses: + 200: + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/PatternObject' + post: + operationId: fake-enum-request-post-ref + responses: + 200: + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/PatternObject' + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/PatternObject" +components: + schemas: + PatternObject: + type: object + properties: + string: + type: string + nullable-string: + nullable: true + type: string + number-enum: + type: number + PatternObjectTwo: + type: object + properties: + boolean: + type: boolean + ObjectCombinedOneOf: + type: "object" + properties: + combined-string-enum: + oneOf: + - $ref: '#/components/schemas/PatternObject' + - $ref: '#/components/schemas/PatternObjectTwo' diff --git a/samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator/FILES b/samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator/FILES new file mode 100644 index 000000000000..49af40e12985 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator/FILES @@ -0,0 +1,9 @@ +.openapi-generator-ignore +apis/DefaultApi.ts +apis/index.ts +index.ts +models/ObjectCombinedOneOf.ts +models/PatternObject.ts +models/PatternObjectTwo.ts +models/index.ts +runtime.ts diff --git a/samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator/VERSION b/samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator/VERSION new file mode 100644 index 000000000000..862529f8cacd --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator/VERSION @@ -0,0 +1 @@ +5.2.1-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore/typescript-fetch/builds/one-of/apis/DefaultApi.ts b/samples/client/petstore/typescript-fetch/builds/one-of/apis/DefaultApi.ts new file mode 100644 index 000000000000..7e7bb84ac00c --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/one-of/apis/DefaultApi.ts @@ -0,0 +1,83 @@ +/* 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 * as runtime from '../runtime'; +import { + PatternObject, + PatternObjectFromJSON, + PatternObjectToJSON, +} from '../models'; + +export interface FakeEnumRequestPostRefRequest { + patternObject?: PatternObject; +} + +/** + * + */ +export class DefaultApi extends runtime.BaseAPI { + + /** + */ + async fakeEnumRequestGetRefRaw(): Promise> { + const queryParameters: any = {}; + + const headerParameters: runtime.HTTPHeaders = {}; + + const response = await this.request({ + path: `/fake/enum-request-ref`, + method: 'GET', + headers: headerParameters, + query: queryParameters, + }); + + return new runtime.JSONApiResponse(response, (jsonValue) => PatternObjectFromJSON(jsonValue)); + } + + /** + */ + async fakeEnumRequestGetRef(): Promise { + const response = await this.fakeEnumRequestGetRefRaw(); + return await response.value(); + } + + /** + */ + async fakeEnumRequestPostRefRaw(requestParameters: FakeEnumRequestPostRefRequest): Promise> { + const queryParameters: any = {}; + + const headerParameters: runtime.HTTPHeaders = {}; + + headerParameters['Content-Type'] = 'application/json'; + + const response = await this.request({ + path: `/fake/enum-request-ref`, + method: 'POST', + headers: headerParameters, + query: queryParameters, + body: PatternObjectToJSON(requestParameters.patternObject), + }); + + return new runtime.JSONApiResponse(response, (jsonValue) => PatternObjectFromJSON(jsonValue)); + } + + /** + */ + async fakeEnumRequestPostRef(requestParameters: FakeEnumRequestPostRefRequest): Promise { + const response = await this.fakeEnumRequestPostRefRaw(requestParameters); + return await response.value(); + } + +} diff --git a/samples/client/petstore/typescript-fetch/builds/one-of/apis/index.ts b/samples/client/petstore/typescript-fetch/builds/one-of/apis/index.ts new file mode 100644 index 000000000000..a1aa4698ff25 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/one-of/apis/index.ts @@ -0,0 +1 @@ +export * from './DefaultApi'; diff --git a/samples/client/petstore/typescript-fetch/builds/one-of/index.ts b/samples/client/petstore/typescript-fetch/builds/one-of/index.ts new file mode 100644 index 000000000000..848ecfa4d100 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/one-of/index.ts @@ -0,0 +1,3 @@ +export * from './runtime'; +export * from './apis'; +export * from './models'; diff --git a/samples/client/petstore/typescript-fetch/builds/one-of/models/ObjectCombinedOneOf.ts b/samples/client/petstore/typescript-fetch/builds/one-of/models/ObjectCombinedOneOf.ts new file mode 100644 index 000000000000..7d9bed75e7a2 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/one-of/models/ObjectCombinedOneOf.ts @@ -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 { + PatternObject, + PatternObjectFromJSON, + PatternObjectFromJSONTyped, + PatternObjectToJSON, + PatternObjectTwo, + PatternObjectTwoFromJSON, + PatternObjectTwoFromJSONTyped, + PatternObjectTwoToJSON, +} from './'; + +/** + * + * @export + * @interface ObjectCombinedOneOf + */ +export interface ObjectCombinedOneOf { + /** + * + * @type {PatternObject | PatternObjectTwo} + * @memberof ObjectCombinedOneOf + */ + combinedStringEnum?: PatternObject | PatternObjectTwo; +} + +export function ObjectCombinedOneOfFromJSON(json: any): ObjectCombinedOneOf { + return ObjectCombinedOneOfFromJSONTyped(json, false); +} + +export function ObjectCombinedOneOfFromJSONTyped(json: any, ignoreDiscriminator: boolean): ObjectCombinedOneOf { + if ((json === undefined) || (json === null)) { + return json; + } + return { + + 'combinedStringEnum': !exists(json, 'combined-string-enum') ? undefined : PatternObject | PatternObjectTwoFromJSON(json['combined-string-enum']), + }; +} + +export function ObjectCombinedOneOfToJSON(value?: ObjectCombinedOneOf | null): any { + if (value === undefined) { + return undefined; + } + if (value === null) { + return null; + } + return { + + 'combined-string-enum': PatternObject | PatternObjectTwoToJSON(value.combinedStringEnum), + }; +} + + diff --git a/samples/client/petstore/typescript-fetch/builds/one-of/models/PatternObject.ts b/samples/client/petstore/typescript-fetch/builds/one-of/models/PatternObject.ts new file mode 100644 index 000000000000..deeb4212fbcc --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/one-of/models/PatternObject.ts @@ -0,0 +1,73 @@ +/* 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'; +/** + * + * @export + * @interface PatternObject + */ +export interface PatternObject { + /** + * + * @type {string} + * @memberof PatternObject + */ + string?: string; + /** + * + * @type {string} + * @memberof PatternObject + */ + nullableString?: string | null; + /** + * + * @type {number} + * @memberof PatternObject + */ + numberEnum?: number; +} + +export function PatternObjectFromJSON(json: any): PatternObject { + return PatternObjectFromJSONTyped(json, false); +} + +export function PatternObjectFromJSONTyped(json: any, ignoreDiscriminator: boolean): PatternObject { + if ((json === undefined) || (json === null)) { + return json; + } + return { + + 'string': !exists(json, 'string') ? undefined : json['string'], + 'nullableString': !exists(json, 'nullable-string') ? undefined : json['nullable-string'], + 'numberEnum': !exists(json, 'number-enum') ? undefined : json['number-enum'], + }; +} + +export function PatternObjectToJSON(value?: PatternObject | null): any { + if (value === undefined) { + return undefined; + } + if (value === null) { + return null; + } + return { + + 'string': value.string, + 'nullable-string': value.nullableString, + 'number-enum': value.numberEnum, + }; +} + + diff --git a/samples/client/petstore/typescript-fetch/builds/one-of/models/PatternObjectTwo.ts b/samples/client/petstore/typescript-fetch/builds/one-of/models/PatternObjectTwo.ts new file mode 100644 index 000000000000..f65012ceda42 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/one-of/models/PatternObjectTwo.ts @@ -0,0 +1,57 @@ +/* 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'; +/** + * + * @export + * @interface PatternObjectTwo + */ +export interface PatternObjectTwo { + /** + * + * @type {boolean} + * @memberof PatternObjectTwo + */ + _boolean?: boolean; +} + +export function PatternObjectTwoFromJSON(json: any): PatternObjectTwo { + return PatternObjectTwoFromJSONTyped(json, false); +} + +export function PatternObjectTwoFromJSONTyped(json: any, ignoreDiscriminator: boolean): PatternObjectTwo { + if ((json === undefined) || (json === null)) { + return json; + } + return { + + '_boolean': !exists(json, 'boolean') ? undefined : json['boolean'], + }; +} + +export function PatternObjectTwoToJSON(value?: PatternObjectTwo | null): any { + if (value === undefined) { + return undefined; + } + if (value === null) { + return null; + } + return { + + 'boolean': value._boolean, + }; +} + + diff --git a/samples/client/petstore/typescript-fetch/builds/one-of/models/index.ts b/samples/client/petstore/typescript-fetch/builds/one-of/models/index.ts new file mode 100644 index 000000000000..8664f1130625 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/one-of/models/index.ts @@ -0,0 +1,3 @@ +export * from './ObjectCombinedOneOf'; +export * from './PatternObject'; +export * from './PatternObjectTwo'; diff --git a/samples/client/petstore/typescript-fetch/builds/one-of/runtime.ts b/samples/client/petstore/typescript-fetch/builds/one-of/runtime.ts new file mode 100644 index 000000000000..c4d82bccb8ff --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/one-of/runtime.ts @@ -0,0 +1,319 @@ +/* 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 const BASE_PATH = "http://localhost:3000".replace(/\/+$/, ""); + +const isBlob = (value: any) => typeof Blob !== 'undefined' && value instanceof Blob; + +/** + * This is the base class for all generated API classes. + */ +export class BaseAPI { + + private middleware: Middleware[]; + + constructor(protected configuration = new Configuration()) { + this.middleware = configuration.middleware; + } + + withMiddleware(this: T, ...middlewares: Middleware[]) { + const next = this.clone(); + next.middleware = next.middleware.concat(...middlewares); + return next; + } + + withPreMiddleware(this: T, ...preMiddlewares: Array) { + const middlewares = preMiddlewares.map((pre) => ({ pre })); + return this.withMiddleware(...middlewares); + } + + withPostMiddleware(this: T, ...postMiddlewares: Array) { + const middlewares = postMiddlewares.map((post) => ({ post })); + return this.withMiddleware(...middlewares); + } + + protected async request(context: RequestOpts): Promise { + const { url, init } = this.createFetchParams(context); + const response = await this.fetchApi(url, init); + if (response.status >= 200 && response.status < 300) { + return response; + } + throw response; + } + + private createFetchParams(context: RequestOpts) { + let url = this.configuration.basePath + context.path; + if (context.query !== undefined && Object.keys(context.query).length !== 0) { + // only add the querystring to the URL if there are query parameters. + // this is done to avoid urls ending with a "?" character which buggy webservers + // do not handle correctly sometimes. + url += '?' + this.configuration.queryParamsStringify(context.query); + } + const body = ((typeof FormData !== "undefined" && context.body instanceof FormData) || context.body instanceof URLSearchParams || isBlob(context.body)) + ? context.body + : JSON.stringify(context.body); + + const headers = Object.assign({}, this.configuration.headers, context.headers); + const init = { + method: context.method, + headers: headers, + body, + credentials: this.configuration.credentials + }; + return { url, init }; + } + + private fetchApi = async (url: string, init: RequestInit) => { + let fetchParams = { url, init }; + for (const middleware of this.middleware) { + if (middleware.pre) { + fetchParams = await middleware.pre({ + fetch: this.fetchApi, + ...fetchParams, + }) || fetchParams; + } + } + let response = await this.configuration.fetchApi(fetchParams.url, fetchParams.init); + for (const middleware of this.middleware) { + if (middleware.post) { + response = await middleware.post({ + fetch: this.fetchApi, + url: fetchParams.url, + init: fetchParams.init, + response: response.clone(), + }) || response; + } + } + return response; + } + + /** + * Create a shallow clone of `this` by constructing a new instance + * and then shallow cloning data members. + */ + private clone(this: T): T { + const constructor = this.constructor as any; + const next = new constructor(this.configuration); + next.middleware = this.middleware.slice(); + return next; + } +}; + +export class RequiredError extends Error { + name: "RequiredError" = "RequiredError"; + constructor(public field: string, msg?: string) { + super(msg); + } +} + +export const COLLECTION_FORMATS = { + csv: ",", + ssv: " ", + tsv: "\t", + pipes: "|", +}; + +export type FetchAPI = GlobalFetch['fetch']; + +export interface ConfigurationParameters { + basePath?: string; // override base path + fetchApi?: FetchAPI; // override for fetch implementation + middleware?: Middleware[]; // middleware to apply before/after fetch requests + queryParamsStringify?: (params: HTTPQuery) => string; // stringify function for query strings + username?: string; // parameter for basic security + password?: string; // parameter for basic security + apiKey?: string | ((name: string) => string); // parameter for apiKey security + accessToken?: string | Promise | ((name?: string, scopes?: string[]) => string | Promise); // parameter for oauth2 security + headers?: HTTPHeaders; //header params we want to use on every request + credentials?: RequestCredentials; //value for the credentials param we want to use on each request +} + +export class Configuration { + constructor(private configuration: ConfigurationParameters = {}) {} + + get basePath(): string { + return this.configuration.basePath != null ? this.configuration.basePath : BASE_PATH; + } + + get fetchApi(): FetchAPI { + return this.configuration.fetchApi || window.fetch.bind(window); + } + + get middleware(): Middleware[] { + return this.configuration.middleware || []; + } + + get queryParamsStringify(): (params: HTTPQuery) => string { + return this.configuration.queryParamsStringify || querystring; + } + + get username(): string | undefined { + return this.configuration.username; + } + + get password(): string | undefined { + return this.configuration.password; + } + + get apiKey(): ((name: string) => string) | undefined { + const apiKey = this.configuration.apiKey; + if (apiKey) { + return typeof apiKey === 'function' ? apiKey : () => apiKey; + } + return undefined; + } + + get accessToken(): ((name?: string, scopes?: string[]) => string | Promise) | undefined { + const accessToken = this.configuration.accessToken; + if (accessToken) { + return typeof accessToken === 'function' ? accessToken : async () => accessToken; + } + return undefined; + } + + get headers(): HTTPHeaders | undefined { + return this.configuration.headers; + } + + get credentials(): RequestCredentials | undefined { + return this.configuration.credentials; + } +} + +export type Json = any; +export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD'; +export type HTTPHeaders = { [key: string]: string }; +export type HTTPQuery = { [key: string]: string | number | null | boolean | Array | HTTPQuery }; +export type HTTPBody = Json | FormData | URLSearchParams; +export type ModelPropertyNaming = 'camelCase' | 'snake_case' | 'PascalCase' | 'original'; + +export interface FetchParams { + url: string; + init: RequestInit; +} + +export interface RequestOpts { + path: string; + method: HTTPMethod; + headers: HTTPHeaders; + query?: HTTPQuery; + body?: HTTPBody; +} + +export function exists(json: any, key: string) { + const value = json[key]; + return value !== null && value !== undefined; +} + +export function querystring(params: HTTPQuery, prefix: string = ''): string { + return Object.keys(params) + .map((key) => { + const fullKey = prefix + (prefix.length ? `[${key}]` : key); + const value = params[key]; + if (value instanceof Array) { + const multiValue = value.map(singleValue => encodeURIComponent(String(singleValue))) + .join(`&${encodeURIComponent(fullKey)}=`); + return `${encodeURIComponent(fullKey)}=${multiValue}`; + } + if (value instanceof Date) { + return `${encodeURIComponent(fullKey)}=${encodeURIComponent(value.toISOString())}`; + } + if (value instanceof Object) { + return querystring(value as HTTPQuery, fullKey); + } + return `${encodeURIComponent(fullKey)}=${encodeURIComponent(String(value))}`; + }) + .filter(part => part.length > 0) + .join('&'); +} + +export function mapValues(data: any, fn: (item: any) => any) { + return Object.keys(data).reduce( + (acc, key) => ({ ...acc, [key]: fn(data[key]) }), + {} + ); +} + +export function canConsumeForm(consumes: Consume[]): boolean { + for (const consume of consumes) { + if ('multipart/form-data' === consume.contentType) { + return true; + } + } + return false; +} + +export interface Consume { + contentType: string +} + +export interface RequestContext { + fetch: FetchAPI; + url: string; + init: RequestInit; +} + +export interface ResponseContext { + fetch: FetchAPI; + url: string; + init: RequestInit; + response: Response; +} + +export interface Middleware { + pre?(context: RequestContext): Promise; + post?(context: ResponseContext): Promise; +} + +export interface ApiResponse { + raw: Response; + value(): Promise; +} + +export interface ResponseTransformer { + (json: any): T; +} + +export class JSONApiResponse { + constructor(public raw: Response, private transformer: ResponseTransformer = (jsonValue: any) => jsonValue) {} + + async value(): Promise { + return this.transformer(await this.raw.json()); + } +} + +export class VoidApiResponse { + constructor(public raw: Response) {} + + async value(): Promise { + return undefined; + } +} + +export class BlobApiResponse { + constructor(public raw: Response) {} + + async value(): Promise { + return await this.raw.blob(); + }; +} + +export class TextApiResponse { + constructor(public raw: Response) {} + + async value(): Promise { + return await this.raw.text(); + }; +} From 5f90822cdc194a9e55bc88fe7f0c5523858bdde6 Mon Sep 17 00:00:00 2001 From: Chris Mc Date: Fri, 13 Aug 2021 13:59:31 -0400 Subject: [PATCH 15/17] Update one-of.yaml --- .../src/test/resources/3_0/typescript-fetch/one-of.yaml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/one-of.yaml b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/one-of.yaml index 8b74069eb9cf..71c67f171c46 100644 --- a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/one-of.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/one-of.yaml @@ -46,10 +46,14 @@ components: properties: boolean: type: boolean - ObjectCombinedOneOf: + PropertyCombinedOneOf: type: "object" properties: combined-string-enum: oneOf: - $ref: '#/components/schemas/PatternObject' - $ref: '#/components/schemas/PatternObjectTwo' + ObjectCombinedOneOf: + oneOf: + - $ref: '#/components/schemas/PatternObject' + - $ref: '#/components/schemas/PatternObjectTwo' From 27abd2030d5b2825818a8c9a6e243309d86b6c71 Mon Sep 17 00:00:00 2001 From: cmcarthu Date: Fri, 13 Aug 2021 14:00:47 -0400 Subject: [PATCH 16/17] following build steps --- .../builds/one-of/.openapi-generator-ignore | 23 +++++++ .../builds/one-of/.openapi-generator/FILES | 2 +- .../one-of/models/ObjectCombinedOneOf.ts | 27 ++------ .../one-of/models/PropertyCombinedOneOf.ts | 68 +++++++++++++++++++ .../builds/one-of/models/index.ts | 1 + 5 files changed, 98 insertions(+), 23 deletions(-) create mode 100644 samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator-ignore create mode 100644 samples/client/petstore/typescript-fetch/builds/one-of/models/PropertyCombinedOneOf.ts diff --git a/samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator-ignore b/samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator-ignore new file mode 100644 index 000000000000..7484ee590a38 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator-ignore @@ -0,0 +1,23 @@ +# OpenAPI Generator Ignore +# Generated by openapi-generator https://github.com/openapitools/openapi-generator + +# Use this file to prevent files from being overwritten by the generator. +# The patterns follow closely to .gitignore or .dockerignore. + +# As an example, the C# client generator defines ApiClient.cs. +# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line: +#ApiClient.cs + +# You can match any string of characters against a directory, file or extension with a single asterisk (*): +#foo/*/qux +# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux + +# You can recursively match patterns against a directory, file or extension with a double asterisk (**): +#foo/**/qux +# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux + +# You can also negate patterns with an exclamation (!). +# For example, you can ignore all files in a docs folder with the file extension .md: +#docs/*.md +# Then explicitly reverse the ignore rule for a single file: +#!docs/README.md diff --git a/samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator/FILES b/samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator/FILES index 49af40e12985..69a993570f08 100644 --- a/samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator/FILES +++ b/samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator/FILES @@ -1,9 +1,9 @@ -.openapi-generator-ignore apis/DefaultApi.ts apis/index.ts index.ts models/ObjectCombinedOneOf.ts models/PatternObject.ts models/PatternObjectTwo.ts +models/PropertyCombinedOneOf.ts models/index.ts runtime.ts diff --git a/samples/client/petstore/typescript-fetch/builds/one-of/models/ObjectCombinedOneOf.ts b/samples/client/petstore/typescript-fetch/builds/one-of/models/ObjectCombinedOneOf.ts index 7d9bed75e7a2..63f4ad62a100 100644 --- a/samples/client/petstore/typescript-fetch/builds/one-of/models/ObjectCombinedOneOf.ts +++ b/samples/client/petstore/typescript-fetch/builds/one-of/models/ObjectCombinedOneOf.ts @@ -12,31 +12,21 @@ * Do not edit the class manually. */ -import { exists, mapValues } from '../runtime'; import { PatternObject, - PatternObjectFromJSON, + PatternObjectTwo, PatternObjectFromJSONTyped, PatternObjectToJSON, - PatternObjectTwo, - PatternObjectTwoFromJSON, PatternObjectTwoFromJSONTyped, PatternObjectTwoToJSON, } from './'; /** + * @type ObjectCombinedOneOf * * @export - * @interface ObjectCombinedOneOf */ -export interface ObjectCombinedOneOf { - /** - * - * @type {PatternObject | PatternObjectTwo} - * @memberof ObjectCombinedOneOf - */ - combinedStringEnum?: PatternObject | PatternObjectTwo; -} +export type ObjectCombinedOneOf = PatternObject | PatternObjectTwo; export function ObjectCombinedOneOfFromJSON(json: any): ObjectCombinedOneOf { return ObjectCombinedOneOfFromJSONTyped(json, false); @@ -46,10 +36,7 @@ export function ObjectCombinedOneOfFromJSONTyped(json: any, ignoreDiscriminator: if ((json === undefined) || (json === null)) { return json; } - return { - - 'combinedStringEnum': !exists(json, 'combined-string-enum') ? undefined : PatternObject | PatternObjectTwoFromJSON(json['combined-string-enum']), - }; + return PatternObjectFromJSONTyped(json, true) || PatternObjectTwoFromJSONTyped(json, true); } export function ObjectCombinedOneOfToJSON(value?: ObjectCombinedOneOf | null): any { @@ -59,10 +46,6 @@ export function ObjectCombinedOneOfToJSON(value?: ObjectCombinedOneOf | null): a if (value === null) { return null; } - return { - - 'combined-string-enum': PatternObject | PatternObjectTwoToJSON(value.combinedStringEnum), - }; + return PatternObjectToJSON(value as PatternObject) ?? PatternObjectTwoToJSON(value as PatternObjectTwo); } - diff --git a/samples/client/petstore/typescript-fetch/builds/one-of/models/PropertyCombinedOneOf.ts b/samples/client/petstore/typescript-fetch/builds/one-of/models/PropertyCombinedOneOf.ts new file mode 100644 index 000000000000..995e14ddd1fc --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/one-of/models/PropertyCombinedOneOf.ts @@ -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 { + PatternObject, + PatternObjectFromJSON, + PatternObjectFromJSONTyped, + PatternObjectToJSON, + PatternObjectTwo, + PatternObjectTwoFromJSON, + PatternObjectTwoFromJSONTyped, + PatternObjectTwoToJSON, +} from './'; + +/** + * + * @export + * @interface PropertyCombinedOneOf + */ +export interface PropertyCombinedOneOf { + /** + * + * @type {PatternObject | PatternObjectTwo} + * @memberof PropertyCombinedOneOf + */ + combinedStringEnum?: PatternObject | PatternObjectTwo; +} + +export function PropertyCombinedOneOfFromJSON(json: any): PropertyCombinedOneOf { + return PropertyCombinedOneOfFromJSONTyped(json, false); +} + +export function PropertyCombinedOneOfFromJSONTyped(json: any, ignoreDiscriminator: boolean): PropertyCombinedOneOf { + if ((json === undefined) || (json === null)) { + return json; + } + return { + + 'combinedStringEnum': !exists(json, 'combined-string-enum') ? undefined : PatternObject | PatternObjectTwoFromJSON(json['combined-string-enum']), + }; +} + +export function PropertyCombinedOneOfToJSON(value?: PropertyCombinedOneOf | null): any { + if (value === undefined) { + return undefined; + } + if (value === null) { + return null; + } + return { + + 'combined-string-enum': PatternObject | PatternObjectTwoToJSON(value.combinedStringEnum), + }; +} + + diff --git a/samples/client/petstore/typescript-fetch/builds/one-of/models/index.ts b/samples/client/petstore/typescript-fetch/builds/one-of/models/index.ts index 8664f1130625..af5b477f4c84 100644 --- a/samples/client/petstore/typescript-fetch/builds/one-of/models/index.ts +++ b/samples/client/petstore/typescript-fetch/builds/one-of/models/index.ts @@ -1,3 +1,4 @@ export * from './ObjectCombinedOneOf'; export * from './PatternObject'; export * from './PatternObjectTwo'; +export * from './PropertyCombinedOneOf'; From 3fb4b5255b7d6af9062f307e23296a63233b7bc9 Mon Sep 17 00:00:00 2001 From: Chris Mc Date: Fri, 13 Aug 2021 14:13:25 -0400 Subject: [PATCH 17/17] better null/truthy handling --- .../src/main/resources/typescript-fetch/modelOneOf.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache b/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache index 6fd04bcca74c..17ad889a4a43 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache @@ -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}}; {{/discriminator}} }