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 <gary.blaser@oracle.com>
This commit is contained in:
Gary Blaser 2021-02-12 02:01:40 -06:00 committed by GitHub
parent 3c4b1a0c4c
commit 0e16eba06a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 24 deletions

View File

@ -113,9 +113,9 @@ export class ObjectSerializer {
let subType: string = type.replace("Array<", ""); // Array<Type> => Type> let subType: string = type.replace("Array<", ""); // Array<Type> => Type>
subType = subType.substring(0, subType.length - 1); // Type> => Type subType = subType.substring(0, subType.length - 1); // Type> => Type
let transformedData: any[] = []; let transformedData: any[] = [];
for (let index in data) { for (let index = 0; index < data.length; index++) {
let date = data[index]; let datum = data[index];
transformedData.push(ObjectSerializer.serialize(date, subType)); transformedData.push(ObjectSerializer.serialize(datum, subType));
} }
return transformedData; return transformedData;
} else if (type === "Date") { } else if (type === "Date") {
@ -134,7 +134,7 @@ export class ObjectSerializer {
// get the map for the correct type. // get the map for the correct type.
let attributeTypes = typeMap[type].getAttributeTypeMap(); let attributeTypes = typeMap[type].getAttributeTypeMap();
let instance: {[index: string]: any} = {}; let instance: {[index: string]: any} = {};
for (let index in attributeTypes) { for (let index = 0; index < attributeTypes.length; index++) {
let attributeType = attributeTypes[index]; let attributeType = attributeTypes[index];
instance[attributeType.baseName] = ObjectSerializer.serialize(data[attributeType.name], attributeType.type); 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> => Type> let subType: string = type.replace("Array<", ""); // Array<Type> => Type>
subType = subType.substring(0, subType.length - 1); // Type> => Type subType = subType.substring(0, subType.length - 1); // Type> => Type
let transformedData: any[] = []; let transformedData: any[] = [];
for (let index in data) { for (let index = 0; index < data.length; index++) {
let date = data[index]; let datum = data[index];
transformedData.push(ObjectSerializer.deserialize(date, subType)); transformedData.push(ObjectSerializer.deserialize(datum, subType));
} }
return transformedData; return transformedData;
} else if (type === "Date") { } else if (type === "Date") {
@ -170,7 +170,7 @@ export class ObjectSerializer {
} }
let instance = new typeMap[type](); let instance = new typeMap[type]();
let attributeTypes = typeMap[type].getAttributeTypeMap(); let attributeTypes = typeMap[type].getAttributeTypeMap();
for (let index in attributeTypes) { for (let index = 0; index < attributeTypes.length; index++) {
let attributeType = attributeTypes[index]; let attributeType = attributeTypes[index];
instance[attributeType.name] = ObjectSerializer.deserialize(data[attributeType.baseName], attributeType.type); instance[attributeType.name] = ObjectSerializer.deserialize(data[attributeType.baseName], attributeType.type);
} }

View File

@ -98,9 +98,9 @@ export class ObjectSerializer {
let subType: string = type.replace("Array<", ""); // Array<Type> => Type> let subType: string = type.replace("Array<", ""); // Array<Type> => Type>
subType = subType.substring(0, subType.length - 1); // Type> => Type subType = subType.substring(0, subType.length - 1); // Type> => Type
let transformedData: any[] = []; let transformedData: any[] = [];
for (let index in data) { for (let index = 0; index < data.length; index++) {
let date = data[index]; let datum = data[index];
transformedData.push(ObjectSerializer.serialize(date, subType)); transformedData.push(ObjectSerializer.serialize(datum, subType));
} }
return transformedData; return transformedData;
} else if (type === "Date") { } else if (type === "Date") {
@ -119,7 +119,7 @@ export class ObjectSerializer {
// get the map for the correct type. // get the map for the correct type.
let attributeTypes = typeMap[type].getAttributeTypeMap(); let attributeTypes = typeMap[type].getAttributeTypeMap();
let instance: {[index: string]: any} = {}; let instance: {[index: string]: any} = {};
for (let index in attributeTypes) { for (let index = 0; index < attributeTypes.length; index++) {
let attributeType = attributeTypes[index]; let attributeType = attributeTypes[index];
instance[attributeType.baseName] = ObjectSerializer.serialize(data[attributeType.name], attributeType.type); 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> => Type> let subType: string = type.replace("Array<", ""); // Array<Type> => Type>
subType = subType.substring(0, subType.length - 1); // Type> => Type subType = subType.substring(0, subType.length - 1); // Type> => Type
let transformedData: any[] = []; let transformedData: any[] = [];
for (let index in data) { for (let index = 0; index < data.length; index++) {
let date = data[index]; let datum = data[index];
transformedData.push(ObjectSerializer.deserialize(date, subType)); transformedData.push(ObjectSerializer.deserialize(datum, subType));
} }
return transformedData; return transformedData;
} else if (type === "Date") { } else if (type === "Date") {
@ -155,7 +155,7 @@ export class ObjectSerializer {
} }
let instance = new typeMap[type](); let instance = new typeMap[type]();
let attributeTypes = typeMap[type].getAttributeTypeMap(); let attributeTypes = typeMap[type].getAttributeTypeMap();
for (let index in attributeTypes) { for (let index = 0; index < attributeTypes.length; index++) {
let attributeType = attributeTypes[index]; let attributeType = attributeTypes[index];
instance[attributeType.name] = ObjectSerializer.deserialize(data[attributeType.baseName], attributeType.type); instance[attributeType.name] = ObjectSerializer.deserialize(data[attributeType.baseName], attributeType.type);
} }

View File

@ -98,9 +98,9 @@ export class ObjectSerializer {
let subType: string = type.replace("Array<", ""); // Array<Type> => Type> let subType: string = type.replace("Array<", ""); // Array<Type> => Type>
subType = subType.substring(0, subType.length - 1); // Type> => Type subType = subType.substring(0, subType.length - 1); // Type> => Type
let transformedData: any[] = []; let transformedData: any[] = [];
for (let index in data) { for (let index = 0; index < data.length; index++) {
let date = data[index]; let datum = data[index];
transformedData.push(ObjectSerializer.serialize(date, subType)); transformedData.push(ObjectSerializer.serialize(datum, subType));
} }
return transformedData; return transformedData;
} else if (type === "Date") { } else if (type === "Date") {
@ -119,7 +119,7 @@ export class ObjectSerializer {
// get the map for the correct type. // get the map for the correct type.
let attributeTypes = typeMap[type].getAttributeTypeMap(); let attributeTypes = typeMap[type].getAttributeTypeMap();
let instance: {[index: string]: any} = {}; let instance: {[index: string]: any} = {};
for (let index in attributeTypes) { for (let index = 0; index < attributeTypes.length; index++) {
let attributeType = attributeTypes[index]; let attributeType = attributeTypes[index];
instance[attributeType.baseName] = ObjectSerializer.serialize(data[attributeType.name], attributeType.type); 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> => Type> let subType: string = type.replace("Array<", ""); // Array<Type> => Type>
subType = subType.substring(0, subType.length - 1); // Type> => Type subType = subType.substring(0, subType.length - 1); // Type> => Type
let transformedData: any[] = []; let transformedData: any[] = [];
for (let index in data) { for (let index = 0; index < data.length; index++) {
let date = data[index]; let datum = data[index];
transformedData.push(ObjectSerializer.deserialize(date, subType)); transformedData.push(ObjectSerializer.deserialize(datum, subType));
} }
return transformedData; return transformedData;
} else if (type === "Date") { } else if (type === "Date") {
@ -155,7 +155,7 @@ export class ObjectSerializer {
} }
let instance = new typeMap[type](); let instance = new typeMap[type]();
let attributeTypes = typeMap[type].getAttributeTypeMap(); let attributeTypes = typeMap[type].getAttributeTypeMap();
for (let index in attributeTypes) { for (let index = 0; index < attributeTypes.length; index++) {
let attributeType = attributeTypes[index]; let attributeType = attributeTypes[index];
instance[attributeType.name] = ObjectSerializer.deserialize(data[attributeType.baseName], attributeType.type); instance[attributeType.name] = ObjectSerializer.deserialize(data[attributeType.baseName], attributeType.type);
} }