From b0f61f394ca7bf35ccacf6956e0e322d06fe0682 Mon Sep 17 00:00:00 2001 From: topce Date: Thu, 7 Dec 2017 16:50:26 +0100 Subject: [PATCH] fix issue 7048 (#7108) --- .../TypeScriptAngularClientCodegen.java | 187 +++++++++--------- .../rxjs-operators.mustache | 6 - .../default/rxjs-operators.ts | 1 - .../npm/rxjs-operators.ts | 1 - .../with-interfaces/rxjs-operators.ts | 1 - .../npm/rxjs-operators.ts | 2 - .../npm/rxjs-operators.ts | 1 - 7 files changed, 98 insertions(+), 101 deletions(-) delete mode 100644 samples/client/petstore/typescript-angular-v4.3/npm/rxjs-operators.ts diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java index fefbf143b0c..ef2bd84e842 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngularClientCodegen.java @@ -47,16 +47,20 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode modelTemplateFiles.put("model.mustache", ".ts"); apiTemplateFiles.put("api.service.mustache", ".ts"); languageSpecificPrimitives.add("Blob"); - typeMapping.put("file","Blob"); + typeMapping.put("file", "Blob"); apiPackage = "api"; modelPackage = "model"; - this.cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package")); this.cliOptions.add(new CliOption(NPM_VERSION, "The version of your npm package")); - this.cliOptions.add(new CliOption(NPM_REPOSITORY, "Use this property to set an url your private npmRepo in the package.json")); - this.cliOptions.add(new CliOption(SNAPSHOT, "When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm", BooleanProperty.TYPE).defaultValue(Boolean.FALSE.toString())); - this.cliOptions.add(new CliOption(WITH_INTERFACES, "Setting this property to true will generate interfaces next to the default class implementations.", BooleanProperty.TYPE).defaultValue(Boolean.FALSE.toString())); + this.cliOptions.add(new CliOption(NPM_REPOSITORY, + "Use this property to set an url your private npmRepo in the package.json")); + this.cliOptions.add(new CliOption(SNAPSHOT, + "When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm", + BooleanProperty.TYPE).defaultValue(Boolean.FALSE.toString())); + this.cliOptions.add(new CliOption(WITH_INTERFACES, + "Setting this property to true will generate interfaces next to the default class implementations.", + BooleanProperty.TYPE).defaultValue(Boolean.FALSE.toString())); this.cliOptions.add(new CliOption(NG_VERSION, "The version of Angular. Default is '4.3'")); } @@ -79,22 +83,23 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode @Override public void processOpts() { super.processOpts(); - supportingFiles.add(new SupportingFile("models.mustache", modelPackage().replace('.', File.separatorChar), "models.ts")); - supportingFiles.add(new SupportingFile("apis.mustache", apiPackage().replace('.', File.separatorChar), "api.ts")); + supportingFiles.add( + new SupportingFile("models.mustache", modelPackage().replace('.', File.separatorChar), "models.ts")); + supportingFiles + .add(new SupportingFile("apis.mustache", apiPackage().replace('.', File.separatorChar), "api.ts")); supportingFiles.add(new SupportingFile("index.mustache", getIndexDirectory(), "index.ts")); supportingFiles.add(new SupportingFile("api.module.mustache", getIndexDirectory(), "api.module.ts")); - supportingFiles.add(new SupportingFile("rxjs-operators.mustache", getIndexDirectory(), "rxjs-operators.ts")); supportingFiles.add(new SupportingFile("configuration.mustache", getIndexDirectory(), "configuration.ts")); supportingFiles.add(new SupportingFile("variables.mustache", getIndexDirectory(), "variables.ts")); supportingFiles.add(new SupportingFile("encoder.mustache", getIndexDirectory(), "encoder.ts")); supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore")); supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); - if(additionalProperties.containsKey(NPM_NAME)) { + if (additionalProperties.containsKey(NPM_NAME)) { addNpmPackageGeneration(); } - if(additionalProperties.containsKey(WITH_INTERFACES)) { + if (additionalProperties.containsKey(WITH_INTERFACES)) { boolean withInterfaces = Boolean.parseBoolean(additionalProperties.get(WITH_INTERFACES).toString()); if (withInterfaces) { apiTemplateFiles.put("apiInterface.mustache", "Interface.ts"); @@ -114,10 +119,13 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode additionalProperties.put("injectionToken", ngVersion.atLeast("4.0.0") ? "InjectionToken" : "OpaqueToken"); additionalProperties.put("injectionTokenTyped", ngVersion.atLeast("4.0.0")); additionalProperties.put("useHttpClient", ngVersion.atLeast("4.3.0")); + if (!ngVersion.atLeast("4.3.0")) { + supportingFiles.add(new SupportingFile("rxjs-operators.mustache", getIndexDirectory(), "rxjs-operators.ts")); + } } private void addNpmPackageGeneration() { - if(additionalProperties.containsKey(NPM_NAME)) { + if (additionalProperties.containsKey(NPM_NAME)) { this.setNpmName(additionalProperties.get(NPM_NAME).toString()); } @@ -125,7 +133,8 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode this.setNpmVersion(additionalProperties.get(NPM_VERSION).toString()); } - if (additionalProperties.containsKey(SNAPSHOT) && Boolean.valueOf(additionalProperties.get(SNAPSHOT).toString())) { + if (additionalProperties.containsKey(SNAPSHOT) + && Boolean.valueOf(additionalProperties.get(SNAPSHOT).toString())) { this.setNpmVersion(npmVersion + "-SNAPSHOT." + SNAPSHOT_SUFFIX_FORMAT.format(new Date())); } additionalProperties.put(NPM_VERSION, npmVersion); @@ -154,17 +163,17 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode @Override public String getTypeDeclaration(Property p) { Property inner; - if(p instanceof ArrayProperty) { - ArrayProperty mp1 = (ArrayProperty)p; + if (p instanceof ArrayProperty) { + ArrayProperty mp1 = (ArrayProperty) p; inner = mp1.getItems(); return this.getSwaggerType(p) + "<" + this.getTypeDeclaration(inner) + ">"; - } else if(p instanceof MapProperty) { - MapProperty mp = (MapProperty)p; + } else if (p instanceof MapProperty) { + MapProperty mp = (MapProperty) p; inner = mp.getAdditionalProperties(); return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }"; - } else if(p instanceof FileProperty) { + } else if (p instanceof FileProperty) { return "Blob"; - } else if(p instanceof ObjectProperty) { + } else if (p instanceof ObjectProperty) { return "any"; } else { return super.getTypeDeclaration(p); @@ -174,7 +183,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode @Override public String getSwaggerType(Property p) { String swaggerType = super.getSwaggerType(p); - if(isLanguagePrimitive(swaggerType) || isLanguageGenericType(swaggerType)) { + if (isLanguagePrimitive(swaggerType) || isLanguageGenericType(swaggerType)) { return swaggerType; } applyLocalTypeMapping(swaggerType); @@ -182,7 +191,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode } private String applyLocalTypeMapping(String type) { - if (typeMapping.containsKey(type)) { + if (typeMapping.containsKey(type)) { type = typeMapping.get(type); } return type; @@ -193,8 +202,8 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode } private boolean isLanguageGenericType(String type) { - for (String genericType: languageGenericTypes) { - if (type.startsWith(genericType + "<")) { + for (String genericType : languageGenericTypes) { + if (type.startsWith(genericType + "<")) { return true; } } @@ -222,29 +231,29 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode // Convert httpMethod to Angular's RequestMethod enum // https://angular.io/docs/ts/latest/api/http/index/RequestMethod-enum.html switch (op.httpMethod) { - case "GET": - op.httpMethod = "RequestMethod.Get"; - break; - case "POST": - op.httpMethod = "RequestMethod.Post"; - break; - case "PUT": - op.httpMethod = "RequestMethod.Put"; - break; - case "DELETE": - op.httpMethod = "RequestMethod.Delete"; - break; - case "OPTIONS": - op.httpMethod = "RequestMethod.Options"; - break; - case "HEAD": - op.httpMethod = "RequestMethod.Head"; - break; - case "PATCH": - op.httpMethod = "RequestMethod.Patch"; - break; - default: - throw new RuntimeException("Unknown HTTP Method " + op.httpMethod + " not allowed"); + case "GET": + op.httpMethod = "RequestMethod.Get"; + break; + case "POST": + op.httpMethod = "RequestMethod.Post"; + break; + case "PUT": + op.httpMethod = "RequestMethod.Put"; + break; + case "DELETE": + op.httpMethod = "RequestMethod.Delete"; + break; + case "OPTIONS": + op.httpMethod = "RequestMethod.Options"; + break; + case "HEAD": + op.httpMethod = "RequestMethod.Head"; + break; + case "PATCH": + op.httpMethod = "RequestMethod.Patch"; + break; + default: + throw new RuntimeException("Unknown HTTP Method " + op.httpMethod + " not allowed"); } } @@ -256,41 +265,41 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode boolean foundUnderscore = false; // Iterate through existing string, one character at a time. - for(int i = 0; i < op.path.length(); i++) { - switch(op.path.charAt(i)) { - case '{': - // We entered curly braces, so track that. - insideCurly++; + for (int i = 0; i < op.path.length(); i++) { + switch (op.path.charAt(i)) { + case '{': + // We entered curly braces, so track that. + insideCurly++; - // Add the more complicated component instead of just the brace. - pathBuffer.append("${encodeURIComponent(String("); - break; - case '}': - // We exited curly braces, so track that. - insideCurly--; + // Add the more complicated component instead of just the brace. + pathBuffer.append("${encodeURIComponent(String("); + break; + case '}': + // We exited curly braces, so track that. + insideCurly--; - // Add the more complicated component instead of just the brace. - pathBuffer.append("))}"); - break; - case '_': - // If we're inside the curly brace, the following character will need to be uppercase. - // Otherwise, just add the character. - if (insideCurly > 0) { - foundUnderscore = true; - } else { - pathBuffer.append(op.path.charAt(i)); - } - break; - default: - // If we previously found an underscore, we need an uppercase letter. - // Otherwise, just add the character. - if (foundUnderscore) { - pathBuffer.append(Character.toUpperCase(op.path.charAt(i))); - foundUnderscore = false; - } else { - pathBuffer.append(op.path.charAt(i)); - } - break; + // Add the more complicated component instead of just the brace. + pathBuffer.append("))}"); + break; + case '_': + // If we're inside the curly brace, the following character will need to be uppercase. + // Otherwise, just add the character. + if (insideCurly > 0) { + foundUnderscore = true; + } else { + pathBuffer.append(op.path.charAt(i)); + } + break; + default: + // If we previously found an underscore, we need an uppercase letter. + // Otherwise, just add the character. + if (foundUnderscore) { + pathBuffer.append(Character.toUpperCase(op.path.charAt(i))); + foundUnderscore = false; + } else { + pathBuffer.append(op.path.charAt(i)); + } + break; } } @@ -300,7 +309,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode // Add additional filename information for model imports in the services List> imports = (List>) operations.get("imports"); - for(Map im : imports) { + for (Map im : imports) { im.put("filename", im.get("import")); im.put("classname", getModelnameFromModelFilename(im.get("filename").toString())); } @@ -317,23 +326,23 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode for (Object _mo : models) { Map mo = (Map) _mo; CodegenModel cm = (CodegenModel) mo.get("model"); - mo.put("tsImports", toTsImports(cm,cm.imports)); + mo.put("tsImports", toTsImports(cm, cm.imports)); } return result; } private List> toTsImports(CodegenModel cm, Set imports) { - List> tsImports = new ArrayList<>(); - for(String im : imports) { - if(!im.equals(cm.classname)) { - HashMap tsImport = new HashMap<>(); - tsImport.put("classname", im); - tsImport.put("filename", toModelFilename(im)); - tsImports.add(tsImport); - } + List> tsImports = new ArrayList<>(); + for (String im : imports) { + if (!im.equals(cm.classname)) { + HashMap tsImport = new HashMap<>(); + tsImport.put("classname", im); + tsImport.put("filename", toModelFilename(im)); + tsImports.add(tsImport); } - return tsImports; + } + return tsImports; } @Override diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular/rxjs-operators.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular/rxjs-operators.mustache index 49e265860b6..5659cd0694f 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular/rxjs-operators.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular/rxjs-operators.mustache @@ -1,8 +1,3 @@ -{{#useHttpClient}} -// RxJS imports are not needed for Angular 4.3 and later -{{/useHttpClient}} - -{{^useHttpClient}} // RxJS imports according to https://angular.io/docs/ts/latest/guide/server-communication.html#!#rxjs // See node_module/rxjs/Rxjs.js @@ -14,4 +9,3 @@ import 'rxjs/add/observable/throw'; // Operators import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/map'; -{{/useHttpClient}} \ No newline at end of file diff --git a/samples/client/petstore/typescript-angular-v2/default/rxjs-operators.ts b/samples/client/petstore/typescript-angular-v2/default/rxjs-operators.ts index 2e79f871519..5659cd0694f 100644 --- a/samples/client/petstore/typescript-angular-v2/default/rxjs-operators.ts +++ b/samples/client/petstore/typescript-angular-v2/default/rxjs-operators.ts @@ -1,4 +1,3 @@ - // RxJS imports according to https://angular.io/docs/ts/latest/guide/server-communication.html#!#rxjs // See node_module/rxjs/Rxjs.js diff --git a/samples/client/petstore/typescript-angular-v2/npm/rxjs-operators.ts b/samples/client/petstore/typescript-angular-v2/npm/rxjs-operators.ts index 2e79f871519..5659cd0694f 100644 --- a/samples/client/petstore/typescript-angular-v2/npm/rxjs-operators.ts +++ b/samples/client/petstore/typescript-angular-v2/npm/rxjs-operators.ts @@ -1,4 +1,3 @@ - // RxJS imports according to https://angular.io/docs/ts/latest/guide/server-communication.html#!#rxjs // See node_module/rxjs/Rxjs.js diff --git a/samples/client/petstore/typescript-angular-v2/with-interfaces/rxjs-operators.ts b/samples/client/petstore/typescript-angular-v2/with-interfaces/rxjs-operators.ts index 2e79f871519..5659cd0694f 100644 --- a/samples/client/petstore/typescript-angular-v2/with-interfaces/rxjs-operators.ts +++ b/samples/client/petstore/typescript-angular-v2/with-interfaces/rxjs-operators.ts @@ -1,4 +1,3 @@ - // RxJS imports according to https://angular.io/docs/ts/latest/guide/server-communication.html#!#rxjs // See node_module/rxjs/Rxjs.js diff --git a/samples/client/petstore/typescript-angular-v4.3/npm/rxjs-operators.ts b/samples/client/petstore/typescript-angular-v4.3/npm/rxjs-operators.ts deleted file mode 100644 index ae0d4c491d7..00000000000 --- a/samples/client/petstore/typescript-angular-v4.3/npm/rxjs-operators.ts +++ /dev/null @@ -1,2 +0,0 @@ -// RxJS imports are not needed for Angular 4.3 and later - diff --git a/samples/client/petstore/typescript-angular-v4/npm/rxjs-operators.ts b/samples/client/petstore/typescript-angular-v4/npm/rxjs-operators.ts index 2e79f871519..5659cd0694f 100644 --- a/samples/client/petstore/typescript-angular-v4/npm/rxjs-operators.ts +++ b/samples/client/petstore/typescript-angular-v4/npm/rxjs-operators.ts @@ -1,4 +1,3 @@ - // RxJS imports according to https://angular.io/docs/ts/latest/guide/server-communication.html#!#rxjs // See node_module/rxjs/Rxjs.js