forked from loafle/openapi-generator-original
Codegen class rename for Angular and AngularJS to reflect naming guidelines (#6109)
* Rrename for angular and angularjs to reflect naming guidelines see http://angularjs.blogspot.com.au/2017/01/branding-guidelines-for-angular-and.html - move makes typescript-angular -> typescript-angularjs - move makes typescript-angular2 -> typescript-angular Language string with generate was already this so generation shouldnt change but class naming follows this convention now * Fixing merge problems
This commit is contained in:
@@ -1,360 +0,0 @@
|
||||
package io.swagger.codegen.languages;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import io.swagger.codegen.CliOption;
|
||||
import io.swagger.codegen.CodegenModel;
|
||||
import io.swagger.codegen.CodegenParameter;
|
||||
import io.swagger.codegen.CodegenOperation;
|
||||
import io.swagger.codegen.SupportingFile;
|
||||
import io.swagger.models.ModelImpl;
|
||||
import io.swagger.models.properties.ArrayProperty;
|
||||
import io.swagger.models.properties.BooleanProperty;
|
||||
import io.swagger.models.properties.FileProperty;
|
||||
import io.swagger.models.properties.MapProperty;
|
||||
import io.swagger.models.properties.ObjectProperty;
|
||||
import io.swagger.models.properties.Property;
|
||||
|
||||
public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCodegen {
|
||||
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm");
|
||||
|
||||
public static final String NPM_NAME = "npmName";
|
||||
public static final String NPM_VERSION = "npmVersion";
|
||||
public static final String NPM_REPOSITORY = "npmRepository";
|
||||
public static final String SNAPSHOT = "snapshot";
|
||||
public static final String WITH_INTERFACES = "withInterfaces";
|
||||
public static final String NG_VERSION = "ngVersion";
|
||||
|
||||
protected String npmName = null;
|
||||
protected String npmVersion = "1.0.0";
|
||||
protected String npmRepository = null;
|
||||
protected String ngVersion = "4";
|
||||
|
||||
public TypeScriptAngular2ClientCodegen() {
|
||||
super();
|
||||
this.outputFolder = "generated-code/typescript-angular2";
|
||||
|
||||
embeddedTemplateDir = templateDir = "typescript-angular2";
|
||||
modelTemplateFiles.put("model.mustache", ".ts");
|
||||
apiTemplateFiles.put("api.service.mustache", ".ts");
|
||||
languageSpecificPrimitives.add("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(NG_VERSION, "The version of Angular (2 or 4). Default is '4'"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, ModelImpl swaggerModel) {
|
||||
codegenModel.additionalPropertiesType = getTypeDeclaration(swaggerModel.getAdditionalProperties());
|
||||
addImport(codegenModel, codegenModel.additionalPropertiesType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "typescript-angular";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelp() {
|
||||
return "Generates a TypeScript Angular (2.x or 4.x) client library.";
|
||||
}
|
||||
|
||||
@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("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("gitignore", "", ".gitignore"));
|
||||
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
|
||||
|
||||
if(additionalProperties.containsKey(NPM_NAME)) {
|
||||
addNpmPackageGeneration();
|
||||
}
|
||||
|
||||
if(additionalProperties.containsKey(WITH_INTERFACES)) {
|
||||
boolean withInterfaces = Boolean.parseBoolean(additionalProperties.get(WITH_INTERFACES).toString());
|
||||
if (withInterfaces) {
|
||||
apiTemplateFiles.put("apiInterface.mustache", "Interface.ts");
|
||||
}
|
||||
}
|
||||
|
||||
// determine NG version
|
||||
if (additionalProperties.containsKey(NG_VERSION)) {
|
||||
if ("2".equals(additionalProperties.get(NG_VERSION).toString())) {
|
||||
additionalProperties.put("isNg2x", true);
|
||||
setNgVersion("2");
|
||||
} else if ("4".equals(additionalProperties.get(NG_VERSION).toString())) {
|
||||
additionalProperties.put("isNg4x", true);
|
||||
setNgVersion("4");
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid ngVersion, which must be either '2' or '4'");
|
||||
}
|
||||
} else {
|
||||
// default to 4
|
||||
additionalProperties.put("isNg4x", true);
|
||||
setNgVersion("4");
|
||||
}
|
||||
}
|
||||
|
||||
private void addNpmPackageGeneration() {
|
||||
if(additionalProperties.containsKey(NPM_NAME)) {
|
||||
this.setNpmName(additionalProperties.get(NPM_NAME).toString());
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(NPM_VERSION)) {
|
||||
this.setNpmVersion(additionalProperties.get(NPM_VERSION).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);
|
||||
|
||||
if (additionalProperties.containsKey(NPM_REPOSITORY)) {
|
||||
this.setNpmRepository(additionalProperties.get(NPM_REPOSITORY).toString());
|
||||
}
|
||||
|
||||
//Files for building our lib
|
||||
supportingFiles.add(new SupportingFile("README.mustache", getIndexDirectory(), "README.md"));
|
||||
supportingFiles.add(new SupportingFile("package.mustache", getIndexDirectory(), "package.json"));
|
||||
supportingFiles.add(new SupportingFile("typings.mustache", getIndexDirectory(), "typings.json"));
|
||||
supportingFiles.add(new SupportingFile("tsconfig.mustache", getIndexDirectory(), "tsconfig.json"));
|
||||
}
|
||||
|
||||
private String getIndexDirectory() {
|
||||
String indexPackage = modelPackage.substring(0, Math.max(0, modelPackage.lastIndexOf('.')));
|
||||
return indexPackage.replace('.', File.separatorChar);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDataTypeFile(final String dataType) {
|
||||
return dataType != null && dataType.equals("Blob");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTypeDeclaration(Property p) {
|
||||
Property inner;
|
||||
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;
|
||||
inner = mp.getAdditionalProperties();
|
||||
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
|
||||
} else if(p instanceof FileProperty) {
|
||||
return "Blob";
|
||||
} else if(p instanceof ObjectProperty) {
|
||||
return "any";
|
||||
} else {
|
||||
return super.getTypeDeclaration(p);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSwaggerType(Property p) {
|
||||
String swaggerType = super.getSwaggerType(p);
|
||||
if(isLanguagePrimitive(swaggerType) || isLanguageGenericType(swaggerType)) {
|
||||
return swaggerType;
|
||||
}
|
||||
applyLocalTypeMapping(swaggerType);
|
||||
return swaggerType;
|
||||
}
|
||||
|
||||
private String applyLocalTypeMapping(String type) {
|
||||
if (typeMapping.containsKey(type)) {
|
||||
type = typeMapping.get(type);
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
private boolean isLanguagePrimitive(String type) {
|
||||
return languageSpecificPrimitives.contains(type);
|
||||
}
|
||||
|
||||
private boolean isLanguageGenericType(String type) {
|
||||
for (String genericType: languageGenericTypes) {
|
||||
if (type.startsWith(genericType + "<")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessParameter(CodegenParameter parameter) {
|
||||
super.postProcessParameter(parameter);
|
||||
parameter.dataType = applyLocalTypeMapping(parameter.dataType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> postProcessOperations(Map<String, Object> operations) {
|
||||
Map<String, Object> objs = (Map<String, Object>) operations.get("operations");
|
||||
|
||||
// Add filename information for api imports
|
||||
objs.put("apiFilename", getApiFilenameFromClassname(objs.get("classname").toString()));
|
||||
|
||||
List<CodegenOperation> ops = (List<CodegenOperation>) objs.get("operation");
|
||||
for (CodegenOperation op : ops) {
|
||||
// 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");
|
||||
}
|
||||
|
||||
// Convert path to TypeScript template string
|
||||
op.path = op.path.replaceAll("\\{(.*?)\\}", "\\$\\{$1\\}");
|
||||
}
|
||||
|
||||
// Add additional filename information for model imports in the services
|
||||
List<Map<String, Object>> imports = (List<Map<String, Object>>) operations.get("imports");
|
||||
for(Map<String, Object> im : imports) {
|
||||
im.put("filename", im.get("import"));
|
||||
im.put("classname", getModelnameFromModelFilename(im.get("filename").toString()));
|
||||
}
|
||||
|
||||
return operations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
|
||||
Map<String, Object> result = super.postProcessModels(objs);
|
||||
|
||||
// Add additional filename information for imports
|
||||
List<Object> models = (List<Object>) postProcessModelsEnum(result).get("models");
|
||||
for (Object _mo : models) {
|
||||
Map<String, Object> mo = (Map<String, Object>) _mo;
|
||||
CodegenModel cm = (CodegenModel) mo.get("model");
|
||||
mo.put("tsImports", toTsImports(cm.imports));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<Map<String, String>> toTsImports(Set<String> imports) {
|
||||
List<Map<String, String>> tsImports = new ArrayList<>();
|
||||
for(String im : imports) {
|
||||
HashMap<String, String> tsImport = new HashMap<>();
|
||||
tsImport.put("classname", im);
|
||||
tsImport.put("filename", toModelFilename(im));
|
||||
tsImports.add(tsImport);
|
||||
}
|
||||
return tsImports;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toApiName(String name) {
|
||||
if (name.length() == 0) {
|
||||
return "DefaultService";
|
||||
}
|
||||
return initialCaps(name) + "Service";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toApiFilename(String name) {
|
||||
if (name.length() == 0) {
|
||||
return "default.service";
|
||||
}
|
||||
return camelize(name, true) + ".service";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toApiImport(String name) {
|
||||
return apiPackage() + "/" + toApiFilename(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toModelFilename(String name) {
|
||||
return camelize(toModelName(name), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toModelImport(String name) {
|
||||
return modelPackage() + "/" + toModelFilename(name);
|
||||
}
|
||||
|
||||
public String getNgVersion() {
|
||||
return ngVersion;
|
||||
}
|
||||
|
||||
public void setNgVersion(String ngVersion) {
|
||||
this.ngVersion = ngVersion;
|
||||
}
|
||||
|
||||
public String getNpmName() {
|
||||
return npmName;
|
||||
}
|
||||
|
||||
public void setNpmName(String npmName) {
|
||||
this.npmName = npmName;
|
||||
}
|
||||
|
||||
public String getNpmVersion() {
|
||||
return npmVersion;
|
||||
}
|
||||
|
||||
public void setNpmVersion(String npmVersion) {
|
||||
this.npmVersion = npmVersion;
|
||||
}
|
||||
|
||||
public String getNpmRepository() {
|
||||
return npmRepository;
|
||||
}
|
||||
|
||||
public void setNpmRepository(String npmRepository) {
|
||||
this.npmRepository = npmRepository;
|
||||
}
|
||||
|
||||
private String getApiFilenameFromClassname(String classname) {
|
||||
String name = classname.substring(0, classname.length() - "Service".length());
|
||||
return toApiFilename(name);
|
||||
}
|
||||
|
||||
private String getModelnameFromModelFilename(String filename) {
|
||||
String name = filename.substring((modelPackage() + "/").length());
|
||||
return camelize(name);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +1,77 @@
|
||||
package io.swagger.codegen.languages;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import io.swagger.codegen.SupportingFile;
|
||||
import io.swagger.codegen.CliOption;
|
||||
import io.swagger.codegen.CodegenModel;
|
||||
import io.swagger.codegen.CodegenParameter;
|
||||
import io.swagger.codegen.CodegenOperation;
|
||||
import io.swagger.codegen.SupportingFile;
|
||||
import io.swagger.models.ModelImpl;
|
||||
import io.swagger.models.properties.ArrayProperty;
|
||||
import io.swagger.models.properties.BooleanProperty;
|
||||
import io.swagger.models.properties.FileProperty;
|
||||
import io.swagger.models.properties.MapProperty;
|
||||
import io.swagger.models.properties.ObjectProperty;
|
||||
import io.swagger.models.properties.Property;
|
||||
|
||||
public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCodegen {
|
||||
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm");
|
||||
|
||||
public static final String NPM_NAME = "npmName";
|
||||
public static final String NPM_VERSION = "npmVersion";
|
||||
public static final String NPM_REPOSITORY = "npmRepository";
|
||||
public static final String SNAPSHOT = "snapshot";
|
||||
public static final String WITH_INTERFACES = "withInterfaces";
|
||||
public static final String NG_VERSION = "ngVersion";
|
||||
|
||||
protected String npmName = null;
|
||||
protected String npmVersion = "1.0.0";
|
||||
protected String npmRepository = null;
|
||||
protected String ngVersion = "4";
|
||||
|
||||
public TypeScriptAngularClientCodegen() {
|
||||
super();
|
||||
this.outputFolder = "generated-code/typescript-angular";
|
||||
|
||||
embeddedTemplateDir = templateDir = "typescript-angular";
|
||||
modelTemplateFiles.put("model.mustache", ".ts");
|
||||
apiTemplateFiles.put("api.service.mustache", ".ts");
|
||||
languageSpecificPrimitives.add("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(NG_VERSION, "The version of Angular (2 or 4). Default is '4'"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, ModelImpl swaggerModel) {
|
||||
codegenModel.additionalPropertiesType = getTypeDeclaration(swaggerModel.getAdditionalProperties());
|
||||
addImport(codegenModel, codegenModel.additionalPropertiesType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "typescript-angularjs";
|
||||
return "typescript-angular";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelp() {
|
||||
return "Generates a TypeScript AngularJS client library.";
|
||||
return "Generates a TypeScript Angular (2.x or 4.x) client library.";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -25,19 +81,94 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
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("git_push.sh.mustache", "", "git_push.sh"));
|
||||
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("gitignore", "", ".gitignore"));
|
||||
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
|
||||
|
||||
if(additionalProperties.containsKey(NPM_NAME)) {
|
||||
addNpmPackageGeneration();
|
||||
}
|
||||
|
||||
if(additionalProperties.containsKey(WITH_INTERFACES)) {
|
||||
boolean withInterfaces = Boolean.parseBoolean(additionalProperties.get(WITH_INTERFACES).toString());
|
||||
if (withInterfaces) {
|
||||
apiTemplateFiles.put("apiInterface.mustache", "Interface.ts");
|
||||
}
|
||||
}
|
||||
|
||||
// determine NG version
|
||||
if (additionalProperties.containsKey(NG_VERSION)) {
|
||||
if ("2".equals(additionalProperties.get(NG_VERSION).toString())) {
|
||||
additionalProperties.put("isNg2x", true);
|
||||
setNgVersion("2");
|
||||
} else if ("4".equals(additionalProperties.get(NG_VERSION).toString())) {
|
||||
additionalProperties.put("isNg4x", true);
|
||||
setNgVersion("4");
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid ngVersion, which must be either '2' or '4'");
|
||||
}
|
||||
} else {
|
||||
// default to 4
|
||||
additionalProperties.put("isNg4x", true);
|
||||
setNgVersion("4");
|
||||
}
|
||||
}
|
||||
|
||||
private void addNpmPackageGeneration() {
|
||||
if(additionalProperties.containsKey(NPM_NAME)) {
|
||||
this.setNpmName(additionalProperties.get(NPM_NAME).toString());
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(NPM_VERSION)) {
|
||||
this.setNpmVersion(additionalProperties.get(NPM_VERSION).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);
|
||||
|
||||
if (additionalProperties.containsKey(NPM_REPOSITORY)) {
|
||||
this.setNpmRepository(additionalProperties.get(NPM_REPOSITORY).toString());
|
||||
}
|
||||
|
||||
//Files for building our lib
|
||||
supportingFiles.add(new SupportingFile("README.mustache", getIndexDirectory(), "README.md"));
|
||||
supportingFiles.add(new SupportingFile("package.mustache", getIndexDirectory(), "package.json"));
|
||||
supportingFiles.add(new SupportingFile("typings.mustache", getIndexDirectory(), "typings.json"));
|
||||
supportingFiles.add(new SupportingFile("tsconfig.mustache", getIndexDirectory(), "tsconfig.json"));
|
||||
}
|
||||
|
||||
private String getIndexDirectory() {
|
||||
String indexPackage = modelPackage.substring(0, Math.max(0, modelPackage.lastIndexOf('.')));
|
||||
return indexPackage.replace('.', File.separatorChar);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDataTypeFile(final String dataType) {
|
||||
return dataType != null && dataType.equals("Blob");
|
||||
}
|
||||
|
||||
public TypeScriptAngularClientCodegen() {
|
||||
super();
|
||||
outputFolder = "generated-code/typescript-angular";
|
||||
modelTemplateFiles.put("model.mustache", ".ts");
|
||||
apiTemplateFiles.put("api.mustache", ".ts");
|
||||
embeddedTemplateDir = templateDir = "typescript-angular";
|
||||
apiPackage = "api";
|
||||
modelPackage = "model";
|
||||
@Override
|
||||
public String getTypeDeclaration(Property p) {
|
||||
Property inner;
|
||||
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;
|
||||
inner = mp.getAdditionalProperties();
|
||||
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
|
||||
} else if(p instanceof FileProperty) {
|
||||
return "Blob";
|
||||
} else if(p instanceof ObjectProperty) {
|
||||
return "any";
|
||||
} else {
|
||||
return super.getTypeDeclaration(p);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -46,30 +177,13 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
if(isLanguagePrimitive(swaggerType) || isLanguageGenericType(swaggerType)) {
|
||||
return swaggerType;
|
||||
}
|
||||
return addModelPrefix(swaggerType);
|
||||
applyLocalTypeMapping(swaggerType);
|
||||
return swaggerType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessParameter(CodegenParameter parameter) {
|
||||
super.postProcessParameter(parameter);
|
||||
parameter.dataType = addModelPrefix(parameter.dataType);
|
||||
}
|
||||
|
||||
private String getIndexDirectory() {
|
||||
String indexPackage = modelPackage.substring(0, Math.max(0, modelPackage.lastIndexOf('.')));
|
||||
return indexPackage.replace('.', File.separatorChar);
|
||||
}
|
||||
|
||||
private String addModelPrefix(String swaggerType) {
|
||||
String type = null;
|
||||
if (typeMapping.containsKey(swaggerType)) {
|
||||
type = typeMapping.get(swaggerType);
|
||||
} else {
|
||||
type = swaggerType;
|
||||
}
|
||||
|
||||
if (!isLanguagePrimitive(type) && !isLanguageGenericType(type)) {
|
||||
type = "models." + swaggerType;
|
||||
private String applyLocalTypeMapping(String type) {
|
||||
if (typeMapping.containsKey(type)) {
|
||||
type = typeMapping.get(type);
|
||||
}
|
||||
return type;
|
||||
}
|
||||
@@ -86,4 +200,161 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessParameter(CodegenParameter parameter) {
|
||||
super.postProcessParameter(parameter);
|
||||
parameter.dataType = applyLocalTypeMapping(parameter.dataType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> postProcessOperations(Map<String, Object> operations) {
|
||||
Map<String, Object> objs = (Map<String, Object>) operations.get("operations");
|
||||
|
||||
// Add filename information for api imports
|
||||
objs.put("apiFilename", getApiFilenameFromClassname(objs.get("classname").toString()));
|
||||
|
||||
List<CodegenOperation> ops = (List<CodegenOperation>) objs.get("operation");
|
||||
for (CodegenOperation op : ops) {
|
||||
// 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");
|
||||
}
|
||||
|
||||
// Convert path to TypeScript template string
|
||||
op.path = op.path.replaceAll("\\{(.*?)\\}", "\\$\\{$1\\}");
|
||||
}
|
||||
|
||||
// Add additional filename information for model imports in the services
|
||||
List<Map<String, Object>> imports = (List<Map<String, Object>>) operations.get("imports");
|
||||
for(Map<String, Object> im : imports) {
|
||||
im.put("filename", im.get("import"));
|
||||
im.put("classname", getModelnameFromModelFilename(im.get("filename").toString()));
|
||||
}
|
||||
|
||||
return operations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
|
||||
Map<String, Object> result = super.postProcessModels(objs);
|
||||
|
||||
// Add additional filename information for imports
|
||||
List<Object> models = (List<Object>) postProcessModelsEnum(result).get("models");
|
||||
for (Object _mo : models) {
|
||||
Map<String, Object> mo = (Map<String, Object>) _mo;
|
||||
CodegenModel cm = (CodegenModel) mo.get("model");
|
||||
mo.put("tsImports", toTsImports(cm.imports));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<Map<String, String>> toTsImports(Set<String> imports) {
|
||||
List<Map<String, String>> tsImports = new ArrayList<>();
|
||||
for(String im : imports) {
|
||||
HashMap<String, String> tsImport = new HashMap<>();
|
||||
tsImport.put("classname", im);
|
||||
tsImport.put("filename", toModelFilename(im));
|
||||
tsImports.add(tsImport);
|
||||
}
|
||||
return tsImports;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toApiName(String name) {
|
||||
if (name.length() == 0) {
|
||||
return "DefaultService";
|
||||
}
|
||||
return initialCaps(name) + "Service";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toApiFilename(String name) {
|
||||
if (name.length() == 0) {
|
||||
return "default.service";
|
||||
}
|
||||
return camelize(name, true) + ".service";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toApiImport(String name) {
|
||||
return apiPackage() + "/" + toApiFilename(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toModelFilename(String name) {
|
||||
return camelize(toModelName(name), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toModelImport(String name) {
|
||||
return modelPackage() + "/" + toModelFilename(name);
|
||||
}
|
||||
|
||||
public String getNgVersion() {
|
||||
return ngVersion;
|
||||
}
|
||||
|
||||
public void setNgVersion(String ngVersion) {
|
||||
this.ngVersion = ngVersion;
|
||||
}
|
||||
|
||||
public String getNpmName() {
|
||||
return npmName;
|
||||
}
|
||||
|
||||
public void setNpmName(String npmName) {
|
||||
this.npmName = npmName;
|
||||
}
|
||||
|
||||
public String getNpmVersion() {
|
||||
return npmVersion;
|
||||
}
|
||||
|
||||
public void setNpmVersion(String npmVersion) {
|
||||
this.npmVersion = npmVersion;
|
||||
}
|
||||
|
||||
public String getNpmRepository() {
|
||||
return npmRepository;
|
||||
}
|
||||
|
||||
public void setNpmRepository(String npmRepository) {
|
||||
this.npmRepository = npmRepository;
|
||||
}
|
||||
|
||||
private String getApiFilenameFromClassname(String classname) {
|
||||
String name = classname.substring(0, classname.length() - "Service".length());
|
||||
return toApiFilename(name);
|
||||
}
|
||||
|
||||
private String getModelnameFromModelFilename(String filename) {
|
||||
String name = filename.substring((modelPackage() + "/").length());
|
||||
return camelize(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package io.swagger.codegen.languages;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import io.swagger.codegen.SupportingFile;
|
||||
import io.swagger.codegen.CodegenParameter;
|
||||
import io.swagger.models.properties.Property;
|
||||
|
||||
public class TypeScriptAngularJsClientCodegen extends AbstractTypeScriptClientCodegen {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "typescript-angularjs";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelp() {
|
||||
return "Generates a TypeScript AngularJS client library.";
|
||||
}
|
||||
|
||||
@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("index.mustache", getIndexDirectory(), "index.ts"));
|
||||
supportingFiles.add(new SupportingFile("api.module.mustache", getIndexDirectory(), "api.module.ts"));
|
||||
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
|
||||
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));
|
||||
|
||||
}
|
||||
|
||||
public TypeScriptAngularJsClientCodegen() {
|
||||
super();
|
||||
outputFolder = "generated-code/typescript-angularjs";
|
||||
modelTemplateFiles.put("model.mustache", ".ts");
|
||||
apiTemplateFiles.put("api.mustache", ".ts");
|
||||
embeddedTemplateDir = templateDir = "typescript-angularjs";
|
||||
apiPackage = "api";
|
||||
modelPackage = "model";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSwaggerType(Property p) {
|
||||
String swaggerType = super.getSwaggerType(p);
|
||||
if(isLanguagePrimitive(swaggerType) || isLanguageGenericType(swaggerType)) {
|
||||
return swaggerType;
|
||||
}
|
||||
return addModelPrefix(swaggerType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessParameter(CodegenParameter parameter) {
|
||||
super.postProcessParameter(parameter);
|
||||
parameter.dataType = addModelPrefix(parameter.dataType);
|
||||
}
|
||||
|
||||
private String getIndexDirectory() {
|
||||
String indexPackage = modelPackage.substring(0, Math.max(0, modelPackage.lastIndexOf('.')));
|
||||
return indexPackage.replace('.', File.separatorChar);
|
||||
}
|
||||
|
||||
private String addModelPrefix(String swaggerType) {
|
||||
String type = null;
|
||||
if (typeMapping.containsKey(swaggerType)) {
|
||||
type = typeMapping.get(swaggerType);
|
||||
} else {
|
||||
type = swaggerType;
|
||||
}
|
||||
|
||||
if (!isLanguagePrimitive(type) && !isLanguageGenericType(type)) {
|
||||
type = "models." + swaggerType;
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
private boolean isLanguagePrimitive(String type) {
|
||||
return languageSpecificPrimitives.contains(type);
|
||||
}
|
||||
|
||||
private boolean isLanguageGenericType(String type) {
|
||||
for (String genericType: languageGenericTypes) {
|
||||
if (type.startsWith(genericType + "<")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user