-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
typescript-node: Fixed models.mustache with for...in on an empty array #8575
Conversation
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 } ] ```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…ries 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 } ] ```
@glblaser please merge the most recent master and re-generate the samples according to the PR checklist |
@glblaser Looks like you have two distinct changes:
Could you please clarify the impact of each change? Showing the api-spec example, and generated code "before" and "after" would be very helpful also. |
@amakhrov Sure thing. Let me know if anything's unclear. For the for...in on an empty array change to a regular for loop.
After the change my result is:
Before the change my when getting an array of size 1:
After the change my result is:
It seems to add this "ghost" object to my returned result with all values being undefined. For the truthiness check change (note: this is with the above array change implemented).
After the change my result is:
The truthiness check removes the '': undefined key/value pair from each nested object. |
Thanks for the clarification @glblaser ! The changes look good to me. As a side note, it looks like you might have something funky in your codebase (perhaps extending Array.prototype with custom properties). |
@@ -172,7 +172,9 @@ export class ObjectSerializer { | |||
let attributeTypes = typeMap[type].getAttributeTypeMap(); | |||
for (let index in attributeTypes) { | |||
let attributeType = attributeTypes[index]; | |||
instance[attributeType.name] = ObjectSerializer.deserialize(data[attributeType.baseName], attributeType.type); | |||
if (attributeType.name) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps a more robust fix could be to switch from for .. in
to for
here.
since you get some random property from attributeTypes
, it's totally possible that this property also has a nested name
key - so this check would be true, which is undesired.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
attributeTypes is just an array of the key's name and the value's type. I modified my fix for this to changing the for...in loop to a regular for loop, as that does not pick up the "ghost" property on the array like the for...in loop. This also negates the need for a truth check.
attributeTypes is [
{ name: 'group', baseName: 'group', type: 'string' },
{ name: 'name', baseName: 'name', type: 'string' },
{ name: 'description', baseName: 'description', type: 'string' }
]
Please merge the latest master to your branch to address some of the CI issues. |
Messed up earlier by rebasing to the latest master, but corrected that with a fetch/merge. I think it's good to go now. Not sure about the the ci/circleci failure/errors having to do with my changes. |
@glblaser thanks for the PR, which has been included in the v5.1.0 release: https://twitter.com/oas_generator/status/1373636654024380423 |
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.
PR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.For Windows users, please run the script in Git BASH.
master
,5.1.x
,6.0.x
TypeScript Committee Members:
@TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov