-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Conversation
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 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) |
@wing328
[1]
[2]
|
|
Some testing from the community would be appreciated :-) Thanks! |
@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 |
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). |
@TiFu please take your time. I would recommend writing tests cases for the |
- primitives were serialized as strings instead of their respective type - added test case for pet
@wing328 Can you confirm that this issue also happens on the It seems like a change in If it works in |
Let me try with a new branch and update the TS node petstore sample to confirm... |
Btw, the tests now look good after you pushed 6178cb0 https://travis-ci.org/swagger-api/swagger-codegen/builds/198072272 |
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) |
UPDATE: https://travis-ci.org/swagger-api/swagger-codegen/builds/198085651 finished without issue. |
The issue you noticed is caused by the typemapping from The issue you saw was fixed on master in 55443da on 17 January 2017 (with the same fix I pushed a couple hours ago) 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? |
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. |
Ideally we want to make it consistent across all TS generators. I'll create another issue to track this. cc @Vrolijkx |
* 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
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 |
We want the property naming to conform to the style guide: https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines#names
There's an option you can do so:
Would that meet your requirement? |
@wing328 That's awesome. |
@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. |
It's documented in |
@wing328 Oh. I feel bad for not noticing that myself. Thanks for the clue-hammer. |
@cenkentimist This would create a conflict in the model (duplicate attribute name) and the code will not compile. |
First: First time contributor, so feel free to yell at me :-).
PR checklist
./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)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.