swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[typescript-angular2] Invalid import statement in variables.ts

Open hallids opened this issue 9 years ago • 4 comments

Description

The generated file variables.ts has this code as the first line:

import { InjectionToken<string> } from '@angular/core';

As far as I can tell, this is not valid typescript, and will not compile. It produces this error:

Module parse failed: ... generated_api\variables.ts Unexpected token (1:31) You may need an appropriate loader to handle this file type. | import { InjectionToken } from ; | from; | '@angular/core';

Removing <string> fixes the problem and the code compiles:

import { InjectionToken } from '@angular/core';
Swagger-codegen version

2.3.0-SNAPSHOT (41527ead54c881f88324641809dcabfef9bf9e00)

Command line used for generation

java -jar ./swagger-codegen-cli.jar generate -i /tmp/swagger.json -l typescript-angular2 -o output/dir/

Related issues

I believe this is the same as https://github.com/swagger-api/swagger-codegen/issues/5652, although it looks like the markdown removed <string> in that issue so it's not immediately clear.

Suggest a Fix

Making the changes below fixed the problem for me.

diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java
index 17c4b6df6..89b70d742 100644
--- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java
+++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java
@@ -36,7 +36,7 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
     protected String npmName = null;
     protected String npmVersion = "1.0.0";
     protected String npmRepository = null;
-    protected String injectionToken = "InjectionToken<string>";
+    protected String injectionToken = "InjectionToken";

     public TypeScriptAngular2ClientCodegen() {
         super();
diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/variables.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/variables.mustache
index f5cdcf76e..42b2bd0bf 100644
--- a/modules/swagger-codegen/src/main/resources/typescript-angular2/variables.mustache
+++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/variables.mustache
@@ -1,6 +1,6 @@
 import { {{{injectionToken}}} } from '@angular/core';

-export const BASE_PATH = new {{{injectionToken}}}('basePath');
+export const BASE_PATH = new {{{injectionToken}}}<string>('basePath');
 export const COLLECTION_FORMATS = {
     'csv': ',',
     'tsv': '   ',

hallids avatar May 26 '17 03:05 hallids

Sorry that is a bug I made. I'll fix it soon

taxpon avatar May 26 '17 04:05 taxpon

@taxpon just a heads up, hard coding <string> in the template

export const BASE_PATH = new {{{injectionToken}}}<string>('basePath');

will cause it to fail for the OpaqueToken usage. I made the necessary changes locally thinking it was another issue and can submit a PR if you haven't gotten started yet.

austingreendev avatar May 29 '17 18:05 austingreendev

I am running into this issue too, please let me know once this is resolved.

scheler avatar Jun 13 '17 04:06 scheler

@scheler Since there have been no other responses, I will open a pull-request for it this evening.

austingreendev avatar Jun 18 '17 03:06 austingreendev