From 0e16eba06acb431471c905225fcfbd184a7de94f Mon Sep 17 00:00:00 2001 From: Gary Blaser <50057217+glblaser@users.noreply.github.com> Date: Fri, 12 Feb 2021 02:01:40 -0600 Subject: [PATCH] typescript-node: bug in models.mustache with for...in on an empty array. (#8575) * Fixed typescript bug in models.mustache with for...in on an empty array. When an empty array comes in, the current for...in syntax results in creating an empty object being created and added to the array. This is the output from receiving an empty array that gets deserialized, resulting in an object with undefined values. Switching to a for i < array.length loop fixes this issue. ``` body: [ GroupCustomResource { apiVersion: undefined, kind: undefined, spec: undefined, metadata: undefined, status: undefined, '': undefined } ] ``` * Fixed typescript deserialization bug that was adding non-existent entries to objects during deserialization. When an object comes in, the current for...in syntax results in adding an undefined key/value pair to each nested object, with key being empty string and value being undefined. This is the output from receiving an object that gets deserialized. Adding a check to see if attributeType.name exists before adding it to the deserialized object fixes this. ``` res.body of listGroup for saved group is [ GroupCustomResource { apiVersion: 'v1', kind: 'Group', spec: GroupSpec { name: 'TestDeploymentGroup67264', tenancyId: 'tenancy', description: 'TestGroup description', '': undefined }, metadata: { additionalProp1: {}, spectra: [Object] }, status: undefined, '': undefined } ] ``` * Fixed for array for loop in serialize and added truth check for deserialize * Made for...in loop a for loop; gets rid of the need for the truth check * Fixed serialze/deserialize for loops for arrays Co-authored-by: Gary Blaser --- .../resources/typescript-node/models.mustache | 16 ++++++++-------- .../typescript-node/default/model/models.ts | 16 ++++++++-------- .../petstore/typescript-node/npm/model/models.ts | 16 ++++++++-------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/typescript-node/models.mustache b/modules/openapi-generator/src/main/resources/typescript-node/models.mustache index 93f3790d667..e803dfeb664 100644 --- a/modules/openapi-generator/src/main/resources/typescript-node/models.mustache +++ b/modules/openapi-generator/src/main/resources/typescript-node/models.mustache @@ -113,9 +113,9 @@ export class ObjectSerializer { let subType: string = type.replace("Array<", ""); // Array => Type> subType = subType.substring(0, subType.length - 1); // Type> => Type let transformedData: any[] = []; - for (let index in data) { - let date = data[index]; - transformedData.push(ObjectSerializer.serialize(date, subType)); + for (let index = 0; index < data.length; index++) { + let datum = data[index]; + transformedData.push(ObjectSerializer.serialize(datum, subType)); } return transformedData; } else if (type === "Date") { @@ -134,7 +134,7 @@ export class ObjectSerializer { // get the map for the correct type. let attributeTypes = typeMap[type].getAttributeTypeMap(); let instance: {[index: string]: any} = {}; - for (let index in attributeTypes) { + for (let index = 0; index < attributeTypes.length; index++) { let attributeType = attributeTypes[index]; instance[attributeType.baseName] = ObjectSerializer.serialize(data[attributeType.name], attributeType.type); } @@ -153,9 +153,9 @@ export class ObjectSerializer { let subType: string = type.replace("Array<", ""); // Array => Type> subType = subType.substring(0, subType.length - 1); // Type> => Type let transformedData: any[] = []; - for (let index in data) { - let date = data[index]; - transformedData.push(ObjectSerializer.deserialize(date, subType)); + for (let index = 0; index < data.length; index++) { + let datum = data[index]; + transformedData.push(ObjectSerializer.deserialize(datum, subType)); } return transformedData; } else if (type === "Date") { @@ -170,7 +170,7 @@ export class ObjectSerializer { } let instance = new typeMap[type](); let attributeTypes = typeMap[type].getAttributeTypeMap(); - for (let index in attributeTypes) { + for (let index = 0; index < attributeTypes.length; index++) { let attributeType = attributeTypes[index]; instance[attributeType.name] = ObjectSerializer.deserialize(data[attributeType.baseName], attributeType.type); } diff --git a/samples/client/petstore/typescript-node/default/model/models.ts b/samples/client/petstore/typescript-node/default/model/models.ts index ba05217de7e..4e9ba21b4e7 100644 --- a/samples/client/petstore/typescript-node/default/model/models.ts +++ b/samples/client/petstore/typescript-node/default/model/models.ts @@ -98,9 +98,9 @@ export class ObjectSerializer { let subType: string = type.replace("Array<", ""); // Array => Type> subType = subType.substring(0, subType.length - 1); // Type> => Type let transformedData: any[] = []; - for (let index in data) { - let date = data[index]; - transformedData.push(ObjectSerializer.serialize(date, subType)); + for (let index = 0; index < data.length; index++) { + let datum = data[index]; + transformedData.push(ObjectSerializer.serialize(datum, subType)); } return transformedData; } else if (type === "Date") { @@ -119,7 +119,7 @@ export class ObjectSerializer { // get the map for the correct type. let attributeTypes = typeMap[type].getAttributeTypeMap(); let instance: {[index: string]: any} = {}; - for (let index in attributeTypes) { + for (let index = 0; index < attributeTypes.length; index++) { let attributeType = attributeTypes[index]; instance[attributeType.baseName] = ObjectSerializer.serialize(data[attributeType.name], attributeType.type); } @@ -138,9 +138,9 @@ export class ObjectSerializer { let subType: string = type.replace("Array<", ""); // Array => Type> subType = subType.substring(0, subType.length - 1); // Type> => Type let transformedData: any[] = []; - for (let index in data) { - let date = data[index]; - transformedData.push(ObjectSerializer.deserialize(date, subType)); + for (let index = 0; index < data.length; index++) { + let datum = data[index]; + transformedData.push(ObjectSerializer.deserialize(datum, subType)); } return transformedData; } else if (type === "Date") { @@ -155,7 +155,7 @@ export class ObjectSerializer { } let instance = new typeMap[type](); let attributeTypes = typeMap[type].getAttributeTypeMap(); - for (let index in attributeTypes) { + for (let index = 0; index < attributeTypes.length; index++) { let attributeType = attributeTypes[index]; instance[attributeType.name] = ObjectSerializer.deserialize(data[attributeType.baseName], attributeType.type); } diff --git a/samples/client/petstore/typescript-node/npm/model/models.ts b/samples/client/petstore/typescript-node/npm/model/models.ts index ba05217de7e..4e9ba21b4e7 100644 --- a/samples/client/petstore/typescript-node/npm/model/models.ts +++ b/samples/client/petstore/typescript-node/npm/model/models.ts @@ -98,9 +98,9 @@ export class ObjectSerializer { let subType: string = type.replace("Array<", ""); // Array => Type> subType = subType.substring(0, subType.length - 1); // Type> => Type let transformedData: any[] = []; - for (let index in data) { - let date = data[index]; - transformedData.push(ObjectSerializer.serialize(date, subType)); + for (let index = 0; index < data.length; index++) { + let datum = data[index]; + transformedData.push(ObjectSerializer.serialize(datum, subType)); } return transformedData; } else if (type === "Date") { @@ -119,7 +119,7 @@ export class ObjectSerializer { // get the map for the correct type. let attributeTypes = typeMap[type].getAttributeTypeMap(); let instance: {[index: string]: any} = {}; - for (let index in attributeTypes) { + for (let index = 0; index < attributeTypes.length; index++) { let attributeType = attributeTypes[index]; instance[attributeType.baseName] = ObjectSerializer.serialize(data[attributeType.name], attributeType.type); } @@ -138,9 +138,9 @@ export class ObjectSerializer { let subType: string = type.replace("Array<", ""); // Array => Type> subType = subType.substring(0, subType.length - 1); // Type> => Type let transformedData: any[] = []; - for (let index in data) { - let date = data[index]; - transformedData.push(ObjectSerializer.deserialize(date, subType)); + for (let index = 0; index < data.length; index++) { + let datum = data[index]; + transformedData.push(ObjectSerializer.deserialize(datum, subType)); } return transformedData; } else if (type === "Date") { @@ -155,7 +155,7 @@ export class ObjectSerializer { } let instance = new typeMap[type](); let attributeTypes = typeMap[type].getAttributeTypeMap(); - for (let index in attributeTypes) { + for (let index = 0; index < attributeTypes.length; index++) { let attributeType = attributeTypes[index]; instance[attributeType.name] = ObjectSerializer.deserialize(data[attributeType.baseName], attributeType.type); }