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 0e66e19302c..e5b54a1d852 100644 --- a/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache @@ -87,37 +87,37 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole 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()))) { + if (!(isNaN(new Date(json).getTime()))) { return {{^required}}json == null ? undefined : {{/required}}({{#required}}{{#isNullable}}json == null ? null : {{/isNullable}}{{/required}}new Date(json)); } {{/isDateType}} {{^isDateType}} {{#isDateTimeType}} - if(!(isNaN(new Date(json).getTime()))) { + if (!(isNaN(new Date(json).getTime()))) { return {{^required}}json == null ? undefined : {{/required}}({{#required}}{{#isNullable}}json == null ? null : {{/isNullable}}{{/required}}new Date(json)); } {{/isDateTimeType}} {{/isDateType}} {{#isNumeric}} - if(typeof json === 'number'{{#isEnum}} && ({{#allowableValues}}{{#values}}json === {{.}}{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) { + 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}}) { + if (typeof json === 'string'{{#isEnum}} && ({{#allowableValues}}{{#values}}json === '{{.}}'{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) { return json; } {{/isString}} @@ -189,14 +189,14 @@ export function {{classname}}ToJSONTyped(value?: {{classname}} | null, ignoreDis {{/isDateTimeType}} {{#isNumeric}} if (Array.isArray(value)) { - if (value.every(item => typeof item === 'number'{{#isEnum}} && ({{#allowableValues}}{{#values}}item === {{.}}{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) { + 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}}) { + if (value.every(item => typeof item === 'string'{{#isEnum}} && ({{#allowableValues}}{{#values}}item === '{{.}}'{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}})) { return value; } } @@ -205,22 +205,22 @@ export function {{classname}}ToJSONTyped(value?: {{classname}} | null, ignoreDis {{/isArray}} {{^isArray}} {{#isDateType}} - if(value instanceof Date) { + if (value instanceof Date) { return ((value{{#isNullable}} as any{{/isNullable}}){{^required}}{{#isNullable}}?{{/isNullable}}{{/required}}.toISOString().substring(0,10)); - } + } {{/isDateType}} {{#isDateTimeType}} - if(value instanceof Date) { + 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()); } {{/isDateTimeType}} {{#isNumeric}} - if(typeof value === 'number'{{#isEnum}} && ({{#allowableValues}}{{#values}}value === {{.}}{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) { + 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}}) { + if (typeof value === 'string'{{#isEnum}} && ({{#allowableValues}}{{#values}}value === '{{.}}'{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) { return value; } {{/isString}} 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 e8a93a8f947..fb592791521 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 @@ -356,19 +356,21 @@ public class TypeScriptFetchClientCodegenTest { Path exampleModelPath = Paths.get(outputPath + "/models/MyCustomSpeed.ts"); //FromJSON - TestUtils.assertFileContains(exampleModelPath, "typeof json === 'number'"); - TestUtils.assertFileContains(exampleModelPath, "typeof json === 'string'"); - TestUtils.assertFileContains(exampleModelPath, "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')"); + 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')))"); //ToJSON - TestUtils.assertFileContains(exampleModelPath, "typeof value === 'number'"); - TestUtils.assertFileContains(exampleModelPath, "typeof value === 'string'"); - TestUtils.assertFileContains(exampleModelPath, "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')"); + 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')))"); } /** diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchModelTest.java index a7a3e644f0b..1795a90ef93 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchModelTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchModelTest.java @@ -47,7 +47,7 @@ import static io.swagger.codegen.utils.ModelUtils.updateCodegenPropertyEnum; @SuppressWarnings("static-method") public class TypeScriptFetchModelTest { - @Test(description = "convert a simple TypeScript Angular model") + @Test(description = "convert a simple TypeScript Fetch model") public void simpleModelTest() { final Schema model = new Schema() .description("a sample model") 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 ed4b8124582..29e0eb1017e 100644 --- a/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestArrayResponse.ts +++ b/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestArrayResponse.ts @@ -57,7 +57,7 @@ export function TestArrayResponseFromJSONTyped(json: any, ignoreDiscriminator: b if (json.every(item => typeof item === 'string')) { return json; } - } + } return {} as any; } @@ -81,7 +81,7 @@ export function TestArrayResponseToJSONTyped(value?: TestArrayResponse | null, i return value; } if (Array.isArray(value)) { - if (value.every(item => typeof item === 'string') { + if (value.every(item => typeof item === 'string')) { 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 466c781097c..031d545e20b 100644 --- a/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestResponse.ts +++ b/samples/client/petstore/typescript-fetch/builds/oneOf/models/TestResponse.ts @@ -51,7 +51,7 @@ export function TestResponseFromJSONTyped(json: any, ignoreDiscriminator: boolea if (instanceOfTestB(json)) { return TestBFromJSONTyped(json, true); } - if(typeof json === 'string') { + if (typeof json === 'string') { return json; } return {} as any; @@ -74,7 +74,7 @@ export function TestResponseToJSONTyped(value?: TestResponse | null, ignoreDiscr if (instanceOfTestB(value)) { return TestBToJSON(value as TestB); } - if(typeof value === 'string') { + if (typeof value === 'string') { return value; } return {};