-
Notifications
You must be signed in to change notification settings - Fork 6k
Fix #4857 typescript-angular2 client deserialization failed due to change of property name #6024
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
base: master
Are you sure you want to change the base?
Conversation
Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/swagger-api/swagger-codegen/graphs/contributors. Let me know if you need help fixing it. |
Thanks for pointing that out! Somehow I hadn't added the e-mail address I use for commits to my GitHub account... |
@@ -117,7 +118,7 @@ export class PetService { | |||
if (response.status === 204) { | |||
return undefined; | |||
} else { | |||
return response.json() || {}; | |||
return ObjectSerializer.deserialize(response.json(), "Array<Pet>") || {}; |
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.
Shouldn't this be "Array<Pet>"
instead of "Array<Pet>"
?
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.
This seems to be related to {{returnType}} in line 99 in api.service.mustache.This is also an issue for the ts-node code generator.
Is the returnType encoded by the program generating the input for mustache?
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.
PLease use {{{returnType}}}
instead to avoid HTML encoding performed by mustache.
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.
I'll submit a PR to fix the issue in ts-node.
[Edit] See #6043
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.
Thanks! Fixed now.
I will take at look at this on weekend. |
@@ -254,7 +255,7 @@ export class {{classname}} { | |||
method: {{httpMethod}}, | |||
headers: headers, | |||
{{#bodyParam}} | |||
body: {{paramName}} == null ? '' : JSON.stringify({{paramName}}), // https://github.com/angular/angular/issues/10612 | |||
body: {{paramName}} == null ? '' : JSON.stringify(ObjectSerializer.serialize({{paramName}}, "{{dataType}}")), // https://github.com/angular/angular/issues/10612 |
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.
This causes the same issue {{returnType}}
caused. => {{{dataType}}}
to prevent html encoding
if (!typeMap[type]) { // dont know the type | ||
return data; | ||
} | ||
let instance = new typeMap[type](); |
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.
types.mustache:133
ObjectSerializer.deserialize (and hence the API methods) returns an instance of the modal class.
Before this fix, the API methods returned a plain object.
They are type-compatible, but some testing frameworks e.g. Jasmine, do more extended checks for equality:
class Foo {
prop: string
}
describe('Jasmine type checking', () => {
it('fails', () => {
let foo = new Foo();
foo.prop = 'Hello';
expect(foo).toBeEqual({'Hello'}); // Error: 'Expected foo to be a type of Object, but is Foo(prop: "Hello")'
});
});
Which means that some users (me included) would be having to rewrite unit-tests.
I'm also having second thoughts about adding the property name maps directly in the models, because (if I understand this correctly) they are exposed for the users to reuse in their own code. Since the property name maps are there just to enable the ObjectSerializer to function correctly, it seems to me that it would make sense to separate them from the models proper. Any thoughts? |
…he model interface and reverted classes back to interfaces.
Here are the changes I would do. I'll revert the commits if you think we should leave the property name maps as they currently are, in the model class. |
IMO the whole serializer should be hidden from the user. It is an implementation detail of the API module and the user should not rely on this functionality. This enables us to change it without breaking the code of users. The user should only interact with the model objects and the api objects. |
@TiFu I second this
…On Mon, Jul 17, 2017, 6:14 PM Tino Fuhrmann ***@***.***> wrote:
IMO the whole serializer should be hidden from the user. It is an
implementation detail of the API module and the user should not rely on
this functionality. This enables us to change it without breaking the code
of users. The user should only interact with the model objects and the api
objects.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#6024 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AMPLtftH9wFtE2Ds8dQtfqUN6uEe0PPyks5sO9ysgaJpZM4OUW9E>
.
|
Is this PR still being developed? |
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.Description of the PR
Fixes issue #4857 Deserialization failed due to change of property name for the typescript-angular2 client, re-using the solution in PR #4264:
Breaks backward compatibility I think, because we change the models from interfaces to classes, which will cause issues for users reusing the models in their code and assigning object literals (because of the added constructor / properties).
If that is undesirable, I'm happy to refactor the property name maps into a separate entity, leaving the models as interfaces.