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/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 12d60088b42c..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}} } @@ -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}} } 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..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 @@ -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: @@ -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' 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..71c67f171c46 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/one-of.yaml @@ -0,0 +1,59 @@ +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 + 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' 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 */ 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 336206328ced..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,12 @@ 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 +models/StringEnumTwo.ts +models/StringOneOf.ts models/index.ts runtime.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/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/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/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 new file mode 100644 index 000000000000..d8e7638a2bc0 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/enum/models/StringEnumTwo.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 StringEnumTwo { + Twoone = 'twoone', + Twotwo = 'twotwo', + Twothree = 'twothree' +} + +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 | null { + 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..1dcbc960dafd --- /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..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,4 +2,10 @@ 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'; +export * from './StringEnumTwo'; +export * from './StringOneOf'; 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 new file mode 100644 index 000000000000..69a993570f08 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/one-of/.openapi-generator/FILES @@ -0,0 +1,9 @@ +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/.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..63f4ad62a100 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/one-of/models/ObjectCombinedOneOf.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 { + PatternObject, + PatternObjectTwo, + PatternObjectFromJSONTyped, + PatternObjectToJSON, + PatternObjectTwoFromJSONTyped, + PatternObjectTwoToJSON, +} from './'; + +/** + * @type ObjectCombinedOneOf + * + * @export + */ +export type ObjectCombinedOneOf = 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 PatternObjectFromJSONTyped(json, true) || PatternObjectTwoFromJSONTyped(json, true); +} + +export function ObjectCombinedOneOfToJSON(value?: ObjectCombinedOneOf | null): any { + if (value === undefined) { + return undefined; + } + if (value === null) { + return null; + } + return PatternObjectToJSON(value as PatternObject) ?? PatternObjectTwoToJSON(value as PatternObjectTwo); +} + 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/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 new file mode 100644 index 000000000000..af5b477f4c84 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/one-of/models/index.ts @@ -0,0 +1,4 @@ +export * from './ObjectCombinedOneOf'; +export * from './PatternObject'; +export * from './PatternObjectTwo'; +export * from './PropertyCombinedOneOf'; 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(); + }; +} 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; }