fix issue 7048 (#7108)

This commit is contained in:
topce 2017-12-07 16:50:26 +01:00 committed by William Cheng
parent a7da5d8098
commit b0f61f394c
7 changed files with 98 additions and 101 deletions

View File

@ -47,16 +47,20 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
modelTemplateFiles.put("model.mustache", ".ts"); modelTemplateFiles.put("model.mustache", ".ts");
apiTemplateFiles.put("api.service.mustache", ".ts"); apiTemplateFiles.put("api.service.mustache", ".ts");
languageSpecificPrimitives.add("Blob"); languageSpecificPrimitives.add("Blob");
typeMapping.put("file","Blob"); typeMapping.put("file", "Blob");
apiPackage = "api"; apiPackage = "api";
modelPackage = "model"; 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_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_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(NPM_REPOSITORY,
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())); "Use this property to set an url your private npmRepo in the package.json"));
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(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'")); 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 @Override
public void processOpts() { public void processOpts() {
super.processOpts(); super.processOpts();
supportingFiles.add(new SupportingFile("models.mustache", modelPackage().replace('.', File.separatorChar), "models.ts")); supportingFiles.add(
supportingFiles.add(new SupportingFile("apis.mustache", apiPackage().replace('.', File.separatorChar), "api.ts")); 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("index.mustache", getIndexDirectory(), "index.ts"));
supportingFiles.add(new SupportingFile("api.module.mustache", getIndexDirectory(), "api.module.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("configuration.mustache", getIndexDirectory(), "configuration.ts"));
supportingFiles.add(new SupportingFile("variables.mustache", getIndexDirectory(), "variables.ts")); supportingFiles.add(new SupportingFile("variables.mustache", getIndexDirectory(), "variables.ts"));
supportingFiles.add(new SupportingFile("encoder.mustache", getIndexDirectory(), "encoder.ts")); supportingFiles.add(new SupportingFile("encoder.mustache", getIndexDirectory(), "encoder.ts"));
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore")); supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh")); supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
if(additionalProperties.containsKey(NPM_NAME)) { if (additionalProperties.containsKey(NPM_NAME)) {
addNpmPackageGeneration(); addNpmPackageGeneration();
} }
if(additionalProperties.containsKey(WITH_INTERFACES)) { if (additionalProperties.containsKey(WITH_INTERFACES)) {
boolean withInterfaces = Boolean.parseBoolean(additionalProperties.get(WITH_INTERFACES).toString()); boolean withInterfaces = Boolean.parseBoolean(additionalProperties.get(WITH_INTERFACES).toString());
if (withInterfaces) { if (withInterfaces) {
apiTemplateFiles.put("apiInterface.mustache", "Interface.ts"); 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("injectionToken", ngVersion.atLeast("4.0.0") ? "InjectionToken" : "OpaqueToken");
additionalProperties.put("injectionTokenTyped", ngVersion.atLeast("4.0.0")); additionalProperties.put("injectionTokenTyped", ngVersion.atLeast("4.0.0"));
additionalProperties.put("useHttpClient", ngVersion.atLeast("4.3.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() { private void addNpmPackageGeneration() {
if(additionalProperties.containsKey(NPM_NAME)) { if (additionalProperties.containsKey(NPM_NAME)) {
this.setNpmName(additionalProperties.get(NPM_NAME).toString()); this.setNpmName(additionalProperties.get(NPM_NAME).toString());
} }
@ -125,7 +133,8 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
this.setNpmVersion(additionalProperties.get(NPM_VERSION).toString()); 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())); this.setNpmVersion(npmVersion + "-SNAPSHOT." + SNAPSHOT_SUFFIX_FORMAT.format(new Date()));
} }
additionalProperties.put(NPM_VERSION, npmVersion); additionalProperties.put(NPM_VERSION, npmVersion);
@ -154,17 +163,17 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
@Override @Override
public String getTypeDeclaration(Property p) { public String getTypeDeclaration(Property p) {
Property inner; Property inner;
if(p instanceof ArrayProperty) { if (p instanceof ArrayProperty) {
ArrayProperty mp1 = (ArrayProperty)p; ArrayProperty mp1 = (ArrayProperty) p;
inner = mp1.getItems(); inner = mp1.getItems();
return this.getSwaggerType(p) + "<" + this.getTypeDeclaration(inner) + ">"; return this.getSwaggerType(p) + "<" + this.getTypeDeclaration(inner) + ">";
} else if(p instanceof MapProperty) { } else if (p instanceof MapProperty) {
MapProperty mp = (MapProperty)p; MapProperty mp = (MapProperty) p;
inner = mp.getAdditionalProperties(); inner = mp.getAdditionalProperties();
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }"; return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
} else if(p instanceof FileProperty) { } else if (p instanceof FileProperty) {
return "Blob"; return "Blob";
} else if(p instanceof ObjectProperty) { } else if (p instanceof ObjectProperty) {
return "any"; return "any";
} else { } else {
return super.getTypeDeclaration(p); return super.getTypeDeclaration(p);
@ -174,7 +183,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
@Override @Override
public String getSwaggerType(Property p) { public String getSwaggerType(Property p) {
String swaggerType = super.getSwaggerType(p); String swaggerType = super.getSwaggerType(p);
if(isLanguagePrimitive(swaggerType) || isLanguageGenericType(swaggerType)) { if (isLanguagePrimitive(swaggerType) || isLanguageGenericType(swaggerType)) {
return swaggerType; return swaggerType;
} }
applyLocalTypeMapping(swaggerType); applyLocalTypeMapping(swaggerType);
@ -193,7 +202,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
} }
private boolean isLanguageGenericType(String type) { private boolean isLanguageGenericType(String type) {
for (String genericType: languageGenericTypes) { for (String genericType : languageGenericTypes) {
if (type.startsWith(genericType + "<")) { if (type.startsWith(genericType + "<")) {
return true; return true;
} }
@ -256,8 +265,8 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
boolean foundUnderscore = false; boolean foundUnderscore = false;
// Iterate through existing string, one character at a time. // Iterate through existing string, one character at a time.
for(int i = 0; i < op.path.length(); i++) { for (int i = 0; i < op.path.length(); i++) {
switch(op.path.charAt(i)) { switch (op.path.charAt(i)) {
case '{': case '{':
// We entered curly braces, so track that. // We entered curly braces, so track that.
insideCurly++; insideCurly++;
@ -300,7 +309,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
// Add additional filename information for model imports in the services // Add additional filename information for model imports in the services
List<Map<String, Object>> imports = (List<Map<String, Object>>) operations.get("imports"); List<Map<String, Object>> imports = (List<Map<String, Object>>) operations.get("imports");
for(Map<String, Object> im : imports) { for (Map<String, Object> im : imports) {
im.put("filename", im.get("import")); im.put("filename", im.get("import"));
im.put("classname", getModelnameFromModelFilename(im.get("filename").toString())); im.put("classname", getModelnameFromModelFilename(im.get("filename").toString()));
} }
@ -317,7 +326,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
for (Object _mo : models) { for (Object _mo : models) {
Map<String, Object> mo = (Map<String, Object>) _mo; Map<String, Object> mo = (Map<String, Object>) _mo;
CodegenModel cm = (CodegenModel) mo.get("model"); CodegenModel cm = (CodegenModel) mo.get("model");
mo.put("tsImports", toTsImports(cm,cm.imports)); mo.put("tsImports", toTsImports(cm, cm.imports));
} }
return result; return result;
@ -325,8 +334,8 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
private List<Map<String, String>> toTsImports(CodegenModel cm, Set<String> imports) { private List<Map<String, String>> toTsImports(CodegenModel cm, Set<String> imports) {
List<Map<String, String>> tsImports = new ArrayList<>(); List<Map<String, String>> tsImports = new ArrayList<>();
for(String im : imports) { for (String im : imports) {
if(!im.equals(cm.classname)) { if (!im.equals(cm.classname)) {
HashMap<String, String> tsImport = new HashMap<>(); HashMap<String, String> tsImport = new HashMap<>();
tsImport.put("classname", im); tsImport.put("classname", im);
tsImport.put("filename", toModelFilename(im)); tsImport.put("filename", toModelFilename(im));

View File

@ -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 // RxJS imports according to https://angular.io/docs/ts/latest/guide/server-communication.html#!#rxjs
// See node_module/rxjs/Rxjs.js // See node_module/rxjs/Rxjs.js
@ -14,4 +9,3 @@ import 'rxjs/add/observable/throw';
// Operators // Operators
import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map'; import 'rxjs/add/operator/map';
{{/useHttpClient}}

View File

@ -1,4 +1,3 @@
// RxJS imports according to https://angular.io/docs/ts/latest/guide/server-communication.html#!#rxjs // RxJS imports according to https://angular.io/docs/ts/latest/guide/server-communication.html#!#rxjs
// See node_module/rxjs/Rxjs.js // See node_module/rxjs/Rxjs.js

View File

@ -1,4 +1,3 @@
// RxJS imports according to https://angular.io/docs/ts/latest/guide/server-communication.html#!#rxjs // RxJS imports according to https://angular.io/docs/ts/latest/guide/server-communication.html#!#rxjs
// See node_module/rxjs/Rxjs.js // See node_module/rxjs/Rxjs.js

View File

@ -1,4 +1,3 @@
// RxJS imports according to https://angular.io/docs/ts/latest/guide/server-communication.html#!#rxjs // RxJS imports according to https://angular.io/docs/ts/latest/guide/server-communication.html#!#rxjs
// See node_module/rxjs/Rxjs.js // See node_module/rxjs/Rxjs.js

View File

@ -1,2 +0,0 @@
// RxJS imports are not needed for Angular 4.3 and later

View File

@ -1,4 +1,3 @@
// RxJS imports according to https://angular.io/docs/ts/latest/guide/server-communication.html#!#rxjs // RxJS imports according to https://angular.io/docs/ts/latest/guide/server-communication.html#!#rxjs
// See node_module/rxjs/Rxjs.js // See node_module/rxjs/Rxjs.js