Skip to content

Conversation

ard61
Copy link

@ard61 ard61 commented Jul 11, 2017

PR checklist

  • Read the contribution guidelines.
  • 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.

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:

  • adds a property name map to each model, turning them from an interface into a class
  • adds global objects tracking created classes and enums
  • adds a class, ObjectSerializer, responsible for translating property names (and serializing/deserializing dates), and makes the generated api use this class at serialization/deserialization.

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.

@ard61 ard61 changed the title Fix issue4857 Fix #4857 typescript-angular2 client deserialization failed due to change of property name Jul 11, 2017
@wing328
Copy link
Contributor

wing328 commented Jul 11, 2017

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.

Ref: https://github.com/swagger-api/swagger-codegen/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

@wing328
Copy link
Contributor

wing328 commented Jul 11, 2017

@ard61
Copy link
Author

ard61 commented Jul 11, 2017

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>") || {};
Copy link

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&lt;Pet&gt;"?

Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor

@TiFu TiFu Jul 13, 2017

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Fixed now.

@sebastianhaas
Copy link
Contributor

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
Copy link
Contributor

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]();
Copy link
Author

@ard61 ard61 Jul 17, 2017

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.

@ard61
Copy link
Author

ard61 commented Jul 17, 2017

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?

@ard61
Copy link
Author

ard61 commented Jul 17, 2017

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.

@TiFu
Copy link
Contributor

TiFu commented Jul 17, 2017

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.

@kenisteward
Copy link
Contributor

kenisteward commented Jul 17, 2017 via email

@wing328 wing328 changed the base branch from 2.3.0 to master July 27, 2017 11:14
@iamsamwood
Copy link

Is this PR still being developed?

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.

8 participants