Merge branch 'master' into issue-5753

This commit is contained in:
Tony Tam
2017-06-26 13:15:00 -07:00
committed by GitHub
301 changed files with 12866 additions and 8412 deletions

View File

@@ -2410,7 +2410,12 @@ public class DefaultCodegen {
p.baseType = pr.datatype;
p.isContainer = true;
p.isListContainer = true;
imports.add(pr.baseType);
// recursively add import
while (pr != null) {
imports.add(pr.baseType);
pr = pr.items;
}
} else if ("object".equals(type)) { // for map parameter
Property inner = qp.getItems();
if (inner == null) {
@@ -2424,7 +2429,11 @@ public class DefaultCodegen {
p.baseType = pr.datatype;
p.isContainer = true;
p.isMapContainer = true;
imports.add(pr.baseType);
// recursively add import
while (pr != null) {
imports.add(pr.baseType);
pr = pr.items;
}
} else {
Map<PropertyId, Object> args = new HashMap<PropertyId, Object>();
String format = qp.getFormat();
@@ -2545,6 +2554,14 @@ public class DefaultCodegen {
imports.add(cp.complexType);
}
imports.add(cp.baseType);
// recursively add import
CodegenProperty innerCp = cp;
while(innerCp != null) {
imports.add(innerCp.complexType);
innerCp = innerCp.items;
}
p.items = cp;
p.dataType = cp.datatype;
p.baseType = cp.complexType;

View File

@@ -65,7 +65,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
supportedLibraries.put("jersey1", "HTTP client: Jersey client 1.19.1. JSON processing: Jackson 2.7.0. Enable Java6 support using '-DsupportJava6=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
supportedLibraries.put("feign", "HTTP client: OpenFeign 9.4.0. JSON processing: Jackson 2.8.7");
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.22.2. JSON processing: Jackson 2.7.0");
supportedLibraries.put("okhttp-gson", "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.6.2. Enable Parcelable modles on Android using '-DparcelableModel=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
supportedLibraries.put("okhttp-gson", "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.6.2. Enable Parcelable models on Android using '-DparcelableModel=true'. Enable gzip request encoding using '-DuseGzipFeature=true'.");
supportedLibraries.put(RETROFIT_1, "HTTP client: OkHttp 2.7.5. JSON processing: Gson 2.3.1 (Retrofit 1.9.0). IMPORTANT NOTE: retrofit1.x is no longer actively maintained so please upgrade to 'retrofit2' instead.");
supportedLibraries.put(RETROFIT_2, "HTTP client: OkHttp 3.2.0. JSON processing: Gson 2.6.1 (Retrofit 2.0.2). Enable the RxJava adapter using '-DuseRxJava[2]=true'. (RxJava 1.x or 2.x)");
supportedLibraries.put("resttemplate", "HTTP client: Spring RestTemplate 4.3.7-RELEASE. JSON processing: Jackson 2.8.8");

View File

@@ -96,7 +96,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
protected String modelDocPath = "docs/";
protected String apiTestPath = "api/";
protected String modelTestPath = "model/";
protected boolean useES6;
protected boolean useES6 = false; // default is ES5
public JavascriptClientCodegen() {
super();
@@ -105,7 +105,8 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
modelTestTemplateFiles.put("model_test.mustache", ".js");
apiTemplateFiles.put("api.mustache", ".js");
apiTestTemplateFiles.put("api_test.mustache", ".js");
embeddedTemplateDir = templateDir = "Javascript";
// subfolder Javascript/es6
embeddedTemplateDir = templateDir = "Javascript" + File.separator + "es6";
apiPackage = "api";
modelPackage = "model";
modelDocTemplateFiles.put("model_doc.mustache", ".md");
@@ -196,7 +197,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
.defaultValue(Boolean.TRUE.toString()));
cliOptions.add(new CliOption(USE_ES6,
"use JavaScript ES6 (ECMAScript 6)")
.defaultValue(Boolean.TRUE.toString()));
.defaultValue(Boolean.FALSE.toString()));
}
@Override
@@ -267,6 +268,8 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
}
if (additionalProperties.containsKey(USE_ES6)) {
setUseES6(convertPropertyToBooleanAndWriteBack(USE_ES6));
} else {
setUseES6(false);
}
}
@@ -430,9 +433,11 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
public void setUseES6(boolean useES6) {
this.useES6 = useES6;
if (useES6) {
embeddedTemplateDir = templateDir = "Javascript-es6";
embeddedTemplateDir = templateDir = "Javascript/es6";
LOGGER.info("Using JS ES6 templates");
} else {
embeddedTemplateDir = templateDir = "Javascript";
LOGGER.info("Using JS ES5 templates");
}
}

View File

@@ -0,0 +1,405 @@
package io.swagger.codegen.languages;
import io.swagger.codegen.*;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import static java.util.UUID.randomUUID;
public class PowerShellClientCodegen extends DefaultCodegen implements CodegenConfig {
static Logger LOGGER = LoggerFactory.getLogger(PowerShellClientCodegen.class);
private String packageGuid = "{" + randomUUID().toString().toUpperCase() + "}";
protected String sourceFolder = "src";
protected String packageName = "IO.Swagger";
protected String csharpClientPath = "$ScriptDir\\csharp\\SwaggerClient";
protected String apiDocPath = "docs/";
protected String modelDocPath = "docs/";
/**
* Constructs an instance of `PowerShellClientCodegen`.
*/
public PowerShellClientCodegen() {
super();
outputFolder = "generated-code" + File.separator + "powershell";
modelTemplateFiles.put("model.mustache", ".ps1");
apiTemplateFiles.put("api.mustache", ".ps1");
modelTestTemplateFiles.put("model_test.mustache", ".ps1");
apiTestTemplateFiles.put("api_test.mustache", ".ps1");
modelDocTemplateFiles.clear();
apiDocTemplateFiles.clear();
embeddedTemplateDir = templateDir = "powershell";
apiPackage = packageName + File.separator + "API";
modelPackage = packageName + File.separator + "Model";
// https://blogs.msdn.microsoft.com/powershell/2010/01/07/how-objects-are-sent-to-and-from-remote-sessions/
languageSpecificPrimitives = new HashSet<String>(Arrays.asList(
"Byte",
"SByte",
"Byte[]",
"Int16",
"Int32",
"Int64",
"UInt16",
"UInt32",
"UInt64",
"Decimal",
"Single",
"Double",
"TimeSpan",
"System.DateTime",
"ProgressRecord",
"Char",
"String",
"XmlDocument",
"SecureString",
"Boolean",
"Guid",
"Uri",
"Version"
));
// https://richardspowershellblog.wordpress.com/2009/05/02/powershell-reserved-words/
reservedWords = new HashSet<String>(Arrays.asList(
"Begin",
"Break",
"Catch",
"Continue",
"Data",
"Do",
"Dynamicparam",
"Else",
"Elseif",
"End",
"Exit",
"Filter",
"Finally",
"For",
"Foreach",
"From",
"Function",
"If",
"In",
"Param",
"Process",
"Return",
"Switch",
"Throw",
"Trap",
"Try",
"Until",
"While",
"Local",
"Private",
"Where"
));
defaultIncludes = new HashSet<String>(Arrays.asList(
"Byte",
"SByte",
"Byte[]",
"Int16",
"Int32",
"Int64",
"UInt16",
"UInt32",
"UInt64",
"Decimal",
"Single",
"Double",
"TimeSpan",
"System.DateTime",
"ProgressRecord",
"Char",
"String",
"XmlDocument",
"SecureString",
"Boolean",
"Guid",
"Uri",
"Version"
));
typeMapping = new HashMap<String, String>();
typeMapping.put("string", "String");
typeMapping.put("boolean", "Boolean");
typeMapping.put("integer", "Int32");
typeMapping.put("float", "Double");
typeMapping.put("long", "Int64");
typeMapping.put("double", "Double");
typeMapping.put("number", "Decimal");
typeMapping.put("date-time", "System.DateTime");
typeMapping.put("date", "System.DateTime");
typeMapping.put("file", "String");
typeMapping.put("object", "String");
typeMapping.put("binary", "String");
typeMapping.put("Date", "System.DateTime");
typeMapping.put("DateTime", "System.DateTime");
cliOptions.clear();
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "Client package name (e.g. io.swagger.client).").defaultValue(this.packageName));
cliOptions.add(new CliOption(CodegenConstants.OPTIONAL_PROJECT_GUID, "GUID for PowerShell module (e.g. a27b908d-2a20-467f-bc32-af6f3a654ac5). A random GUID will be generated by default."));
cliOptions.add(new CliOption("csharpClientPath", "Path to the C# API client generated by Swagger Codegen, e.g. $ScriptDir\\..\\csharp\\SwaggerClient where $ScriptDir is the current directory.").defaultValue(this.csharpClientPath));
}
public CodegenType getTag() {
return CodegenType.CLIENT;
}
public String getName() {
return "powershell";
}
public String getHelp() {
return "Generates a PowerShell API client (beta).";
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public void setCsharpClientPath(String csharpClientPath) {
this.csharpClientPath = csharpClientPath;
}
public void setSourceFolder(String sourceFolder) {
this.sourceFolder = sourceFolder;
}
public void setPackageGuid(String packageGuid) {
this.packageGuid = packageGuid;
}
@Override
public void processOpts() {
super.processOpts();
if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_PROJECT_GUID)) {
setPackageGuid((String) additionalProperties.get(CodegenConstants.OPTIONAL_PROJECT_GUID));
}
additionalProperties.put("packageGuid", packageGuid);
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
this.setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
} else {
additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName);
}
if (additionalProperties.containsKey("csharpClientPath")) {
this.setCsharpClientPath((String) additionalProperties.get("csharpClientPath"));
} else {
additionalProperties.put("csharpClientPath", csharpClientPath);
}
if (additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) {
LOGGER.warn(CodegenConstants.MODEL_PACKAGE + " with " + this.getName() + " generator is ignored. Setting this value independently of " + CodegenConstants.PACKAGE_NAME + " is not currently supported.");
}
if (additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) {
LOGGER.warn(CodegenConstants.API_PACKAGE + " with " + this.getName() + " generator is ignored. Setting this value independently of " + CodegenConstants.PACKAGE_NAME + " is not currently supported.");
}
additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage());
additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage());
additionalProperties.put("apiDocPath", apiDocPath);
additionalProperties.put("modelDocPath", modelDocPath);
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("Build.ps1.mustache", "", "Build.ps1"));
final String infrastructureFolder = (sourceFolder + File.separator + packageName + File.separator);
supportingFiles.add(new SupportingFile("IO.Swagger.psm1.mustache", infrastructureFolder, packageName + ".psm1"));
// private
supportingFiles.add(new SupportingFile("Get-CommonParameters.ps1", infrastructureFolder + File.separator + "Private" + File.separator, "Get-CommonParameters.ps1"));
supportingFiles.add(new SupportingFile("Out-DebugParameter.ps1", infrastructureFolder + File.separator + "Private" + File.separator, "Out-DebugParameter.ps1"));
// en-US
supportingFiles.add(new SupportingFile("about_IO.Swagger.help.txt.mustache", infrastructureFolder + File.separator + "en-US" + File.separator + "about_" + packageName + ".help.txt"));
}
@Override
public String escapeUnsafeCharacters(String input) {
return input.replace("#>", "#_>").replace("<#", "<_#");
}
@Override
public String escapeQuotationMark(String input) {
// remove " to avoid code injection
return input.replace("\"", "");
}
@Override
public String apiTestFileFolder() {
return (outputFolder + "/test").replace('/', File.separatorChar);
}
@Override
public String apiDocFileFolder() {
return (outputFolder + "/" + apiDocPath).replace('/', File.separatorChar);
}
@Override
public String apiFileFolder() {
return outputFolder + File.separator + sourceFolder + File.separator + apiPackage();
}
@Override
public String modelTestFileFolder() {
return (outputFolder + "/test").replace('/', File.separatorChar);
}
@Override
public String modelDocFileFolder() {
return (outputFolder + "/" + modelDocPath).replace('/', File.separatorChar);
}
@Override
public String modelFileFolder() {
return outputFolder + File.separator + sourceFolder + File.separator + modelPackage();
}
@Override
public String escapeReservedWord(String name) {
return "_" + name;
}
/**
* Output the proper model name (capitalized).
* In case the name belongs to the TypeSystem it won't be renamed.
*
* @param name the name of the model
* @return capitalized model name
*/
@Override
public String toModelName(String name) {
if (!StringUtils.isEmpty(modelNamePrefix)) {
name = modelNamePrefix + "_" + name;
}
if (!StringUtils.isEmpty(modelNameSuffix)) {
name = name + "_" + modelNameSuffix;
}
name = sanitizeName(name);
// model name cannot use reserved keyword, e.g. return
if (isReservedWord(name)) {
LOGGER.warn(name + " (reserved word) cannot be used as model name. Renamed to " + camelize("model_" + name));
name = "model_" + name; // e.g. return => ModelReturn (after camelize)
}
// model name starts with number
if (name.matches("^\\d.*")) {
LOGGER.warn(name + " (model name starts with number) cannot be used as model name. Renamed to " + camelize("model_" + name));
name = "model_" + name; // e.g. 200Response => Model200Response (after camelize)
}
// camelize the model name
// phone_number => PhoneNumber
return camelize(name);
}
@Override
public String toModelFilename(String name) {
// should be the same as the model name
return "New-" + toModelName(name);
}
/**
* returns the swagger type for the property
*
* @param p Swagger property object
* @return string presentation of the type
**/
@Override
public String getSwaggerType(Property p) {
String swaggerType = super.getSwaggerType(p);
String type;
// This maps, for example, long -> Long based on hashes in this type's constructor
if (typeMapping.containsKey(swaggerType)) {
type = typeMapping.get(swaggerType);
if (languageSpecificPrimitives.contains(type)) {
return type;
}
} else {
type = swaggerType;
}
// model/object
return toModelName(type);
}
/**
* Output the type declaration of the property
*
* @param p Swagger Property object
* @return a string presentation of the property type
*/
@Override
public String getTypeDeclaration(Property p) {
if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
Property inner = ap.getItems();
return getTypeDeclaration(inner) + "[]";
} else if (p instanceof MapProperty) {
MapProperty mp = (MapProperty) p;
Property inner = mp.getAdditionalProperties();
// TODO not sure if the following map/hash declaration is correct
return "{String, " + getTypeDeclaration(inner) + "}";
} else if (!languageSpecificPrimitives.contains(getSwaggerType(p))) {
return packageName + ".Model." + super.getTypeDeclaration(p);
}
return super.getTypeDeclaration(p);
}
@Override
public String toOperationId(String operationId) {
// throw exception if method name is empty (should not occur as an auto-generated method name will be used)
if (StringUtils.isEmpty(operationId)) {
throw new RuntimeException("Empty method name (operationId) not allowed");
}
// method name cannot use reserved keyword, e.g. return
if (isReservedWord(operationId)) {
LOGGER.warn(operationId + " (reserved word) cannot be used as method name. Renamed to " + camelize(sanitizeName("call_" + operationId)));
operationId = "call_" + operationId;
}
return camelize(sanitizeName(operationId));
}
@Override
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation op : operationList) {
int index = 0;
for (CodegenParameter p : op.allParams) {
p.vendorExtensions.put("x-index", index);
index++;
}
}
return objs;
}
}

View File

@@ -26,14 +26,11 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
public static final String NPM_VERSION = "npmVersion";
public static final String NPM_REPOSITORY = "npmRepository";
public static final String SNAPSHOT = "snapshot";
public static final String USE_OPAQUE_TOKEN = "useOpaqueToken";
public static final String INJECTION_TOKEN = "injectionToken";
public static final String WITH_INTERFACES = "withInterfaces";
protected String npmName = null;
protected String npmVersion = "1.0.0";
protected String npmRepository = null;
protected String injectionToken = "InjectionToken<string>";
public TypeScriptAngular2ClientCodegen() {
super();
@@ -51,7 +48,6 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
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(USE_OPAQUE_TOKEN, "When setting this property to true, OpaqueToken is used instead of InjectionToken", 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()));
}
@@ -86,11 +82,6 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
addNpmPackageGeneration();
}
if(additionalProperties.containsKey(USE_OPAQUE_TOKEN) && Boolean.valueOf(additionalProperties.get(USE_OPAQUE_TOKEN).toString())) {
this.setOpaqueToken();
}
additionalProperties.put(INJECTION_TOKEN, this.injectionToken);
if(additionalProperties.containsKey(WITH_INTERFACES)) {
boolean withInterfaces = Boolean.parseBoolean(additionalProperties.get(WITH_INTERFACES).toString());
if (withInterfaces) {
@@ -253,7 +244,4 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
this.npmRepository = npmRepository;
}
public void setOpaqueToken() {
this.injectionToken = "OpaqueToken";
}
}