From 2e4b2ef17af74ae757d538e60af02f25bd71b649 Mon Sep 17 00:00:00 2001 From: Tobias Trumm Date: Wed, 3 Sep 2025 18:38:52 +0200 Subject: [PATCH] fix(typescript-fetch): Do not remove primitive values when using oneOf Fixes OpenAPITools/openapi-generator#21878, OpenAPITools/openapi-generator#21811 --- .../typescript-fetch/modelOneOf.mustache | 92 ++++--------------- .../TypeScriptFetchClientCodegenTest.java | 53 ++++++++--- .../resources/3_0/typescript-fetch/oneOf.yaml | 49 ++++++++++ .../builds/oneOf/.openapi-generator/FILES | 2 + .../builds/oneOf/apis/DefaultApi.ts | 60 ++++++++++++ .../builds/oneOf/models/TestArrayResponse.ts | 18 +--- .../oneOf/models/TestPrimitiveResponse.ts | 43 +++++++++ .../builds/oneOf/models/TestResponse.ts | 24 ++--- .../oneOf/models/TestWithDateResponse.ts | 59 ++++++++++++ .../builds/oneOf/models/index.ts | 2 + 10 files changed, 278 insertions(+), 124 deletions(-) create mode 100644 samples/client/petstore/typescript-fetch/builds/oneOf/models/TestPrimitiveResponse.ts create mode 100644 samples/client/petstore/typescript-fetch/builds/oneOf/models/TestWithDateResponse.ts 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 7d5c01878949..82f83239bcb8 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache @@ -42,12 +42,7 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole {{/discriminator}} {{^discriminator}} {{#oneOfModels}} - {{#-first}} - if (typeof json !== 'object') { - return json; - } - {{/-first}} - if (instanceOf{{{.}}}(json)) { + if (typeof json === 'object' && instanceOf{{{.}}}(json)) { return {{{.}}}FromJSONTyped(json, true); } {{/oneOfModels}} @@ -61,7 +56,6 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole } {{#-last}} } - return json; } {{/-last}} {{/oneOfArrays}} @@ -70,60 +64,36 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole {{#items}} {{#isDateType}} if (Array.isArray(json)) { - if (json.every(item => !(isNaN(new Date(json).getTime()))) { - return json.map(value => new Date(json); + if (json.every(item => !(isNaN(new Date(item).getTime())))) { + return json.map(item => new Date(item)); } } {{/isDateType}} {{#isDateTimeType}} if (Array.isArray(json)) { - if (json.every(item => !(isNaN(new Date(json).getTime()))) { - return json.map(value => new Date(json); + if (json.every(item => !(isNaN(new Date(item).getTime())))) { + return json.map(item => new Date(item)); } } {{/isDateTimeType}} - {{#isNumeric}} - if (Array.isArray(json)) { - if (json.every(item => typeof item === 'number'{{#isEnum}} && ({{#allowableValues}}{{#values}}item === {{.}}{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}})) { - return json; - } - } - {{/isNumeric}} - {{#isString}} - if (Array.isArray(json)) { - if (json.every(item => typeof item === 'string'{{#isEnum}} && ({{#allowableValues}}{{#values}}item === '{{.}}'{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}})) { - return json; - } - } - {{/isString}} {{/items}} {{/isArray}} {{^isArray}} {{#isDateType}} if (!(isNaN(new Date(json).getTime()))) { - return {{^required}}json == null ? undefined : {{/required}}({{#required}}{{#isNullable}}json == null ? null : {{/isNullable}}{{/required}}new Date(json)); + return new Date(json); } {{/isDateType}} {{^isDateType}} {{#isDateTimeType}} if (!(isNaN(new Date(json).getTime()))) { - return {{^required}}json == null ? undefined : {{/required}}({{#required}}{{#isNullable}}json == null ? null : {{/isNullable}}{{/required}}new Date(json)); + return new Date(json); } {{/isDateTimeType}} {{/isDateType}} - {{#isNumeric}} - if (typeof json === 'number'{{#isEnum}} && ({{#allowableValues}}{{#values}}json === {{.}}{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) { - return json; - } - {{/isNumeric}} - {{#isString}} - if (typeof json === 'string'{{#isEnum}} && ({{#allowableValues}}{{#values}}json === '{{.}}'{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) { - return json; - } - {{/isString}} {{/isArray}} {{/oneOfPrimitives}} - return {} as any; + return json; {{/discriminator}} } @@ -147,12 +117,7 @@ export function {{classname}}ToJSONTyped(value?: {{classname}} | null, ignoreDis {{/discriminator}} {{^discriminator}} {{#oneOfModels}} - {{#-first}} - if (typeof value !== 'object') { - return value; - } - {{/-first}} - if (instanceOf{{{.}}}(value)) { + if (typeof value === 'object' && instanceOf{{{.}}}(value)) { return {{{.}}}ToJSON(value as {{{.}}}); } {{/oneOfModels}} @@ -166,7 +131,6 @@ export function {{classname}}ToJSONTyped(value?: {{classname}} | null, ignoreDis } {{#-last}} } - return value; } {{/-last}} {{/oneOfArrays}} @@ -175,57 +139,33 @@ export function {{classname}}ToJSONTyped(value?: {{classname}} | null, ignoreDis {{#items}} {{#isDateType}} if (Array.isArray(value)) { - if (value.every(item => item instanceof Date) { - return value.map(value => value.toISOString().substring(0,10))); + if (value.every(item => item instanceof Date)) { + return value.map(value => value.toISOString().substring(0,10)); } } {{/isDateType}} {{#isDateTimeType}} if (Array.isArray(value)) { - if (value.every(item => item instanceof Date) { - return value.map(value => value.toISOString(); + if (value.every(item => item instanceof Date)) { + return value.map(value => value.toISOString()); } } {{/isDateTimeType}} - {{#isNumeric}} - if (Array.isArray(value)) { - if (value.every(item => typeof item === 'number'{{#isEnum}} && ({{#allowableValues}}{{#values}}item === {{.}}{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}})) { - return value; - } - } - {{/isNumeric}} - {{#isString}} - if (Array.isArray(value)) { - if (value.every(item => typeof item === 'string'{{#isEnum}} && ({{#allowableValues}}{{#values}}item === '{{.}}'{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}})) { - return value; - } - } - {{/isString}} {{/items}} {{/isArray}} {{^isArray}} {{#isDateType}} if (value instanceof Date) { - return ((value{{#isNullable}} as any{{/isNullable}}){{^required}}{{#isNullable}}?{{/isNullable}}{{/required}}.toISOString().substring(0,10)); + return value.toISOString().substring(0,10); } {{/isDateType}} {{#isDateTimeType}} if (value instanceof Date) { - return {{^required}}{{#isNullable}}value === null ? null : {{/isNullable}}{{^isNullable}}value == null ? undefined : {{/isNullable}}{{/required}}((value{{#isNullable}} as any{{/isNullable}}){{^required}}{{#isNullable}}?{{/isNullable}}{{/required}}.toISOString()); + return value.toISOString(); } {{/isDateTimeType}} - {{#isNumeric}} - if (typeof value === 'number'{{#isEnum}} && ({{#allowableValues}}{{#values}}value === {{.}}{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) { - return value; - } - {{/isNumeric}} - {{#isString}} - if (typeof value === 'string'{{#isEnum}} && ({{#allowableValues}}{{#values}}value === '{{.}}'{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) { - return value; - } - {{/isString}} {{/isArray}} {{/oneOfPrimitives}} - return {}; + return value; {{/discriminator}} } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java index 1608fe2750cf..339119d4bd5d 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java @@ -358,21 +358,41 @@ public void givenSchemaIsOneOfAndComposedSchemasArePrimitiveThenReturnStatements Path exampleModelPath = Paths.get(outputPath + "/models/MyCustomSpeed.ts"); //FromJSON - TestUtils.assertFileContains(exampleModelPath, "(typeof json !== 'object')"); - TestUtils.assertFileContains(exampleModelPath, "(instanceOfMyNumericValue(json))"); - TestUtils.assertFileContains(exampleModelPath, "(typeof json === 'number' && (json === 10 || json === 20 || json === 30))"); - TestUtils.assertFileContains(exampleModelPath, "(typeof json === 'string' && (json === 'fixed-value-a' || json === 'fixed-value-b' || json === 'fixed-value-c'))"); - TestUtils.assertFileContains(exampleModelPath, "(isNaN(new Date(json).getTime())"); - TestUtils.assertFileContains(exampleModelPath, "(json.every(item => typeof item === 'number'))"); - TestUtils.assertFileContains(exampleModelPath, "(json.every(item => typeof item === 'string' && (item === 'oneof-array-enum-a' || item === 'oneof-array-enum-b' || item === 'oneof-array-enum-c')))"); + String expectedFromJSON = + "export function MyCustomSpeedFromJSONTyped(json: any, ignoreDiscriminator: boolean): MyCustomSpeed {" + + " if (json == null) {" + + " return json;" + + " }" + + " if (typeof json === 'object' && instanceOfMyNumericValue(json)) {" + + " return MyNumericValueFromJSONTyped(json, true);" + + " }" + + " if (!(isNaN(new Date(json).getTime()))) {" + + " return new Date(json);" + + " }" + + " if (!(isNaN(new Date(json).getTime()))) {" + + " return new Date(json);" + + " }" + + " return json;" + + "}"; + TestUtils.assertFileContains(exampleModelPath, expectedFromJSON); //ToJSON - TestUtils.assertFileContains(exampleModelPath, "(typeof value !== 'object')"); - TestUtils.assertFileContains(exampleModelPath, "(instanceOfMyNumericValue(value))"); - TestUtils.assertFileContains(exampleModelPath, "(typeof value === 'number' && (value === 10 || value === 20 || value === 30))"); - TestUtils.assertFileContains(exampleModelPath, "(typeof value === 'string' && (value === 'fixed-value-a' || value === 'fixed-value-b' || value === 'fixed-value-c'))"); - TestUtils.assertFileContains(exampleModelPath, "(value instanceof Date)"); - TestUtils.assertFileContains(exampleModelPath, "(value.every(item => typeof item === 'number'))"); - TestUtils.assertFileContains(exampleModelPath, "(value.every(item => typeof item === 'string' && (item === 'oneof-array-enum-a' || item === 'oneof-array-enum-b' || item === 'oneof-array-enum-c')))"); + String expectedToJSON = + "export function MyCustomSpeedToJSONTyped(value?: MyCustomSpeed | null, ignoreDiscriminator: boolean = false): any {" + + " if (value == null) {" + + " return value;" + + " }" + + " if (typeof value === 'object' && instanceOfMyNumericValue(value)) {" + + " return MyNumericValueToJSON(value as MyNumericValue);" + + " }" + + " if (value instanceof Date) {" + + " return value.toISOString();" + + " }" + + " if (value instanceof Date) {" + + " return value.toISOString().substring(0,10);" + + " }" + + " return value;" + + "}"; + TestUtils.assertFileContains(exampleModelPath, expectedToJSON); } /** @@ -397,7 +417,10 @@ public void testOneOfModelsDoNotImportPrimitiveTypes() throws IOException { TestUtils.assertFileContains(testArrayResponse, "import type { TestA } from './TestA'"); TestUtils.assertFileContains(testArrayResponse, "import type { TestB } from './TestB'"); TestUtils.assertFileNotContains(testResponse, "import type { string } from './string'"); - TestUtils.assertFileContains(testArrayResponse, "export type TestArrayResponse = Array | Array | Array"); + TestUtils.assertFileNotContains(testResponse, "import type { boolean } from './boolean'"); + TestUtils.assertFileNotContains(testResponse, "import type { number } from './number'"); + TestUtils.assertFileNotContains(testResponse, "import type { object } from './object'"); + TestUtils.assertFileContains(testArrayResponse, "export type TestArrayResponse = Array | Array | Array | Array | Array | Array"); Path testDiscriminatorResponse = Paths.get(output + "/models/TestDiscriminatorResponse.ts"); TestUtils.assertFileExists(testDiscriminatorResponse); diff --git a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/oneOf.yaml b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/oneOf.yaml index 958b601804dc..5f0dad1a1db2 100644 --- a/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/oneOf.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/typescript-fetch/oneOf.yaml @@ -35,6 +35,26 @@ paths: application/json: schema: $ref: '#/components/schemas/TestDiscriminatorResponse' + /test-primitive: + get: + operationId: testPrimitive + responses: + 200: + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/TestPrimitiveResponse' + /test-with-date: + get: + operationId: testWithDate + responses: + 200: + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/TestWithDateResponse' components: schemas: TestArrayResponse: @@ -48,6 +68,15 @@ components: - type: array items: type: string + - type: array + items: + type: number + - type: array + items: + type: boolean + - type: array + items: + type: object TestDiscriminatorResponse: discriminator: propertyName: discriminatorField @@ -62,6 +91,26 @@ components: - $ref: "#/components/schemas/TestA" - $ref: "#/components/schemas/TestB" - type: string + TestPrimitiveResponse: + oneOf: + - type: string + - type: number + - type: object + - type: boolean + - type: array + items: {} + TestWithDateResponse: + oneOf: + - type: string + format: date + - type: number + - type: array + items: + type: string + format: date + - type: array + items: + type: number TestA: type: object properties: diff --git a/samples/client/petstore/typescript-fetch/builds/oneOf/.openapi-generator/FILES b/samples/client/petstore/typescript-fetch/builds/oneOf/.openapi-generator/FILES index 037062f16b80..703eccbf250a 100644 --- a/samples/client/petstore/typescript-fetch/builds/oneOf/.openapi-generator/FILES +++ b/samples/client/petstore/typescript-fetch/builds/oneOf/.openapi-generator/FILES @@ -7,6 +7,8 @@ models/TestA.ts models/TestArrayResponse.ts models/TestB.ts models/TestDiscriminatorResponse.ts +models/TestPrimitiveResponse.ts models/TestResponse.ts +models/TestWithDateResponse.ts models/index.ts runtime.ts diff --git a/samples/client/petstore/typescript-fetch/builds/oneOf/apis/DefaultApi.ts b/samples/client/petstore/typescript-fetch/builds/oneOf/apis/DefaultApi.ts index 4d855b758fb7..65ad7abb205e 100644 --- a/samples/client/petstore/typescript-fetch/builds/oneOf/apis/DefaultApi.ts +++ b/samples/client/petstore/typescript-fetch/builds/oneOf/apis/DefaultApi.ts @@ -17,15 +17,21 @@ import * as runtime from '../runtime'; import type { TestArrayResponse, TestDiscriminatorResponse, + TestPrimitiveResponse, TestResponse, + TestWithDateResponse, } from '../models/index'; import { TestArrayResponseFromJSON, TestArrayResponseToJSON, TestDiscriminatorResponseFromJSON, TestDiscriminatorResponseToJSON, + TestPrimitiveResponseFromJSON, + TestPrimitiveResponseToJSON, TestResponseFromJSON, TestResponseToJSON, + TestWithDateResponseFromJSON, + TestWithDateResponseToJSON, } from '../models/index'; /** @@ -114,4 +120,58 @@ export class DefaultApi extends runtime.BaseAPI { return await response.value(); } + /** + */ + async testPrimitiveRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise> { + const queryParameters: any = {}; + + const headerParameters: runtime.HTTPHeaders = {}; + + + let urlPath = `/test-primitive`; + + const response = await this.request({ + path: urlPath, + method: 'GET', + headers: headerParameters, + query: queryParameters, + }, initOverrides); + + return new runtime.JSONApiResponse(response, (jsonValue) => TestPrimitiveResponseFromJSON(jsonValue)); + } + + /** + */ + async testPrimitive(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise { + const response = await this.testPrimitiveRaw(initOverrides); + return await response.value(); + } + + /** + */ + async testWithDateRaw(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise> { + const queryParameters: any = {}; + + const headerParameters: runtime.HTTPHeaders = {}; + + + let urlPath = `/test-with-date`; + + const response = await this.request({ + path: urlPath, + method: 'GET', + headers: headerParameters, + query: queryParameters, + }, initOverrides); + + return new runtime.JSONApiResponse(response, (jsonValue) => TestWithDateResponseFromJSON(jsonValue)); + } + + /** + */ + async testWithDate(initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise { + const response = await this.testWithDateRaw(initOverrides); + return await response.value(); + } + } diff --git a/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestArrayResponse.ts b/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestArrayResponse.ts index 29e0eb1017e3..e4a9238d23b0 100644 --- a/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestArrayResponse.ts +++ b/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestArrayResponse.ts @@ -32,7 +32,7 @@ import { * * @export */ -export type TestArrayResponse = Array | Array | Array; +export type TestArrayResponse = Array | Array | Array | Array | Array | Array; export function TestArrayResponseFromJSON(json: any): TestArrayResponse { return TestArrayResponseFromJSONTyped(json, false); @@ -51,14 +51,8 @@ export function TestArrayResponseFromJSONTyped(json: any, ignoreDiscriminator: b return json.map(value => TestBFromJSONTyped(value, true)); } } - return json; - } - if (Array.isArray(json)) { - if (json.every(item => typeof item === 'string')) { - return json; - } } - return {} as any; + return json; } export function TestArrayResponseToJSON(json: any): any { @@ -78,13 +72,7 @@ export function TestArrayResponseToJSONTyped(value?: TestArrayResponse | null, i return value.map(value => TestBToJSON(value as TestB)); } } - return value; - } - if (Array.isArray(value)) { - if (value.every(item => typeof item === 'string')) { - return value; - } } - return {}; + return value; } diff --git a/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestPrimitiveResponse.ts b/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestPrimitiveResponse.ts new file mode 100644 index 000000000000..79692186e680 --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestPrimitiveResponse.ts @@ -0,0 +1,43 @@ +/* tslint:disable */ +/* eslint-disable */ +/** + * testing oneOf without discriminator + * 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. + */ + +/** + * @type TestPrimitiveResponse + * + * @export + */ +export type TestPrimitiveResponse = Array | boolean | number | object | string; + +export function TestPrimitiveResponseFromJSON(json: any): TestPrimitiveResponse { + return TestPrimitiveResponseFromJSONTyped(json, false); +} + +export function TestPrimitiveResponseFromJSONTyped(json: any, ignoreDiscriminator: boolean): TestPrimitiveResponse { + if (json == null) { + return json; + } + return json; +} + +export function TestPrimitiveResponseToJSON(json: any): any { + return TestPrimitiveResponseToJSONTyped(json, false); +} + +export function TestPrimitiveResponseToJSONTyped(value?: TestPrimitiveResponse | null, ignoreDiscriminator: boolean = false): any { + if (value == null) { + return value; + } + return value; +} + diff --git a/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestResponse.ts b/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestResponse.ts index 031d545e20bc..4507f8021865 100644 --- a/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestResponse.ts +++ b/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestResponse.ts @@ -42,19 +42,13 @@ export function TestResponseFromJSONTyped(json: any, ignoreDiscriminator: boolea if (json == null) { return json; } - if (typeof json !== 'object') { - return json; - } - if (instanceOfTestA(json)) { + if (typeof json === 'object' && instanceOfTestA(json)) { return TestAFromJSONTyped(json, true); } - if (instanceOfTestB(json)) { + if (typeof json === 'object' && instanceOfTestB(json)) { return TestBFromJSONTyped(json, true); } - if (typeof json === 'string') { - return json; - } - return {} as any; + return json; } export function TestResponseToJSON(json: any): any { @@ -65,18 +59,12 @@ export function TestResponseToJSONTyped(value?: TestResponse | null, ignoreDiscr if (value == null) { return value; } - if (typeof value !== 'object') { - return value; - } - if (instanceOfTestA(value)) { + if (typeof value === 'object' && instanceOfTestA(value)) { return TestAToJSON(value as TestA); } - if (instanceOfTestB(value)) { + if (typeof value === 'object' && instanceOfTestB(value)) { return TestBToJSON(value as TestB); } - if (typeof value === 'string') { - return value; - } - return {}; + return value; } diff --git a/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestWithDateResponse.ts b/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestWithDateResponse.ts new file mode 100644 index 000000000000..435281ff150c --- /dev/null +++ b/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestWithDateResponse.ts @@ -0,0 +1,59 @@ +/* tslint:disable */ +/* eslint-disable */ +/** + * testing oneOf without discriminator + * 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. + */ + +/** + * @type TestWithDateResponse + * + * @export + */ +export type TestWithDateResponse = Array | Array | Date | number; + +export function TestWithDateResponseFromJSON(json: any): TestWithDateResponse { + return TestWithDateResponseFromJSONTyped(json, false); +} + +export function TestWithDateResponseFromJSONTyped(json: any, ignoreDiscriminator: boolean): TestWithDateResponse { + if (json == null) { + return json; + } + if (Array.isArray(json)) { + if (json.every(item => !(isNaN(new Date(item).getTime())))) { + return json.map(item => new Date(item)); + } + } + if (!(isNaN(new Date(json).getTime()))) { + return new Date(json); + } + return json; +} + +export function TestWithDateResponseToJSON(json: any): any { + return TestWithDateResponseToJSONTyped(json, false); +} + +export function TestWithDateResponseToJSONTyped(value?: TestWithDateResponse | null, ignoreDiscriminator: boolean = false): any { + if (value == null) { + return value; + } + if (Array.isArray(value)) { + if (value.every(item => item instanceof Date)) { + return value.map(value => value.toISOString().substring(0,10)); + } + } + if (value instanceof Date) { + return value.toISOString().substring(0,10); + } + return value; +} + diff --git a/samples/client/petstore/typescript-fetch/builds/oneOf/models/index.ts b/samples/client/petstore/typescript-fetch/builds/oneOf/models/index.ts index 79f8303e9ed5..053712c488b9 100644 --- a/samples/client/petstore/typescript-fetch/builds/oneOf/models/index.ts +++ b/samples/client/petstore/typescript-fetch/builds/oneOf/models/index.ts @@ -6,4 +6,6 @@ export * from './TestA'; export * from './TestArrayResponse'; export * from './TestB'; export * from './TestDiscriminatorResponse'; +export * from './TestPrimitiveResponse'; export * from './TestResponse'; +export * from './TestWithDateResponse';