Skip to content

Commit 7c1dce4

Browse files
[Bug][typescript-fetch] Fix missing close parenthesis in oneOf models. (#21645)
* Modify unit tests for typescript-fetch, issue 21259 to check for closing parens & fix a typo in TypeScriptFetchModelTest * Fix bug in closing parens in modelOneOf.mustache and some spacing adjustments * Update samples --------- Co-authored-by: Chris Gual <[email protected]>
1 parent 815a732 commit 7c1dce4

File tree

5 files changed

+32
-30
lines changed

5 files changed

+32
-30
lines changed

modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,37 +87,37 @@ export function {{classname}}FromJSONTyped(json: any, ignoreDiscriminator: boole
8787
if (json.every(item => typeof item === 'number'{{#isEnum}} && ({{#allowableValues}}{{#values}}item === {{.}}{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}})) {
8888
return json;
8989
}
90-
}
90+
}
9191
{{/isNumeric}}
9292
{{#isString}}
9393
if (Array.isArray(json)) {
9494
if (json.every(item => typeof item === 'string'{{#isEnum}} && ({{#allowableValues}}{{#values}}item === '{{.}}'{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}})) {
9595
return json;
9696
}
97-
}
97+
}
9898
{{/isString}}
9999
{{/items}}
100100
{{/isArray}}
101101
{{^isArray}}
102102
{{#isDateType}}
103-
if(!(isNaN(new Date(json).getTime()))) {
103+
if (!(isNaN(new Date(json).getTime()))) {
104104
return {{^required}}json == null ? undefined : {{/required}}({{#required}}{{#isNullable}}json == null ? null : {{/isNullable}}{{/required}}new Date(json));
105105
}
106106
{{/isDateType}}
107107
{{^isDateType}}
108108
{{#isDateTimeType}}
109-
if(!(isNaN(new Date(json).getTime()))) {
109+
if (!(isNaN(new Date(json).getTime()))) {
110110
return {{^required}}json == null ? undefined : {{/required}}({{#required}}{{#isNullable}}json == null ? null : {{/isNullable}}{{/required}}new Date(json));
111111
}
112112
{{/isDateTimeType}}
113113
{{/isDateType}}
114114
{{#isNumeric}}
115-
if(typeof json === 'number'{{#isEnum}} && ({{#allowableValues}}{{#values}}json === {{.}}{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) {
115+
if (typeof json === 'number'{{#isEnum}} && ({{#allowableValues}}{{#values}}json === {{.}}{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) {
116116
return json;
117117
}
118118
{{/isNumeric}}
119119
{{#isString}}
120-
if(typeof json === 'string'{{#isEnum}} && ({{#allowableValues}}{{#values}}json === '{{.}}'{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) {
120+
if (typeof json === 'string'{{#isEnum}} && ({{#allowableValues}}{{#values}}json === '{{.}}'{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) {
121121
return json;
122122
}
123123
{{/isString}}
@@ -189,14 +189,14 @@ export function {{classname}}ToJSONTyped(value?: {{classname}} | null, ignoreDis
189189
{{/isDateTimeType}}
190190
{{#isNumeric}}
191191
if (Array.isArray(value)) {
192-
if (value.every(item => typeof item === 'number'{{#isEnum}} && ({{#allowableValues}}{{#values}}item === {{.}}{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) {
192+
if (value.every(item => typeof item === 'number'{{#isEnum}} && ({{#allowableValues}}{{#values}}item === {{.}}{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}})) {
193193
return value;
194194
}
195195
}
196196
{{/isNumeric}}
197197
{{#isString}}
198198
if (Array.isArray(value)) {
199-
if (value.every(item => typeof item === 'string'{{#isEnum}} && ({{#allowableValues}}{{#values}}item === '{{.}}'{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) {
199+
if (value.every(item => typeof item === 'string'{{#isEnum}} && ({{#allowableValues}}{{#values}}item === '{{.}}'{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}})) {
200200
return value;
201201
}
202202
}
@@ -205,22 +205,22 @@ export function {{classname}}ToJSONTyped(value?: {{classname}} | null, ignoreDis
205205
{{/isArray}}
206206
{{^isArray}}
207207
{{#isDateType}}
208-
if(value instanceof Date) {
208+
if (value instanceof Date) {
209209
return ((value{{#isNullable}} as any{{/isNullable}}){{^required}}{{#isNullable}}?{{/isNullable}}{{/required}}.toISOString().substring(0,10));
210-
}
210+
}
211211
{{/isDateType}}
212212
{{#isDateTimeType}}
213-
if(value instanceof Date) {
213+
if (value instanceof Date) {
214214
return {{^required}}{{#isNullable}}value === null ? null : {{/isNullable}}{{^isNullable}}value == null ? undefined : {{/isNullable}}{{/required}}((value{{#isNullable}} as any{{/isNullable}}){{^required}}{{#isNullable}}?{{/isNullable}}{{/required}}.toISOString());
215215
}
216216
{{/isDateTimeType}}
217217
{{#isNumeric}}
218-
if(typeof value === 'number'{{#isEnum}} && ({{#allowableValues}}{{#values}}value === {{.}}{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) {
218+
if (typeof value === 'number'{{#isEnum}} && ({{#allowableValues}}{{#values}}value === {{.}}{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) {
219219
return value;
220220
}
221221
{{/isNumeric}}
222222
{{#isString}}
223-
if(typeof value === 'string'{{#isEnum}} && ({{#allowableValues}}{{#values}}value === '{{.}}'{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) {
223+
if (typeof value === 'string'{{#isEnum}} && ({{#allowableValues}}{{#values}}value === '{{.}}'{{^-last}} || {{/-last}}{{/values}}{{/allowableValues}}){{/isEnum}}) {
224224
return value;
225225
}
226226
{{/isString}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -356,19 +356,21 @@ public void givenSchemaIsOneOfAndComposedSchemasArePrimitiveThenReturnStatements
356356

357357
Path exampleModelPath = Paths.get(outputPath + "/models/MyCustomSpeed.ts");
358358
//FromJSON
359-
TestUtils.assertFileContains(exampleModelPath, "typeof json === 'number'");
360-
TestUtils.assertFileContains(exampleModelPath, "typeof json === 'string'");
361-
TestUtils.assertFileContains(exampleModelPath, "json === 'fixed-value-a' || json === 'fixed-value-b' || json === 'fixed-value-c'");
362-
TestUtils.assertFileContains(exampleModelPath, "isNaN(new Date(json).getTime())");
363-
TestUtils.assertFileContains(exampleModelPath, "json.every(item => typeof item === 'number'");
364-
TestUtils.assertFileContains(exampleModelPath, "json.every(item => typeof item === 'string' && (item === 'oneof-array-enum-a' || item === 'oneof-array-enum-b' || item === 'oneof-array-enum-c')");
359+
TestUtils.assertFileContains(exampleModelPath, "(typeof json !== 'object')");
360+
TestUtils.assertFileContains(exampleModelPath, "(instanceOfMyNumericValue(json))");
361+
TestUtils.assertFileContains(exampleModelPath, "(typeof json === 'number' && (json === 10 || json === 20 || json === 30))");
362+
TestUtils.assertFileContains(exampleModelPath, "(typeof json === 'string' && (json === 'fixed-value-a' || json === 'fixed-value-b' || json === 'fixed-value-c'))");
363+
TestUtils.assertFileContains(exampleModelPath, "(isNaN(new Date(json).getTime())");
364+
TestUtils.assertFileContains(exampleModelPath, "(json.every(item => typeof item === 'number'))");
365+
TestUtils.assertFileContains(exampleModelPath, "(json.every(item => typeof item === 'string' && (item === 'oneof-array-enum-a' || item === 'oneof-array-enum-b' || item === 'oneof-array-enum-c')))");
365366
//ToJSON
366-
TestUtils.assertFileContains(exampleModelPath, "typeof value === 'number'");
367-
TestUtils.assertFileContains(exampleModelPath, "typeof value === 'string'");
368-
TestUtils.assertFileContains(exampleModelPath, "value === 'fixed-value-a' || value === 'fixed-value-b' || value === 'fixed-value-c'");
369-
TestUtils.assertFileContains(exampleModelPath, "value instanceof Date");
370-
TestUtils.assertFileContains(exampleModelPath, "value.every(item => typeof item === 'number'");
371-
TestUtils.assertFileContains(exampleModelPath, "value.every(item => typeof item === 'string' && (item === 'oneof-array-enum-a' || item === 'oneof-array-enum-b' || item === 'oneof-array-enum-c')");
367+
TestUtils.assertFileContains(exampleModelPath, "(typeof value !== 'object')");
368+
TestUtils.assertFileContains(exampleModelPath, "(instanceOfMyNumericValue(value))");
369+
TestUtils.assertFileContains(exampleModelPath, "(typeof value === 'number' && (value === 10 || value === 20 || value === 30))");
370+
TestUtils.assertFileContains(exampleModelPath, "(typeof value === 'string' && (value === 'fixed-value-a' || value === 'fixed-value-b' || value === 'fixed-value-c'))");
371+
TestUtils.assertFileContains(exampleModelPath, "(value instanceof Date)");
372+
TestUtils.assertFileContains(exampleModelPath, "(value.every(item => typeof item === 'number'))");
373+
TestUtils.assertFileContains(exampleModelPath, "(value.every(item => typeof item === 'string' && (item === 'oneof-array-enum-a' || item === 'oneof-array-enum-b' || item === 'oneof-array-enum-c')))");
372374
}
373375

374376
/**

modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchModelTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
@SuppressWarnings("static-method")
4848
public class TypeScriptFetchModelTest {
4949

50-
@Test(description = "convert a simple TypeScript Angular model")
50+
@Test(description = "convert a simple TypeScript Fetch model")
5151
public void simpleModelTest() {
5252
final Schema model = new Schema()
5353
.description("a sample model")

samples/client/petstore/typescript-fetch/builds/oneOf/models/TestArrayResponse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function TestArrayResponseFromJSONTyped(json: any, ignoreDiscriminator: b
5757
if (json.every(item => typeof item === 'string')) {
5858
return json;
5959
}
60-
}
60+
}
6161
return {} as any;
6262
}
6363

@@ -81,7 +81,7 @@ export function TestArrayResponseToJSONTyped(value?: TestArrayResponse | null, i
8181
return value;
8282
}
8383
if (Array.isArray(value)) {
84-
if (value.every(item => typeof item === 'string') {
84+
if (value.every(item => typeof item === 'string')) {
8585
return value;
8686
}
8787
}

samples/client/petstore/typescript-fetch/builds/oneOf/models/TestResponse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function TestResponseFromJSONTyped(json: any, ignoreDiscriminator: boolea
5151
if (instanceOfTestB(json)) {
5252
return TestBFromJSONTyped(json, true);
5353
}
54-
if(typeof json === 'string') {
54+
if (typeof json === 'string') {
5555
return json;
5656
}
5757
return {} as any;
@@ -74,7 +74,7 @@ export function TestResponseToJSONTyped(value?: TestResponse | null, ignoreDiscr
7474
if (instanceOfTestB(value)) {
7575
return TestBToJSON(value as TestB);
7676
}
77-
if(typeof value === 'string') {
77+
if (typeof value === 'string') {
7878
return value;
7979
}
8080
return {};

0 commit comments

Comments
 (0)