Skip to content
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] Property renaming fix when deserializing JSON #4264

Merged
merged 15 commits into from
Feb 5, 2017

Conversation

TiFu
Copy link
Contributor

@TiFu TiFu commented Nov 26, 2016

First: First time contributor, so feel free to yell at me :-).

PR checklist

  • Read the contribution guildelines.
  • Ran the shell/batch script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates)
  • Filed the PR against the correct branch: master for non-breaking changes and 2.3.0 branch for breaking (non-backward compatible) changes.

Second Point: Is it enough if i recreated the samples for typescript-node (e. g. I don't have to recreate typescript-angular samples)? I only changed one file in swagger-codegen/src/main/resources/typescript-node.

Description of the PR

Reference: #2766
This also fixes #2776

The Typescript-Node-Client did not parse the JSON returned from requests, so that properties, which were renamed (e. g. from mounted_at in the response to mountedAt in the model), still appeared as the non-renamed version (e. g. mounted_at) in the model object.

This PR fixes this issue by adding property maps and parsing the JSON recursively.

NOTE:
I'm not sure if this change breaks backwards compatibility, so I filed the PR against 2.3.0.

I hope that I did not miss any cases, I need to handle. It would be great if someone with more experience with swagger-codegen, than I have, could take a look at the code.

The previous version did not parse the JSON-Response at all, so that
properties, which where renamed (e. g.due to modelPropertyNaming config
option), were not correctly parsed.

E. g.: modelPropertyNaming=camelCase & property 'mounted_at' in json,
would be renamed to mountedAt in the model.
This was not parsed at all in the typescript-node client api, so that
the actual model still had the mounted_at property, but not mountedAt.

See swagger-api#2766 for additional details
@TiFu
Copy link
Contributor Author

TiFu commented Nov 28, 2016

@wing328 I just read the discussion in PR #4267. I will update my PR to use Map[baseName, type] and Map[baseName, name] maps in the model and create a general deserializer/serializer.
Compared to that my solution is ugly at best.

@wing328
Copy link
Contributor

wing328 commented Nov 28, 2016

@TiFu I would suggest we wait till @gregjacobs to update his PR so that we can reuse the same solution in all Typescript API clients (node, angular, angular2, typescript)

@TiFu
Copy link
Contributor Author

TiFu commented Dec 18, 2016

@wing328
I have some questions:

  1. How can I fix the issue appveyor is reporting? see [1]
  2. How can I fix the issue travis is reporting? see [2]
    • Is there a way to trigger a rebuild?
  3. The PHP Object serializer [0] has a method for sanitizing the filename which seems to be used when the Content-Disposition header is present. Why is that sanitization necessary?

[0] https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php

[1]

git clone https://github.com/wing328/swagger-samples
fatal: destination path 'swagger-samples' already exists and is not an empty directory.
Command exited with code 128

[2]

[INFO] ------------------------------------------------------------------------
[INFO] Building Swagger Petstore - Javascript Client 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.2.1:exec (npm-install) @ swagger-petstore-javascript ---
npm WARN package.json swagger_petstore@1.0.0 No repository field.
npm WARN deprecated jade@0.26.3: Jade has been renamed to pug, please install the latest version of pug instead of jade
npm WARN deprecated graceful-fs@2.0.3: graceful-fs v3.0.0 and before will fail on node releases >= v7.0. Please update to graceful-fs@^4.0.0 as soon as possible. Use 'npm ls graceful-fs' to find it in the tree.
npm WARN deprecated minimatch@0.2.14: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm ERR! Linux 4.4.0-51-generic
npm ERR! argv "/home/travis/.nvm/versions/node/v4.1.2/bin/node" "/home/travis/.nvm/versions/node/v4.1.2/bin/npm" "install"
npm ERR! node v4.1.2
npm ERR! npm  v2.14.4
npm ERR! code ECONNRESET
npm ERR! errno ECONNRESET
npm ERR! syscall read```

@wing328
Copy link
Contributor

wing328 commented Dec 18, 2016

  1. You can ignore it (or rebase on the latest master to fix it).

  2. I've restarted that particular job manually. To trigger a rebuild, please close and reopen the PR (which will trigger all the tests).

  3. sanitizeFilename is for sanitizing filename to remove relative path (e.g. ../../../newfile.txt) so as to prevent potential security issues when storing the file in a relative path.

@TiFu TiFu closed this Dec 22, 2016
@TiFu TiFu reopened this Dec 22, 2016
@TiFu
Copy link
Contributor Author

TiFu commented Dec 22, 2016

Some testing from the community would be appreciated :-) Thanks!

@wing328
Copy link
Contributor

wing328 commented Jan 4, 2017

@TiFu thanks for the PR. If it's too much work, I wonder if you can add some tests for serialization/deserialization of models similar to what we've done for PHP models: https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/php/SwaggerClient-php/tests/OrderApiTest.php#L43-L132

@TiFu
Copy link
Contributor Author

TiFu commented Jan 4, 2017

I'll look into it next week and update my PR.

Edit: I can't seem to find a time slot to implement this. I'll do it when I've more time again (probably around end of February).

@wing328
Copy link
Contributor

wing328 commented Jan 23, 2017

@TiFu please take your time. I would recommend writing tests cases for the Pet object, which contains nested objects Tag, Category: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml#L653

TiFu and others added 2 commits January 25, 2017 10:57
- primitives were serialized as strings instead of their respective type
- added test case for pet
@TiFu TiFu closed this Jan 25, 2017
@TiFu TiFu reopened this Jan 25, 2017
@TiFu
Copy link
Contributor Author

TiFu commented Feb 3, 2017

@wing328 Can you confirm that this issue also happens on the 2.3.0 branch? If I recreate the sample for ts-node and then run the test with mvn verify, the same issue happens.

It seems like a change in PetAPI.uploadFile(args) causes this issue (the type of the last argument file changed from any to Buffer). I have commited a fix in 6178cb0 .

If it works in 2.3.0, I will have to dig deeper and check how I introduced this bug.

@wing328
Copy link
Contributor

wing328 commented Feb 3, 2017

@wing328 Can you confirm that this issue also happens on the 2.3.0 branch? If I recreate the sample for ts-node and then run the test with mvn verify, the same issue happens.

Let me try with a new branch and update the TS node petstore sample to confirm...

@wing328
Copy link
Contributor

wing328 commented Feb 3, 2017

Btw, the tests now look good after you pushed 6178cb0

https://travis-ci.org/swagger-api/swagger-codegen/builds/198072272

@wing328
Copy link
Contributor

wing328 commented Feb 3, 2017

Here is the job for updated TS Node Petstore based on 2.3.0: https://travis-ci.org/swagger-api/swagger-codegen/builds/198085651 (still running)

@wing328
Copy link
Contributor

wing328 commented Feb 3, 2017

UPDATE: https://travis-ci.org/swagger-api/swagger-codegen/builds/198085651 finished without issue.

@TiFu
Copy link
Contributor Author

TiFu commented Feb 3, 2017

The issue you noticed is caused by the typemapping from file => Buffer. This mapping was added to TypeScriptNodeCodegen.java almost a year ago. Somehow the ts node tests still contain the previous any type mapping defined in AbstractTypeScriptCodegen.java.
Another thing I noticed is, that TypeScriptNodeCodegen uses typeMapping.put("file", "Buffer");, whereas AbstratcTypeScriptCodegen uses typeMapping.put("File", "any"); (note the capitalization of file)
Does that make any difference or was that changed recently?

The issue you saw was fixed on master in 55443da on 17 January 2017 (with the same fix I pushed a couple hours ago)
However, the fix was not applied to 2.3.0, which is why this error popped up.

I have no idea why this did not appear earlier. Maybe it was a caching issue on my side so that the tests did not get updated properly or the behaviour of typeMapping was changed recently.

Like you said above the tests are fine now. Do I need to do anything else to get this PR merged?

@wing328
Copy link
Contributor

wing328 commented Feb 5, 2017

Like you said above the tests are fine now. Do I need to do anything else to get this PR merged?

No, I believe we all appreciate your effort in this PR and I'll merge shortly so that other developers can enjoy the enhancements you put into the Typescript generators.

@wing328 wing328 merged commit 8e036f2 into swagger-api:2.3.0 Feb 5, 2017
@wing328
Copy link
Contributor

wing328 commented Feb 5, 2017

Another thing I noticed is, that TypeScriptNodeCodegen uses typeMapping.put("file", "Buffer");, whereas AbstratcTypeScriptCodegen uses typeMapping.put("File", "any"); (note the capitalization of file)

Ideally we want to make it consistent across all TS generators. I'll create another issue to track this.

cc @Vrolijkx

mamghari pushed a commit to mamghari/swagger-codegen that referenced this pull request Mar 6, 2017
* Implemented fix for missing json to property mapping in typescript-node
The previous version did not parse the JSON-Response at all, so that
properties, which where renamed (e. g.due to modelPropertyNaming config
option), were not correctly parsed.

E. g.: modelPropertyNaming=camelCase & property 'mounted_at' in json,
would be renamed to mountedAt in the model.
This was not parsed at all in the typescript-node client api, so that
the actual model still had the mounted_at property, but not mountedAt.

See swagger-api#2766 for additional details

* Updated samples for typescript-node

* Reverted initial changes to api.mustache

* Draft for object serializer for typescript-node

* Fixed missing variable error in ObjectSerializer in typescript-node

* Fix for body return type

* Fixed attributeTypeMaps when polymorphism is used

* Added ObjectSerializer support for polymorphism

* Code formatting in typescript-node api.mustache

* Fixed primitive type bug & added tests for ts-node
- primitives were serialized as strings instead of their respective type
- added test case for pet

* Code Formatting in ts-node client test
See samples/client/petstore/typescript-node/npm/client.ts

* Replaced tabs with 4 spaces and improved code formatting

* Recreated security test for typescript-node

* Read sample.png with fs.readFileSync instead of a stream
@wing328 wing328 changed the title Typescript Property Renaming Fix [Typescript] Property renaming fix when deserializing JSON Mar 23, 2017
@cenkentimist
Copy link

What's the rational for transforming snake case to camel case anyway? What about just leaving things as they are? given the number of bugs caused by _ conversion this might make sense? I myself prefer camel case to snake case but the current state of things basically guarantees that '_' causes bugs. It also adds unnecessary decode overhead and complexity.

@TiFu has anyone tested what happens if there's a model with both
hiThere and hi_there

@wing328
Copy link
Contributor

wing328 commented Apr 6, 2017

@cenkentimist

What's the rational for transforming snake case to camel case anyway?

We want the property naming to conform to the style guide: https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines#names

What about just leaving things as they are?

There's an option you can do so:

	modelPropertyNaming
	    Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name (Default: camelCase)`

Would that meet your requirement?

@cenkentimist
Copy link

cenkentimist commented Apr 6, 2017

@wing328 That's awesome.
edit: Seems in general language specific flags are mostly undocumented?

@wing328
Copy link
Contributor

wing328 commented Apr 6, 2017

@cenkentimist another reason is that other API client generators (e.g. PHP, Python) also has a serializer/deserializer for JSON and we want to make the TS generators more consistent with other generators.

@wing328
Copy link
Contributor

wing328 commented Apr 6, 2017

Seems in general language specific flags are mostly undocumented?

It's documented in config-help: java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar config-help -l typescript-angular2

@cenkentimist
Copy link

@wing328 Oh. I feel bad for not noticing that myself. Thanks for the clue-hammer.

@TiFu
Copy link
Contributor Author

TiFu commented Apr 6, 2017

@cenkentimist This would create a conflict in the model (duplicate attribute name) and the code will not compile.

@wing328 wing328 changed the title [Typescript] Property renaming fix when deserializing JSON [Typescript][Node] Property renaming fix when deserializing JSON Apr 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants