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

PR-1334 Improved angular 2 - typescript generation #5695

Closed

Conversation

iain17
Copy link

@iain17 iain17 commented May 23, 2017

A bunch of fixes for the angular 2 typescript client generation. I had to make these changes to make use of this project. Large problems fixed, such as making it typescript compliant (my app just refused to compile without these fixes). I've updated the tests as well :)

Other problems that have now been fixed:

  • Models being generated as interfaces. They are now classes.
  • naming conflicts when it comes to enums.
  • array of a definition would result in a generated typescript syntax error.

Issue #1334 .

@iain17 iain17 changed the title PR-1334 PR-1334 Improved angular - typescript generation May 23, 2017
@iain17 iain17 changed the title PR-1334 Improved angular - typescript generation PR-1334 Improved angular 2 - typescript generation May 23, 2017
@wing328
Copy link
Contributor

wing328 commented May 24, 2017

@iain17 thanks for the PR.

It looks to me it's a breaking change so it should be filed against 2.3.0 branch instead.

Also you may want to checkout 2.3.0 to generate TS Angular2 client, which has a lot of enhancements and bug fixes.

@wing328
Copy link
Contributor

wing328 commented May 24, 2017

@Vrolijkx @taxpon @TiFu @kenisteward , please take a look when you've time. Thanks!

return response.json() || {};
}catch(e){
Copy link
Contributor

Choose a reason for hiding this comment

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

There should be a space between } and catch.

Copy link
Author

Choose a reason for hiding this comment

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

sure thing.

@@ -1,9 +1,9 @@
import { {{{injectionToken}}} } from '@angular/core';

export const BASE_PATH = new {{{injectionToken}}}('basePath');
export const BASE_PATH = new {{{injectionToken}}}<String>('basePath');
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you elaborate on this change? Shouldn't it be string like before?

Copy link
Author

Choose a reason for hiding this comment

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

Please see: https://angular.io/docs/ts/latest/api/core/index/InjectionToken-class.html
I changed it because my app just wouldn't compile otherwise.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's weird. Can you post a minimum working example which doesn't compile?
string is the TypeScript string type and String is the default JavaScript string type, which is why I am wondering if the underlying issue is something else.
See e.g. here

*
* OpenAPI spec version: 1.0.0
* Contact: apiteam@swagger.io
* Contact: apiteam@wordnik.com
Copy link
Contributor

Choose a reason for hiding this comment

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

This change was probably not intended. Could you please revert it?

Copy link
Contributor

Choose a reason for hiding this comment

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

The latest master is using petstore.yaml instead of petstore.json (which still references wordnik.com) so rebasing the PR on the latest master should avoid the above change.

@kenisteward
Copy link
Contributor

Hello @iain17 please look at my comment for #1334 and provide an explanation of why your code doesn't compile using the latest changes. I have had no issues using interfaces with my application so I worry you will be making everyone rewrite code when they upgrade for a reason that Is lovalized and not broad.

@iain17
Copy link
Author

iain17 commented May 24, 2017

@wing328 Thanks for your comment. I'll look into it and apply the necessary changes.

@iain17
Copy link
Author

iain17 commented May 25, 2017

@kenisteward I don't specifically get errors due to interfaces, there were other issues why it didn't compile unrelated to the interface change.

The main issue we had with using interfaces was the fact that we couldn't create a new instance of a model. The other was that in most languages it is bad practice to use interfaces to describe data types. Usually, you have structs or classes that do that job.

However, I do understand your point and reading more into this, I see that it's common practice in Typescript to use interfaces to describe data types (see for instance: https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-aliases).

I'll make the necessary changes and try and apply them to the current project we are doing. See how it works out for us. Right now, it feels more natural to us to say: new User(); which cannot be done with an interface.

@kenisteward
Copy link
Contributor

kenisteward commented May 25, 2017 via email

@kirpit
Copy link
Contributor

kirpit commented May 28, 2017

honestly it mostly isn't needed unless you want to actually define custom logic or functions

That's the thing, we sometimes do need to define different behaviours and alternative implementations based on inheritance. To do that in the right way, I believe there should be classes and sometimes both the classes and interfaces needed, whether it creates some upgrading work for some or not. By the way, I recently figured out using I prefix for interfaces is discouraged according TS coding guidelines.

I was actually considering to make this change and file a PR, as it looks like I'm a bit late.

@wing328 you marked the milestone to v2.2.3 but you also mentioned this should go against v2.3.0. Is it intentional?

@kenisteward Should the new model classes work consistently with the new withInterfaces parameter I introduced recently here in #5519?

@kenisteward
Copy link
Contributor

kenisteward commented May 28, 2017 via email

@wing328
Copy link
Contributor

wing328 commented Jun 10, 2017

@wing328 you marked the milestone to v2.2.3 but you also mentioned this should go against v2.3.0. Is it intentional?

I'll update it once @iain17 changes this PR for 2.3.0 (there's an "Edit" button next to the subject line)

@sebastianhaas
Copy link
Contributor

sebastianhaas commented Jun 13, 2017

@wing328 I too would like to vote against this change. I don't see any reason to move from interfaces to classes, and as @kenisteward points out

If you want to make classes just have them implement the generated

@Vrolijkx what's your opinion on this?

The other was that in most languages it is bad practice to use interfaces to describe data types.

Would you care to elaborate on this @iain17 ? While I don't think this is necessarily true for any of the programming languages/OOP paradigms I know, I sincerely have my doubts on this when it comes to Typescript. An Interface in Typescript will just disappear after compiling, why throw that away? Also, you don't even have to have a class implement an Interface to create an object of that type.

The main issue we had with using interfaces was the fact that we couldn't create a new instance of a model.

I feel like this is not true. Whats wrong with this code?

interface A {
    abc: string;
}

let test: A = {
    abc: "def"
};

@wing328 wing328 removed this from the v2.2.3 milestone Jul 16, 2017
@wing328 wing328 modified the milestones: Future, v2.3.0 Jul 27, 2017
@wing328
Copy link
Contributor

wing328 commented Sep 6, 2017

@iain17 when you've time, I wonder if you can file another PR to just do the following:

  • naming conflicts when it comes to enums.
  • array of a definition would result in a generated typescript syntax error.

Please pull the latest master as there're some enhancements to TS generators (and see if the above issues have been addressed)

@wing328 wing328 closed this Sep 6, 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.

6 participants