forked from loafle/openapi-generator-original
Merge remote-tracking branch 'origin/master' into feature/allow-overriding-of-only-some-templates-v2
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package io.swagger.codegen;
|
||||
|
||||
import config.Config;
|
||||
import config.ConfigParser;
|
||||
import io.swagger.models.Swagger;
|
||||
import io.swagger.parser.SwaggerParser;
|
||||
import org.apache.commons.cli.BasicParser;
|
||||
@@ -9,6 +11,7 @@ import org.apache.commons.cli.HelpFormatter;
|
||||
import org.apache.commons.cli.Options;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@@ -41,6 +44,7 @@ public class Codegen extends DefaultGenerator {
|
||||
options.addOption("t", "template-dir", true, "folder containing the template files");
|
||||
options.addOption("d", "debug-info", false, "prints additional info for debugging");
|
||||
options.addOption("a", "auth", true, "adds authorization headers when fetching the swagger definitions remotely. Pass in a URL-encoded string of name:header with a comma separating multiple values");
|
||||
options.addOption("c", "config", true, "location of the configuration file");
|
||||
|
||||
ClientOptInput clientOptInput = new ClientOptInput();
|
||||
ClientOpts clientOpts = new ClientOpts();
|
||||
@@ -84,6 +88,18 @@ public class Codegen extends DefaultGenerator {
|
||||
if (cmd.hasOption("i")) {
|
||||
swagger = new SwaggerParser().read(cmd.getOptionValue("i"), clientOptInput.getAuthorizationValues(), true);
|
||||
}
|
||||
if (cmd.hasOption("c")) {
|
||||
String configFile = cmd.getOptionValue("c");
|
||||
Config genConfig = ConfigParser.read(configFile);
|
||||
config = clientOptInput.getConfig();
|
||||
if (null != genConfig && null != config) {
|
||||
for (CliOption langCliOption : config.cliOptions()) {
|
||||
if (genConfig.hasOption(langCliOption.getOpt())) {
|
||||
config.additionalProperties().put(langCliOption.getOpt(), genConfig.getOption(langCliOption.getOpt()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cmd.hasOption("t")) {
|
||||
clientOpts.getProperties().put(CodegenConstants.TEMPLATE_DIR, String.valueOf(cmd.getOptionValue("t")));
|
||||
}
|
||||
@@ -145,4 +161,4 @@ public class Codegen extends DefaultGenerator {
|
||||
configString = sb.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +69,8 @@ public interface CodegenConfig {
|
||||
|
||||
CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions);
|
||||
|
||||
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger);
|
||||
|
||||
CodegenOperation fromOperation(String resourcePath, String httpMethod, Operation operation, Map<String, Model> definitions);
|
||||
|
||||
List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes);
|
||||
|
||||
@@ -40,4 +40,6 @@ public class CodegenConstants {
|
||||
public static final String SORT_PARAMS_BY_REQUIRED_FLAG = "sortParamsByRequiredFlag";
|
||||
public static final String SORT_PARAMS_BY_REQUIRED_FLAG_DESC = "Sort method arguments to place required parameters before optional parameters. Default: true";
|
||||
|
||||
public static final String PACKAGE_NAME = "packageName";
|
||||
public static final String PACKAGE_VERSION = "packageVersion";
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import java.util.Set;
|
||||
|
||||
public class CodegenOperation {
|
||||
public final List<CodegenProperty> responseHeaders = new ArrayList<CodegenProperty>();
|
||||
public Boolean hasConsumes, hasProduces, hasParams, returnTypeIsPrimitive,
|
||||
public Boolean hasAuthMethods, hasConsumes, hasProduces, hasParams, returnTypeIsPrimitive,
|
||||
returnSimpleType, subresourceOperation, isMapContainer, isListContainer,
|
||||
hasMore = Boolean.TRUE, isMultipart, isResponseBinary = Boolean.FALSE;
|
||||
public String path, operationId, returnType, httpMethod, returnBaseType,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.swagger.codegen;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class CodegenSecurity {
|
||||
public String name;
|
||||
@@ -11,5 +12,5 @@ public class CodegenSecurity {
|
||||
public Boolean isKeyInQuery, isKeyInHeader;
|
||||
// Oauth specific
|
||||
public String flow, authorizationUrl, tokenUrl;
|
||||
public Set<String> scopes;
|
||||
public List<Map<String, Object>> scopes;
|
||||
}
|
||||
|
||||
@@ -106,7 +106,8 @@ public class DefaultCodegen {
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG)) {
|
||||
this.setSortParamsByRequiredFlag(Boolean.valueOf((String)additionalProperties.get(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG).toString()));
|
||||
this.setSortParamsByRequiredFlag(Boolean.valueOf(additionalProperties
|
||||
.get(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG).toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,18 +245,42 @@ public class DefaultCodegen {
|
||||
this.sortParamsByRequiredFlag = sortParamsByRequiredFlag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the file name of the Api
|
||||
*
|
||||
* @param name the file name of the Api
|
||||
* @return the file name of the Api
|
||||
*/
|
||||
public String toApiFilename(String name) {
|
||||
return toApiName(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the variable name in the Api
|
||||
*
|
||||
* @param name the varible name of the Api
|
||||
* @return the snake-cased variable name
|
||||
*/
|
||||
public String toApiVarName(String name) {
|
||||
return snakeCase(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the capitalized file name of the model
|
||||
*
|
||||
* @param name the model name
|
||||
* @return the file name of the model
|
||||
*/
|
||||
public String toModelFilename(String name) {
|
||||
return initialCaps(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the operation ID (method name)
|
||||
*
|
||||
* @param operationId operation ID
|
||||
* @return the sanitized method name
|
||||
*/
|
||||
public String toOperationId(String operationId) {
|
||||
// throw exception if method name is empty
|
||||
if (StringUtils.isEmpty(operationId)) {
|
||||
@@ -265,6 +290,13 @@ public class DefaultCodegen {
|
||||
return operationId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the variable name by removing invalid characters and proper escaping if
|
||||
* it's a reserved word.
|
||||
*
|
||||
* @param name the variable name
|
||||
* @return the sanitized variable name
|
||||
*/
|
||||
public String toVarName(String name) {
|
||||
if (reservedWords.contains(name)) {
|
||||
return escapeReservedWord(name);
|
||||
@@ -273,6 +305,13 @@ public class DefaultCodegen {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the parameter name by removing invalid characters and proper escaping if
|
||||
* it's a reserved word.
|
||||
*
|
||||
* @param property Codegen property object
|
||||
* @return the sanitized parameter name
|
||||
*/
|
||||
public String toParamName(String name) {
|
||||
name = removeNonNameElementToCamelCase(name);
|
||||
if (reservedWords.contains(name)) {
|
||||
@@ -281,14 +320,33 @@ public class DefaultCodegen {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Enum name (e.g. StatusEnum given 'status')
|
||||
*
|
||||
* @param property Codegen property object
|
||||
* @return the Enum name
|
||||
*/
|
||||
public String toEnumName(CodegenProperty property) {
|
||||
return StringUtils.capitalize(property.name) + "Enum";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the escaped name of the reserved word
|
||||
*
|
||||
* @param name the name to be escaped
|
||||
* @throws Runtime exception as reserved word is not allowed (default behavior)
|
||||
* @return the escaped reserved word
|
||||
*/
|
||||
public String escapeReservedWord(String name) {
|
||||
throw new RuntimeException("reserved word " + name + " not allowed");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the fully-qualified "Model" name for import
|
||||
*
|
||||
* @param name the name of the "Model"
|
||||
* @return the fully-qualified "Model" name for import
|
||||
*/
|
||||
public String toModelImport(String name) {
|
||||
if ("".equals(modelPackage())) {
|
||||
return name;
|
||||
@@ -297,10 +355,26 @@ public class DefaultCodegen {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the fully-qualified "Api" name for import
|
||||
*
|
||||
* @param name the name of the "Api"
|
||||
* @return the fully-qualified "Api" name for import
|
||||
*/
|
||||
public String toApiImport(String name) {
|
||||
return apiPackage() + "." + name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
* This method will map between Swagger type and language-specified type, as well as mapping
|
||||
* between Swagger type and the corresponding import statement for the language. This will
|
||||
* also add some language specified CLI options, if any.
|
||||
*
|
||||
* @param path the path of the operation
|
||||
* @param operation Swagger operation object
|
||||
* @return string presentation of the example path
|
||||
*/
|
||||
public DefaultCodegen() {
|
||||
defaultIncludes = new HashSet<String>(
|
||||
Arrays.asList("double",
|
||||
@@ -364,7 +438,13 @@ public class DefaultCodegen {
|
||||
cliOptions.add(new CliOption(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG_DESC));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the example path
|
||||
*
|
||||
* @param path the path of the operation
|
||||
* @param operation Swagger operation object
|
||||
* @return string presentation of the example path
|
||||
*/
|
||||
public String generateExamplePath(String path, Operation operation) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(path);
|
||||
@@ -415,6 +495,12 @@ public class DefaultCodegen {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the instantiation type of the property, especially for map and array
|
||||
*
|
||||
* @param p Swagger property object
|
||||
* @return string presentation of the instantiation type of the property
|
||||
*/
|
||||
public String toInstantiationType(Property p) {
|
||||
if (p instanceof MapProperty) {
|
||||
MapProperty ap = (MapProperty) p;
|
||||
@@ -435,6 +521,12 @@ public class DefaultCodegen {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the default value of the property
|
||||
*
|
||||
* @param p Swagger property object
|
||||
* @return string presentation of the default value of the property
|
||||
*/
|
||||
public String toDefaultValue(Property p) {
|
||||
if (p instanceof StringProperty) {
|
||||
return "null";
|
||||
@@ -475,6 +567,8 @@ public class DefaultCodegen {
|
||||
|
||||
/**
|
||||
* returns the swagger type for the property
|
||||
* @param p Swagger property object
|
||||
* @return string presentation of the type
|
||||
**/
|
||||
public String getSwaggerType(Property p) {
|
||||
String datatype = null;
|
||||
@@ -520,18 +614,42 @@ public class DefaultCodegen {
|
||||
return datatype;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the snake-case of the string
|
||||
*
|
||||
* @param name string to be snake-cased
|
||||
* @return snake-cased string
|
||||
*/
|
||||
public String snakeCase(String name) {
|
||||
return (name.length() > 0) ? (Character.toLowerCase(name.charAt(0)) + name.substring(1)) : "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Capitalize the string
|
||||
*
|
||||
* @param name string to be capitalized
|
||||
* @return capitalized string
|
||||
*/
|
||||
public String initialCaps(String name) {
|
||||
return StringUtils.capitalize(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the type declaration of a given name
|
||||
*
|
||||
* @param name name
|
||||
* @return a string presentation of the type
|
||||
*/
|
||||
public String getTypeDeclaration(String name) {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the type declaration of the property
|
||||
*
|
||||
* @param p Swagger Property object
|
||||
* @return a string presentation of the property type
|
||||
*/
|
||||
public String getTypeDeclaration(Property p) {
|
||||
String swaggerType = getSwaggerType(p);
|
||||
if (typeMapping.containsKey(swaggerType)) {
|
||||
@@ -540,6 +658,13 @@ public class DefaultCodegen {
|
||||
return swaggerType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the API (class) name (capitalized) ending with "Api"
|
||||
* Return DefaultApi if name is empty
|
||||
*
|
||||
* @param name the name of the Api
|
||||
* @return capitalized Api name ending with "Api"
|
||||
*/
|
||||
public String toApiName(String name) {
|
||||
if (name.length() == 0) {
|
||||
return "DefaultApi";
|
||||
@@ -547,14 +672,35 @@ public class DefaultCodegen {
|
||||
return initialCaps(name) + "Api";
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the proper model name (capitalized)
|
||||
*
|
||||
* @param name the name of the model
|
||||
* @return capitalized model name
|
||||
*/
|
||||
public String toModelName(String name) {
|
||||
return initialCaps(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Swagger Model object to Codegen Model object without providing all model definitions
|
||||
*
|
||||
* @param name the name of the model
|
||||
* @param model Swagger Model object
|
||||
* @return Codegen Model object
|
||||
*/
|
||||
public CodegenModel fromModel(String name, Model model) {
|
||||
return fromModel(name, model, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Swagger Model object to Codegen Model object
|
||||
*
|
||||
* @param name the name of the model
|
||||
* @param model Swagger Model object
|
||||
* @param allDefinitions a map of all Swagger models from the spec
|
||||
* @return Codegen Model object
|
||||
*/
|
||||
public CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions) {
|
||||
CodegenModel m = CodegenModelFactory.newInstance(CodegenModelType.MODEL);
|
||||
if (reservedWords.contains(name)) {
|
||||
@@ -641,6 +787,12 @@ public class DefaultCodegen {
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Camelize the method name of the getter and setter
|
||||
*
|
||||
* @param name string to be camelized
|
||||
* @return Camelized string
|
||||
*/
|
||||
public String getterAndSetterCapitalize(String name) {
|
||||
if (name == null || name.length() == 0) {
|
||||
return name;
|
||||
@@ -649,7 +801,14 @@ public class DefaultCodegen {
|
||||
return camelize(toVarName(name));
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert Swagger Property object to Codegen Property object
|
||||
*
|
||||
* @param name name of the property
|
||||
* @param p Swagger property object
|
||||
* @return Codegen Property object
|
||||
*/
|
||||
public CodegenProperty fromProperty(String name, Property p) {
|
||||
if (p == null) {
|
||||
LOGGER.error("unexpected missing property for name " + name);
|
||||
@@ -705,9 +864,10 @@ public class DefaultCodegen {
|
||||
property.allowableValues = allowableValues;
|
||||
}
|
||||
}
|
||||
if(p instanceof IntegerProperty) {
|
||||
|
||||
if (p instanceof IntegerProperty) {
|
||||
IntegerProperty sp = (IntegerProperty) p;
|
||||
if(sp.getEnum() != null) {
|
||||
if (sp.getEnum() != null) {
|
||||
List<Integer> _enum = sp.getEnum();
|
||||
property._enum = new ArrayList<String>();
|
||||
for(Integer i : _enum) {
|
||||
@@ -721,9 +881,10 @@ public class DefaultCodegen {
|
||||
property.allowableValues = allowableValues;
|
||||
}
|
||||
}
|
||||
if(p instanceof LongProperty) {
|
||||
|
||||
if (p instanceof LongProperty) {
|
||||
LongProperty sp = (LongProperty) p;
|
||||
if(sp.getEnum() != null) {
|
||||
if (sp.getEnum() != null) {
|
||||
List<Long> _enum = sp.getEnum();
|
||||
property._enum = new ArrayList<String>();
|
||||
for(Long i : _enum) {
|
||||
@@ -737,9 +898,10 @@ public class DefaultCodegen {
|
||||
property.allowableValues = allowableValues;
|
||||
}
|
||||
}
|
||||
if(p instanceof DoubleProperty) {
|
||||
|
||||
if (p instanceof DoubleProperty) {
|
||||
DoubleProperty sp = (DoubleProperty) p;
|
||||
if(sp.getEnum() != null) {
|
||||
if (sp.getEnum() != null) {
|
||||
List<Double> _enum = sp.getEnum();
|
||||
property._enum = new ArrayList<String>();
|
||||
for(Double i : _enum) {
|
||||
@@ -753,9 +915,10 @@ public class DefaultCodegen {
|
||||
property.allowableValues = allowableValues;
|
||||
}
|
||||
}
|
||||
if(p instanceof FloatProperty) {
|
||||
|
||||
if (p instanceof FloatProperty) {
|
||||
FloatProperty sp = (FloatProperty) p;
|
||||
if(sp.getEnum() != null) {
|
||||
if (sp.getEnum() != null) {
|
||||
List<Float> _enum = sp.getEnum();
|
||||
property._enum = new ArrayList<String>();
|
||||
for(Float i : _enum) {
|
||||
@@ -769,9 +932,10 @@ public class DefaultCodegen {
|
||||
property.allowableValues = allowableValues;
|
||||
}
|
||||
}
|
||||
if(p instanceof DateProperty) {
|
||||
|
||||
if (p instanceof DateProperty) {
|
||||
DateProperty sp = (DateProperty) p;
|
||||
if(sp.getEnum() != null) {
|
||||
if (sp.getEnum() != null) {
|
||||
List<String> _enum = sp.getEnum();
|
||||
property._enum = new ArrayList<String>();
|
||||
for(String i : _enum) {
|
||||
@@ -785,9 +949,10 @@ public class DefaultCodegen {
|
||||
property.allowableValues = allowableValues;
|
||||
}
|
||||
}
|
||||
if(p instanceof DateTimeProperty) {
|
||||
|
||||
if (p instanceof DateTimeProperty) {
|
||||
DateTimeProperty sp = (DateTimeProperty) p;
|
||||
if(sp.getEnum() != null) {
|
||||
if (sp.getEnum() != null) {
|
||||
List<String> _enum = sp.getEnum();
|
||||
property._enum = new ArrayList<String>();
|
||||
for(String i : _enum) {
|
||||
@@ -876,8 +1041,31 @@ public class DefaultCodegen {
|
||||
}
|
||||
return responses.get(code);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert Swagger Operation object to Codegen Operation object (without providing a Swagger object)
|
||||
*
|
||||
* @param path the path of the operation
|
||||
* @param httpMethod HTTP method
|
||||
* @param operation Swagger operation object
|
||||
* @param definitions a map of Swagger models
|
||||
* @return Codegen Operation object
|
||||
*/
|
||||
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
|
||||
return fromOperation(path, httpMethod, operation, definitions, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Swagger Operation object to Codegen Operation object
|
||||
*
|
||||
* @param path the path of the operation
|
||||
* @param httpMethod HTTP method
|
||||
* @param operation Swagger operation object
|
||||
* @param definitions a map of Swagger models
|
||||
* @param swagger a Swagger object representing the spec
|
||||
* @return Codegen Operation object
|
||||
*/
|
||||
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
|
||||
CodegenOperation op = CodegenModelFactory.newInstance(CodegenModelType.OPERATION);
|
||||
Set<String> imports = new HashSet<String>();
|
||||
op.vendorExtensions = operation.getVendorExtensions();
|
||||
@@ -913,15 +1101,32 @@ public class DefaultCodegen {
|
||||
op.summary = escapeText(operation.getSummary());
|
||||
op.notes = escapeText(operation.getDescription());
|
||||
op.tags = operation.getTags();
|
||||
op.hasConsumes = false;
|
||||
op.hasProduces = false;
|
||||
|
||||
if (operation.getConsumes() != null && operation.getConsumes().size() > 0) {
|
||||
List<String> consumes = new ArrayList<String>();
|
||||
if (operation.getConsumes() != null) {
|
||||
if (operation.getConsumes().size() > 0) {
|
||||
// use consumes defined in the operation
|
||||
consumes = operation.getConsumes();
|
||||
} else {
|
||||
// empty list, do nothing to override global setting
|
||||
}
|
||||
} else if (swagger != null && swagger.getConsumes() != null && swagger.getConsumes().size() > 0) {
|
||||
// use consumes defined globally
|
||||
consumes = swagger.getConsumes();
|
||||
LOGGER.debug("No consumes defined in operation. Using global consumes (" + swagger.getConsumes() + ") for " + op.operationId);
|
||||
}
|
||||
|
||||
// if "consumes" is defined (per operation or using global definition)
|
||||
if (consumes != null && consumes.size() > 0) {
|
||||
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
|
||||
int count = 0;
|
||||
for (String key : operation.getConsumes()) {
|
||||
for (String key : consumes) {
|
||||
Map<String, String> mediaType = new HashMap<String, String>();
|
||||
mediaType.put("mediaType", key);
|
||||
count += 1;
|
||||
if (count < operation.getConsumes().size()) {
|
||||
if (count < consumes.size()) {
|
||||
mediaType.put("hasMore", "true");
|
||||
} else {
|
||||
mediaType.put("hasMore", null);
|
||||
@@ -932,14 +1137,29 @@ public class DefaultCodegen {
|
||||
op.hasConsumes = true;
|
||||
}
|
||||
|
||||
if (operation.getProduces() != null && operation.getProduces().size() > 0) {
|
||||
List<String> produces = new ArrayList<String>();
|
||||
if (operation.getProduces() != null) {
|
||||
if (operation.getProduces().size() > 0) {
|
||||
// use produces defined in the operation
|
||||
produces = operation.getProduces();
|
||||
} else {
|
||||
// empty list, do nothing to override global setting
|
||||
}
|
||||
} else if (swagger != null && swagger.getProduces() != null && swagger.getProduces().size() > 0) {
|
||||
// use produces defined globally
|
||||
produces = swagger.getProduces();
|
||||
LOGGER.debug("No produces defined in operation. Using global produces (" + swagger.getProduces() + ") for " + op.operationId);
|
||||
}
|
||||
|
||||
// if "produces" is defined (per operation or using global definition)
|
||||
if (produces != null && produces.size() > 0) {
|
||||
List<Map<String, String>> c = new ArrayList<Map<String, String>>();
|
||||
int count = 0;
|
||||
for (String key : operation.getProduces()) {
|
||||
for (String key : produces) {
|
||||
Map<String, String> mediaType = new HashMap<String, String>();
|
||||
mediaType.put("mediaType", key);
|
||||
count += 1;
|
||||
if (count < operation.getProduces().size()) {
|
||||
if (count < produces.size()) {
|
||||
mediaType.put("hasMore", "true");
|
||||
} else {
|
||||
mediaType.put("hasMore", null);
|
||||
@@ -1092,6 +1312,13 @@ public class DefaultCodegen {
|
||||
return op;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Swagger Response object to Codegen Response object
|
||||
*
|
||||
* @param responseCode HTTP response code
|
||||
* @param response Swagger Response object
|
||||
* @return Codegen Response object
|
||||
*/
|
||||
public CodegenResponse fromResponse(String responseCode, Response response) {
|
||||
CodegenResponse r = CodegenModelFactory.newInstance(CodegenModelType.RESPONSE);
|
||||
if ("default".equals(responseCode)) {
|
||||
@@ -1141,7 +1368,14 @@ public class DefaultCodegen {
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert Swagger Parameter object to Codegen Parameter object
|
||||
*
|
||||
* @param param Swagger parameter object
|
||||
* @param a set of imports for library/package/module
|
||||
* @return Codegen Parameter object
|
||||
*/
|
||||
public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
|
||||
CodegenParameter p = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
|
||||
p.baseName = param.getName();
|
||||
@@ -1287,6 +1521,12 @@ public class DefaultCodegen {
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert map of Swagger SecuritySchemeDefinition objects to a list of Codegen Security objects
|
||||
*
|
||||
* @param schemes a map of Swagger SecuritySchemeDefinition object
|
||||
* @return a list of Codegen Security objects
|
||||
*/
|
||||
public List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes) {
|
||||
if (schemes == null) {
|
||||
return null;
|
||||
@@ -1319,7 +1559,23 @@ public class DefaultCodegen {
|
||||
sec.authorizationUrl = oauth2Definition.getAuthorizationUrl();
|
||||
sec.tokenUrl = oauth2Definition.getTokenUrl();
|
||||
if (oauth2Definition.getScopes() != null) {
|
||||
sec.scopes = oauth2Definition.getScopes().keySet();
|
||||
List<Map<String, Object>> scopes = new ArrayList<Map<String, Object>>();
|
||||
int count = 0, numScopes = oauth2Definition.getScopes().size();
|
||||
for(Map.Entry<String, String> scopeEntry : oauth2Definition.getScopes().entrySet()) {
|
||||
Map<String, Object> scope = new HashMap<String, Object>();
|
||||
scope.put("scope", scopeEntry.getKey());
|
||||
scope.put("description", scopeEntry.getValue());
|
||||
|
||||
count += 1;
|
||||
if (count < numScopes) {
|
||||
scope.put("hasMore", "true");
|
||||
} else {
|
||||
scope.put("hasMore", null);
|
||||
}
|
||||
|
||||
scopes.add(scope);
|
||||
}
|
||||
sec.scopes = scopes;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1329,6 +1585,12 @@ public class DefaultCodegen {
|
||||
return secs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the type to see if it needs import the library/module/package
|
||||
*
|
||||
* @param type name of the type
|
||||
* @return true if the library/module/package of the corresponding type needs to be imported
|
||||
*/
|
||||
protected boolean needToImport(String type) {
|
||||
return !defaultIncludes.contains(type)
|
||||
&& !languageSpecificPrimitives.contains(type)
|
||||
@@ -1386,6 +1648,15 @@ public class DefaultCodegen {
|
||||
return objs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add operation to group
|
||||
*
|
||||
* @param tag name of the tag
|
||||
* @param resourcePath path of the resource
|
||||
* @param operation Swagger Operation object
|
||||
* @param co Codegen Operation object
|
||||
* @param operations map of Codegen operations
|
||||
*/
|
||||
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
|
||||
List<CodegenOperation> opList = operations.get(tag);
|
||||
if (opList == null) {
|
||||
@@ -1396,9 +1667,7 @@ public class DefaultCodegen {
|
||||
co.baseName = tag;
|
||||
}
|
||||
|
||||
/* underscore and camelize are copied from Twitter elephant bird
|
||||
* https://github.com/twitter/elephant-bird/blob/master/core/src/main/java/com/twitter/elephantbird/util/Strings.java
|
||||
*/
|
||||
|
||||
|
||||
private void addParentContainer(CodegenModel m, String name, Property property) {
|
||||
final CodegenProperty tmp = fromProperty(name, property);
|
||||
@@ -1417,6 +1686,8 @@ public class DefaultCodegen {
|
||||
|
||||
/**
|
||||
* Underscore the given word.
|
||||
* Copied from Twitter elephant bird
|
||||
* https://github.com/twitter/elephant-bird/blob/master/core/src/main/java/com/twitter/elephantbird/util/Strings.java
|
||||
*
|
||||
* @param word The word
|
||||
* @return The underscored version of the word
|
||||
@@ -1481,12 +1752,11 @@ public class DefaultCodegen {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove characters not suitable for variable or method name from the input and camelize it
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
* @param name string to be camelize
|
||||
* @return camelized string
|
||||
*/
|
||||
public String removeNonNameElementToCamelCase(String name) {
|
||||
String nonNameElementPattern = "[-_:;#]";
|
||||
@@ -1502,11 +1772,26 @@ public class DefaultCodegen {
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Camelize name (parameter, property, method, etc) with upper case for first letter
|
||||
* copied from Twitter elephant bird
|
||||
* https://github.com/twitter/elephant-bird/blob/master/core/src/main/java/com/twitter/elephantbird/util/Strings.java
|
||||
*
|
||||
* @param word string to be camelize
|
||||
* @return camelized string
|
||||
*/
|
||||
public static String camelize(String word) {
|
||||
return camelize(word, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Camelize name (parameter, property, method, etc)
|
||||
*
|
||||
* @param word string to be camelize
|
||||
* @param lowercaseFirstLetter lower case for first letter if set to true
|
||||
* @return camelized string
|
||||
*/
|
||||
public static String camelize(String word, boolean lowercaseFirstLetter) {
|
||||
// Replace all slashes with dots (package separator)
|
||||
Pattern p = Pattern.compile("\\/(.?)");
|
||||
@@ -1611,7 +1896,7 @@ public class DefaultCodegen {
|
||||
}
|
||||
|
||||
/**
|
||||
* sanitize name (parameter, property, method, etc)
|
||||
* Sanitize name (parameter, property, method, etc)
|
||||
*
|
||||
* @param name string to be sanitize
|
||||
* @return sanitized string
|
||||
|
||||
@@ -29,16 +29,7 @@ import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
Logger LOGGER = LoggerFactory.getLogger(DefaultGenerator.class);
|
||||
@@ -60,6 +51,53 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
|
||||
@Override
|
||||
public List<File> generate() {
|
||||
Boolean generateApis = null;
|
||||
Boolean generateModels = null;
|
||||
Boolean generateSupportingFiles = null;
|
||||
|
||||
Set<String> modelsToGenerate = null;
|
||||
Set<String> apisToGenerate = null;
|
||||
Set<String> supportingFilesToGenerate = null;
|
||||
|
||||
// allows generating only models by specifying a CSV of models to generate, or empty for all
|
||||
if(System.getProperty("models") != null) {
|
||||
String modelNames = System.getProperty("models");
|
||||
generateModels = true;
|
||||
if(!modelNames.isEmpty()) {
|
||||
modelsToGenerate = new HashSet<String>(Arrays.asList(modelNames.split(",")));
|
||||
}
|
||||
}
|
||||
if(System.getProperty("apis") != null) {
|
||||
String apiNames = System.getProperty("apis");
|
||||
generateApis = true;
|
||||
if(!apiNames.isEmpty()) {
|
||||
apisToGenerate = new HashSet<String>(Arrays.asList(apiNames.split(",")));
|
||||
}
|
||||
}
|
||||
if(System.getProperty("supportingFiles") != null) {
|
||||
String supportingFiles = System.getProperty("supportingFiles");
|
||||
generateSupportingFiles = true;
|
||||
if(!supportingFiles.isEmpty()) {
|
||||
supportingFilesToGenerate = new HashSet<String>(Arrays.asList(supportingFiles.split(",")));
|
||||
}
|
||||
}
|
||||
|
||||
if(generateApis == null && generateModels == null && generateSupportingFiles == null) {
|
||||
// no specifics are set, generate everything
|
||||
generateApis = true; generateModels = true; generateSupportingFiles = true;
|
||||
}
|
||||
else {
|
||||
if(generateApis == null) {
|
||||
generateApis = false;
|
||||
}
|
||||
if(generateModels == null) {
|
||||
generateModels = false;
|
||||
}
|
||||
if(generateSupportingFiles == null) {
|
||||
generateSupportingFiles = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (swagger == null || config == null) {
|
||||
throw new RuntimeException("missing swagger input or config!");
|
||||
}
|
||||
@@ -125,8 +163,13 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
}
|
||||
String contextPath = swagger.getBasePath() == null ? "" : swagger.getBasePath();
|
||||
String basePath = hostBuilder.toString();
|
||||
String basePathWithoutHost = swagger.getBasePath();
|
||||
|
||||
|
||||
// resolve inline models
|
||||
InlineModelResolver inlineModelResolver = new InlineModelResolver();
|
||||
inlineModelResolver.flatten(swagger);
|
||||
|
||||
List<Object> allOperations = new ArrayList<Object>();
|
||||
List<Object> allModels = new ArrayList<Object>();
|
||||
|
||||
@@ -135,28 +178,107 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
if (definitions != null) {
|
||||
List<String> sortedModelKeys = sortModelsByInheritance(definitions);
|
||||
|
||||
for (String name : sortedModelKeys) {
|
||||
try {
|
||||
|
||||
//dont generate models that have an import mapping
|
||||
if(config.importMapping().containsKey(name)) {
|
||||
continue;
|
||||
if(generateModels) {
|
||||
if(modelsToGenerate != null && modelsToGenerate.size() > 0) {
|
||||
List<String> updatedKeys = new ArrayList<String>();
|
||||
for(String m : sortedModelKeys) {
|
||||
if(modelsToGenerate.contains(m)) {
|
||||
updatedKeys.add(m);
|
||||
}
|
||||
}
|
||||
sortedModelKeys = updatedKeys;
|
||||
}
|
||||
|
||||
Model model = definitions.get(name);
|
||||
Map<String, Model> modelMap = new HashMap<String, Model>();
|
||||
modelMap.put(name, model);
|
||||
Map<String, Object> models = processModels(config, modelMap, definitions);
|
||||
models.putAll(config.additionalProperties());
|
||||
|
||||
allModels.add(((List<Object>) models.get("models")).get(0));
|
||||
|
||||
for (String templateName : config.modelTemplateFiles().keySet()) {
|
||||
String suffix = config.modelTemplateFiles().get(templateName);
|
||||
String filename = config.modelFileFolder() + File.separator + config.toModelFilename(name) + suffix;
|
||||
if (!config.shouldOverwrite(filename)) {
|
||||
for (String name : sortedModelKeys) {
|
||||
try {
|
||||
//don't generate models that have an import mapping
|
||||
if(config.importMapping().containsKey(name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Model model = definitions.get(name);
|
||||
Map<String, Model> modelMap = new HashMap<String, Model>();
|
||||
modelMap.put(name, model);
|
||||
Map<String, Object> models = processModels(config, modelMap, definitions);
|
||||
models.putAll(config.additionalProperties());
|
||||
|
||||
allModels.add(((List<Object>) models.get("models")).get(0));
|
||||
|
||||
for (String templateName : config.modelTemplateFiles().keySet()) {
|
||||
String suffix = config.modelTemplateFiles().get(templateName);
|
||||
String filename = config.modelFileFolder() + File.separator + config.toModelFilename(name) + suffix;
|
||||
if (!config.shouldOverwrite(filename)) {
|
||||
continue;
|
||||
}
|
||||
String templateFile = getFullTemplateFile(config, templateName);
|
||||
String template = readTemplate(templateFile);
|
||||
Template tmpl = Mustache.compiler()
|
||||
.withLoader(new Mustache.TemplateLoader() {
|
||||
@Override
|
||||
public Reader getTemplate(String name) {
|
||||
return getTemplateReader(getFullTemplateFile(config, name + ".mustache"));
|
||||
}
|
||||
})
|
||||
.defaultValue("")
|
||||
.compile(template);
|
||||
writeToFile(filename, tmpl.execute(models));
|
||||
files.add(new File(filename));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Could not generate model '" + name + "'", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (System.getProperty("debugModels") != null) {
|
||||
System.out.println("############ Model info ############");
|
||||
Json.prettyPrint(allModels);
|
||||
}
|
||||
|
||||
// apis
|
||||
Map<String, List<CodegenOperation>> paths = processPaths(swagger.getPaths());
|
||||
if(generateApis) {
|
||||
if(apisToGenerate != null && apisToGenerate.size() > 0) {
|
||||
Map<String, List<CodegenOperation>> updatedPaths = new TreeMap<String, List<CodegenOperation>>();
|
||||
for(String m : paths.keySet()) {
|
||||
if(apisToGenerate.contains(m)) {
|
||||
updatedPaths.put(m, paths.get(m));
|
||||
}
|
||||
}
|
||||
paths = updatedPaths;
|
||||
}
|
||||
for (String tag : paths.keySet()) {
|
||||
try {
|
||||
List<CodegenOperation> ops = paths.get(tag);
|
||||
Map<String, Object> operation = processOperations(config, tag, ops);
|
||||
|
||||
operation.put("basePath", basePath);
|
||||
operation.put("basePathWithoutHost", basePathWithoutHost);
|
||||
operation.put("contextPath", contextPath);
|
||||
operation.put("baseName", tag);
|
||||
operation.put("modelPackage", config.modelPackage());
|
||||
operation.putAll(config.additionalProperties());
|
||||
operation.put("classname", config.toApiName(tag));
|
||||
operation.put("classVarName", config.toApiVarName(tag));
|
||||
operation.put("importPath", config.toApiImport(tag));
|
||||
|
||||
processMimeTypes(swagger.getConsumes(), operation, "consumes");
|
||||
processMimeTypes(swagger.getProduces(), operation, "produces");
|
||||
|
||||
allOperations.add(new HashMap<String, Object>(operation));
|
||||
for (int i = 0; i < allOperations.size(); i++) {
|
||||
Map<String, Object> oo = (Map<String, Object>) allOperations.get(i);
|
||||
if (i < (allOperations.size() - 1)) {
|
||||
oo.put("hasMore", "true");
|
||||
}
|
||||
}
|
||||
|
||||
for (String templateName : config.apiTemplateFiles().keySet()) {
|
||||
String filename = config.apiFilename(templateName, tag);
|
||||
if (!config.shouldOverwrite(filename) && new File(filename).exists()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String templateFile = getFullTemplateFile(config, templateName);
|
||||
String template = readTemplate(templateFile);
|
||||
Template tmpl = Mustache.compiler()
|
||||
@@ -168,71 +290,15 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
})
|
||||
.defaultValue("")
|
||||
.compile(template);
|
||||
writeToFile(filename, tmpl.execute(models));
|
||||
|
||||
writeToFile(filename, tmpl.execute(operation));
|
||||
files.add(new File(filename));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Could not generate model '" + name + "'", e);
|
||||
throw new RuntimeException("Could not generate api file for '" + tag + "'", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (System.getProperty("debugModels") != null) {
|
||||
System.out.println("############ Model info ############");
|
||||
Json.prettyPrint(allModels);
|
||||
}
|
||||
|
||||
// apis
|
||||
Map<String, List<CodegenOperation>> paths = processPaths(swagger.getPaths());
|
||||
for (String tag : paths.keySet()) {
|
||||
try {
|
||||
List<CodegenOperation> ops = paths.get(tag);
|
||||
Map<String, Object> operation = processOperations(config, tag, ops);
|
||||
|
||||
operation.put("basePath", basePath);
|
||||
operation.put("contextPath", contextPath);
|
||||
operation.put("baseName", tag);
|
||||
operation.put("modelPackage", config.modelPackage());
|
||||
operation.putAll(config.additionalProperties());
|
||||
operation.put("classname", config.toApiName(tag));
|
||||
operation.put("classVarName", config.toApiVarName(tag));
|
||||
operation.put("importPath", config.toApiImport(tag));
|
||||
|
||||
processMimeTypes(swagger.getConsumes(), operation, "consumes");
|
||||
processMimeTypes(swagger.getProduces(), operation, "produces");
|
||||
|
||||
allOperations.add(new HashMap<String, Object>(operation));
|
||||
for (int i = 0; i < allOperations.size(); i++) {
|
||||
Map<String, Object> oo = (Map<String, Object>) allOperations.get(i);
|
||||
if (i < (allOperations.size() - 1)) {
|
||||
oo.put("hasMore", "true");
|
||||
}
|
||||
}
|
||||
|
||||
for (String templateName : config.apiTemplateFiles().keySet()) {
|
||||
String filename = config.apiFilename(templateName, tag);
|
||||
if (!config.shouldOverwrite(filename) && new File(filename).exists()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String templateFile = getFullTemplateFile(config, templateName);
|
||||
String template = readTemplate(templateFile);
|
||||
Template tmpl = Mustache.compiler()
|
||||
.withLoader(new Mustache.TemplateLoader() {
|
||||
@Override
|
||||
public Reader getTemplate(String name) {
|
||||
return getTemplateReader(getFullTemplateFile(config, name + ".mustache"));
|
||||
}
|
||||
})
|
||||
.defaultValue("")
|
||||
.compile(template);
|
||||
|
||||
writeToFile(filename, tmpl.execute(operation));
|
||||
files.add(new File(filename));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Could not generate api file for '" + tag + "'", e);
|
||||
}
|
||||
}
|
||||
if (System.getProperty("debugOperations") != null) {
|
||||
System.out.println("############ Operation info ############");
|
||||
Json.prettyPrint(allOperations);
|
||||
@@ -276,66 +342,79 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
Json.prettyPrint(bundle);
|
||||
}
|
||||
|
||||
for (SupportingFile support : config.supportingFiles()) {
|
||||
try {
|
||||
String outputFolder = config.outputFolder();
|
||||
if (isNotEmpty(support.folder)) {
|
||||
outputFolder += File.separator + support.folder;
|
||||
}
|
||||
File of = new File(outputFolder);
|
||||
if (!of.isDirectory()) {
|
||||
of.mkdirs();
|
||||
}
|
||||
String outputFilename = outputFolder + File.separator + support.destinationFilename;
|
||||
if (!config.shouldOverwrite(outputFilename)) {
|
||||
continue;
|
||||
}
|
||||
if(generateSupportingFiles) {
|
||||
for (SupportingFile support : config.supportingFiles()) {
|
||||
try {
|
||||
String outputFolder = config.outputFolder();
|
||||
if (isNotEmpty(support.folder)) {
|
||||
outputFolder += File.separator + support.folder;
|
||||
}
|
||||
File of = new File(outputFolder);
|
||||
if (!of.isDirectory()) {
|
||||
of.mkdirs();
|
||||
}
|
||||
String outputFilename = outputFolder + File.separator + support.destinationFilename;
|
||||
if (!config.shouldOverwrite(outputFilename)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String templateFile = getFullTemplateFile(config, support.templateFile);
|
||||
String templateFile = getFullTemplateFile(config, support.templateFile);
|
||||
|
||||
if (templateFile.endsWith("mustache")) {
|
||||
String template = readTemplate(templateFile);
|
||||
Template tmpl = Mustache.compiler()
|
||||
.withLoader(new Mustache.TemplateLoader() {
|
||||
@Override
|
||||
public Reader getTemplate(String name) {
|
||||
return getTemplateReader(getFullTemplateFile(config, name + ".mustache"));
|
||||
boolean shouldGenerate = true;
|
||||
if(supportingFilesToGenerate != null && supportingFilesToGenerate.size() > 0) {
|
||||
if(supportingFilesToGenerate.contains(support.destinationFilename)) {
|
||||
shouldGenerate = true;
|
||||
}
|
||||
else {
|
||||
shouldGenerate = false;
|
||||
}
|
||||
}
|
||||
if(shouldGenerate) {
|
||||
if (templateFile.endsWith("mustache")) {
|
||||
String template = readTemplate(templateFile);
|
||||
Template tmpl = Mustache.compiler()
|
||||
.withLoader(new Mustache.TemplateLoader() {
|
||||
@Override
|
||||
public Reader getTemplate(String name) {
|
||||
return getTemplateReader(getFullTemplateFile(config, name + ".mustache"));
|
||||
}
|
||||
})
|
||||
.defaultValue("")
|
||||
.compile(template);
|
||||
|
||||
writeToFile(outputFilename, tmpl.execute(bundle));
|
||||
files.add(new File(outputFilename));
|
||||
} else {
|
||||
InputStream in = null;
|
||||
|
||||
try {
|
||||
in = new FileInputStream(templateFile);
|
||||
} catch (Exception e) {
|
||||
// continue
|
||||
}
|
||||
if (in == null) {
|
||||
in = this.getClass().getClassLoader().getResourceAsStream(getCPResourcePath(templateFile));
|
||||
}
|
||||
File outputFile = new File(outputFilename);
|
||||
OutputStream out = new FileOutputStream(outputFile, false);
|
||||
if (in != null && out != null) {
|
||||
System.out.println("writing file " + outputFile);
|
||||
IOUtils.copy(in, out);
|
||||
} else {
|
||||
if (in == null) {
|
||||
System.out.println("can't open " + templateFile + " for input");
|
||||
}
|
||||
})
|
||||
.defaultValue("")
|
||||
.compile(template);
|
||||
if (out == null) {
|
||||
System.out.println("can't open " + outputFile + " for output");
|
||||
}
|
||||
}
|
||||
|
||||
writeToFile(outputFilename, tmpl.execute(bundle));
|
||||
files.add(new File(outputFilename));
|
||||
} else {
|
||||
InputStream in = null;
|
||||
|
||||
try {
|
||||
in = new FileInputStream(templateFile);
|
||||
} catch (Exception e) {
|
||||
// continue
|
||||
}
|
||||
if (in == null) {
|
||||
in = this.getClass().getClassLoader().getResourceAsStream(getCPResourcePath(templateFile));
|
||||
}
|
||||
File outputFile = new File(outputFilename);
|
||||
OutputStream out = new FileOutputStream(outputFile, false);
|
||||
if (in != null && out != null) {
|
||||
System.out.println("writing file " + outputFile);
|
||||
IOUtils.copy(in, out);
|
||||
} else {
|
||||
if (in == null) {
|
||||
System.out.println("can't open " + templateFile + " for input");
|
||||
}
|
||||
if (out == null) {
|
||||
System.out.println("can't open " + outputFile + " for output");
|
||||
files.add(outputFile);
|
||||
}
|
||||
}
|
||||
|
||||
files.add(outputFile);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Could not generate supporting file '" + support + "'", e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Could not generate supporting file '" + support + "'", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -471,7 +550,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
for (String tag : tags) {
|
||||
CodegenOperation co = null;
|
||||
try {
|
||||
co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions());
|
||||
co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions(), swagger);
|
||||
co.tags = new ArrayList<String>();
|
||||
co.tags.add(sanitizeTag(tag));
|
||||
config.addOperationToGroup(sanitizeTag(tag), resourcePath, operation, co, operations);
|
||||
@@ -519,6 +598,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
}
|
||||
if (!authMethods.isEmpty()) {
|
||||
co.authMethods = config.fromSecurity(authMethods);
|
||||
co.hasAuthMethods = true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
@@ -526,7 +606,8 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
+ " Tag: " + tag + "\n"//
|
||||
+ " Operation: " + operation.getOperationId() + "\n" //
|
||||
+ " Resource: " + httpMethod + " " + resourcePath + "\n"//
|
||||
+ " Definitions: " + swagger.getDefinitions();
|
||||
+ " Definitions: " + swagger.getDefinitions() + "\n" //
|
||||
+ " Exception: " + ex.getMessage();
|
||||
throw new RuntimeException(msg, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,326 @@
|
||||
package io.swagger.codegen;
|
||||
|
||||
import com.sun.org.apache.xpath.internal.operations.Mod;
|
||||
import io.swagger.models.*;
|
||||
import io.swagger.models.parameters.BodyParameter;
|
||||
import io.swagger.models.parameters.Parameter;
|
||||
import io.swagger.models.parameters.RefParameter;
|
||||
import io.swagger.models.properties.*;
|
||||
import io.swagger.util.Json;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class InlineModelResolver {
|
||||
private Swagger swagger = null;
|
||||
private boolean skipMatches = false;
|
||||
|
||||
Map<String, Model> addedModels = new HashMap<String, Model>();
|
||||
Map<String, String> generatedSignature = new HashMap<String, String>();
|
||||
|
||||
public void flatten(Swagger swagger) {
|
||||
this.swagger = swagger;
|
||||
|
||||
if (swagger.getDefinitions() == null) {
|
||||
swagger.setDefinitions(new HashMap<String, Model>());
|
||||
}
|
||||
|
||||
// operations
|
||||
Map<String, Path> paths = swagger.getPaths();
|
||||
Map<String, Model> models = swagger.getDefinitions();
|
||||
|
||||
if (paths != null) {
|
||||
for (String pathname : paths.keySet()) {
|
||||
Path path = paths.get(pathname);
|
||||
|
||||
for (Operation operation : path.getOperations()) {
|
||||
List<Parameter> parameters = operation.getParameters();
|
||||
|
||||
if (parameters != null) {
|
||||
for (Parameter parameter : parameters) {
|
||||
if (parameter instanceof BodyParameter) {
|
||||
BodyParameter bp = (BodyParameter) parameter;
|
||||
if (bp.getSchema() != null) {
|
||||
Model model = bp.getSchema();
|
||||
if(model instanceof ModelImpl) {
|
||||
String modelName = uniqueName(bp.getName());
|
||||
ModelImpl obj = (ModelImpl) model;
|
||||
flattenProperties(obj.getProperties(), pathname);
|
||||
|
||||
bp.setSchema(new RefModel(modelName));
|
||||
addGenerated(modelName, model);
|
||||
swagger.addDefinition(modelName, model);
|
||||
}
|
||||
else if (model instanceof ArrayModel) {
|
||||
ArrayModel am = (ArrayModel) model;
|
||||
Property inner = am.getItems();
|
||||
|
||||
if(inner instanceof ObjectProperty) {
|
||||
ObjectProperty op = (ObjectProperty) inner;
|
||||
flattenProperties(op.getProperties(), pathname);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Map<String, Response> responses = operation.getResponses();
|
||||
if (responses != null) {
|
||||
for (String key : responses.keySet()) {
|
||||
Response response = responses.get(key);
|
||||
if (response.getSchema() != null) {
|
||||
Property property = response.getSchema();
|
||||
if (property instanceof ObjectProperty) {
|
||||
String modelName = uniqueName("inline_response_" + key);
|
||||
ObjectProperty op = (ObjectProperty) property;
|
||||
Model model = modelFromProperty(op, modelName);
|
||||
String existing = matchGenerated(model);
|
||||
if (existing != null) {
|
||||
response.setSchema(new RefProperty(existing));
|
||||
} else {
|
||||
response.setSchema(new RefProperty(modelName));
|
||||
addGenerated(modelName, model);
|
||||
swagger.addDefinition(modelName, model);
|
||||
}
|
||||
} else if (property instanceof ArrayProperty) {
|
||||
ArrayProperty ap = (ArrayProperty) property;
|
||||
if(ap.getItems() instanceof ObjectProperty) {
|
||||
ObjectProperty op = (ObjectProperty) ap.getItems();
|
||||
Map<String, Property> props = op.getProperties();
|
||||
flattenProperties(props, "path");
|
||||
}
|
||||
} else if (property instanceof MapProperty) {
|
||||
MapProperty op = (MapProperty) property;
|
||||
|
||||
Property innerProperty = op.getAdditionalProperties();
|
||||
if(innerProperty instanceof ObjectProperty) {
|
||||
ModelImpl innerModel = new ModelImpl();
|
||||
// TODO: model props
|
||||
innerModel.setTitle(property.getTitle());
|
||||
property.getVendorExtensions();
|
||||
property.getRequired();
|
||||
property.getReadOnly();
|
||||
property.getAccess();
|
||||
innerModel.setDescription(property.getDescription());
|
||||
innerModel.setExample(property.getExample());
|
||||
innerModel.setName(property.getName());
|
||||
innerModel.setXml(property.getXml());
|
||||
|
||||
innerModel.setAdditionalProperties(innerProperty);
|
||||
|
||||
String modelName = uniqueName("inline_response_" + key);
|
||||
String existing = matchGenerated(innerModel);
|
||||
if (existing != null) {
|
||||
response.setSchema(new RefProperty(existing));
|
||||
} else {
|
||||
response.setSchema(new RefProperty(modelName));
|
||||
addGenerated(modelName, innerModel);
|
||||
swagger.addDefinition(modelName, innerModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// definitions
|
||||
if (models != null) {
|
||||
List<String> modelNames = new ArrayList<String>(models.keySet());
|
||||
for (String modelName : modelNames) {
|
||||
Model model = models.get(modelName);
|
||||
if (model instanceof ModelImpl) {
|
||||
ModelImpl m = (ModelImpl) model;
|
||||
|
||||
Map<String, Property> properties = m.getProperties();
|
||||
flattenProperties(properties, modelName);
|
||||
|
||||
} else if (model instanceof ArrayModel) {
|
||||
ArrayModel m = (ArrayModel) model;
|
||||
Property inner = m.getItems();
|
||||
if (inner instanceof ObjectProperty) {
|
||||
String innerModelName = uniqueName(modelName + "_inner");
|
||||
Model innerModel = modelFromProperty((ObjectProperty) inner, modelName);
|
||||
|
||||
String existing = matchGenerated(innerModel);
|
||||
if (existing == null) {
|
||||
swagger.addDefinition(innerModelName, innerModel);
|
||||
addGenerated(innerModelName, innerModel);
|
||||
m.setItems(new RefProperty(innerModelName));
|
||||
} else {
|
||||
m.setItems(new RefProperty(existing));
|
||||
}
|
||||
}
|
||||
} else if (model instanceof ComposedModel) {
|
||||
ComposedModel m = (ComposedModel) model;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String matchGenerated(Model model) {
|
||||
if (this.skipMatches) {
|
||||
return null;
|
||||
}
|
||||
String json = Json.pretty(model);
|
||||
if (generatedSignature.containsKey(json)) {
|
||||
return generatedSignature.get(json);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addGenerated(String name, Model model) {
|
||||
generatedSignature.put(Json.pretty(model), name);
|
||||
}
|
||||
|
||||
public String uniqueName(String key) {
|
||||
int count = 0;
|
||||
boolean done = false;
|
||||
key = key.replaceAll("[^a-z_\\.A-Z0-9 ]", "");
|
||||
while (!done) {
|
||||
String name = key;
|
||||
if (count > 0) {
|
||||
name = key + "_" + count;
|
||||
}
|
||||
if (swagger.getDefinitions() == null) {
|
||||
return name;
|
||||
} else if (!swagger.getDefinitions().containsKey(name)) {
|
||||
return name;
|
||||
}
|
||||
count += 1;
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
public void flattenProperties(Map<String, Property> properties, String path) {
|
||||
if (properties == null) {
|
||||
return;
|
||||
}
|
||||
Map<String, Property> propsToUpdate = new HashMap<String, Property>();
|
||||
Map<String, Model> modelsToAdd = new HashMap<String, Model>();
|
||||
for (String key : properties.keySet()) {
|
||||
Property property = properties.get(key);
|
||||
if (property instanceof ObjectProperty && ((ObjectProperty)property).getProperties().size() > 0) {
|
||||
String modelName = uniqueName(path + "_" + key);
|
||||
|
||||
ObjectProperty op = (ObjectProperty) property;
|
||||
Model model = modelFromProperty(op, modelName);
|
||||
|
||||
String existing = matchGenerated(model);
|
||||
|
||||
if (existing != null) {
|
||||
propsToUpdate.put(key, new RefProperty(existing));
|
||||
} else {
|
||||
propsToUpdate.put(key, new RefProperty(modelName));
|
||||
modelsToAdd.put(modelName, model);
|
||||
addGenerated(modelName, model);
|
||||
swagger.addDefinition(modelName, model);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (propsToUpdate.size() > 0) {
|
||||
for (String key : propsToUpdate.keySet()) {
|
||||
properties.put(key, propsToUpdate.get(key));
|
||||
}
|
||||
}
|
||||
for (String key : modelsToAdd.keySet()) {
|
||||
swagger.addDefinition(key, modelsToAdd.get(key));
|
||||
this.addedModels.put(key, modelsToAdd.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
public Model modelFromProperty(ArrayProperty object, String path) {
|
||||
String access = object.getAccess();
|
||||
String description = object.getDescription();
|
||||
String example = object.getExample();
|
||||
String name = object.getName();
|
||||
Integer position = object.getPosition();
|
||||
Boolean readOnly = object.getReadOnly();
|
||||
Boolean required = object.getRequired();
|
||||
String title = object.getTitle();
|
||||
Map<String, Object> extensions = object.getVendorExtensions();
|
||||
Xml xml = object.getXml();
|
||||
|
||||
// object.getItems()
|
||||
// Map<String, Property> properties = object.getProperties();
|
||||
|
||||
Property inner = object.getItems();
|
||||
if (inner instanceof ObjectProperty) {
|
||||
ArrayModel model = new ArrayModel();
|
||||
model.setDescription(description);
|
||||
model.setExample(example);
|
||||
// model.setName(name);
|
||||
// model.setXml(xml);
|
||||
|
||||
model.setItems(object.getItems());
|
||||
return model;
|
||||
}
|
||||
|
||||
// if(properties != null) {
|
||||
// flattenProperties(properties, path);
|
||||
// model.setProperties(properties);
|
||||
// }
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Model modelFromProperty(ObjectProperty object, String path) {
|
||||
String access = object.getAccess();
|
||||
String description = object.getDescription();
|
||||
String example = object.getExample();
|
||||
String name = object.getName();
|
||||
Integer position = object.getPosition();
|
||||
Boolean readOnly = object.getReadOnly();
|
||||
Boolean required = object.getRequired();
|
||||
String title = object.getTitle();
|
||||
Map<String, Object> extensions = object.getVendorExtensions();
|
||||
Xml xml = object.getXml();
|
||||
|
||||
Map<String, Property> properties = object.getProperties();
|
||||
|
||||
ModelImpl model = new ModelImpl();
|
||||
model.setDescription(description);
|
||||
model.setExample(example);
|
||||
model.setName(name);
|
||||
model.setXml(xml);
|
||||
|
||||
if (properties != null) {
|
||||
flattenProperties(properties, path);
|
||||
model.setProperties(properties);
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
public Model modelFromProperty(MapProperty object, String path) {
|
||||
String access = object.getAccess();
|
||||
String description = object.getDescription();
|
||||
String example = object.getExample();
|
||||
String name = object.getName();
|
||||
Integer position = object.getPosition();
|
||||
Boolean readOnly = object.getReadOnly();
|
||||
Boolean required = object.getRequired();
|
||||
String title = object.getTitle();
|
||||
Map<String, Object> extensions = object.getVendorExtensions();
|
||||
Xml xml = object.getXml();
|
||||
|
||||
ArrayModel model = new ArrayModel();
|
||||
model.setDescription(description);
|
||||
model.setExample(example);
|
||||
model.setItems(object.getAdditionalProperties());
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
public boolean isSkipMatches() {
|
||||
return skipMatches;
|
||||
}
|
||||
|
||||
public void setSkipMatches(boolean skipMatches) {
|
||||
this.skipMatches = skipMatches;
|
||||
}
|
||||
}
|
||||
@@ -84,12 +84,15 @@ public class XmlExampleGenerator {
|
||||
name = xml.getName();
|
||||
}
|
||||
}
|
||||
for (String pName : model.getProperties().keySet()) {
|
||||
Property p = model.getProperties().get(pName);
|
||||
if (p != null && p.getXml() != null && p.getXml().getAttribute() != null && p.getXml().getAttribute()) {
|
||||
attributes.put(pName, p);
|
||||
} else {
|
||||
elements.put(pName, p);
|
||||
// TODO: map objects will not enter this block
|
||||
if(model.getProperties() != null) {
|
||||
for (String pName : model.getProperties().keySet()) {
|
||||
Property p = model.getProperties().get(pName);
|
||||
if (p != null && p.getXml() != null && p.getXml().getAttribute() != null && p.getXml().getAttribute()) {
|
||||
attributes.put(pName, p);
|
||||
} else {
|
||||
elements.put(pName, p);
|
||||
}
|
||||
}
|
||||
}
|
||||
sb.append(indent(indent)).append(TAG_START);
|
||||
|
||||
@@ -17,6 +17,7 @@ import java.util.HashSet;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
public class AndroidClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
public static final String USE_ANDROID_MAVEN_GRADLE_PLUGIN = "useAndroidMavenGradlePlugin";
|
||||
protected String invokerPackage = "io.swagger.client";
|
||||
protected String groupId = "io.swagger";
|
||||
protected String artifactId = "swagger-android-client";
|
||||
@@ -64,7 +65,7 @@ public class AndroidClientCodegen extends DefaultCodegen implements CodegenConfi
|
||||
cliOptions.add(new CliOption(CodegenConstants.ARTIFACT_ID, "artifactId for use in the generated build.gradle and pom.xml"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.ARTIFACT_VERSION, "artifact version for use in the generated build.gradle and pom.xml"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC));
|
||||
cliOptions.add(new CliOption("useAndroidMavenGradlePlugin", "A flag to toggle android-maven gradle plugin. Default is true."));
|
||||
cliOptions.add(new CliOption(USE_ANDROID_MAVEN_GRADLE_PLUGIN, "A flag to toggle android-maven gradle plugin. Default is true."));
|
||||
}
|
||||
|
||||
public CodegenType getTag() {
|
||||
@@ -220,14 +221,15 @@ public class AndroidClientCodegen extends DefaultCodegen implements CodegenConfi
|
||||
this.setSourceFolder((String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("useAndroidMavenGradlePlugin")) {
|
||||
this.setUseAndroidMavenGradlePlugin((Boolean) additionalProperties.get("useAndroidMavenGradlePlugin"));
|
||||
if (additionalProperties.containsKey(USE_ANDROID_MAVEN_GRADLE_PLUGIN)) {
|
||||
this.setUseAndroidMavenGradlePlugin(Boolean.valueOf((String) additionalProperties
|
||||
.get(USE_ANDROID_MAVEN_GRADLE_PLUGIN)));
|
||||
} else {
|
||||
additionalProperties.put("useAndroidMavenGradlePlugin", useAndroidMavenGradlePlugin);
|
||||
additionalProperties.put(USE_ANDROID_MAVEN_GRADLE_PLUGIN, useAndroidMavenGradlePlugin);
|
||||
}
|
||||
|
||||
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
|
||||
additionalProperties.put("useAndroidMavenGradlePlugin", useAndroidMavenGradlePlugin);
|
||||
additionalProperties.put(USE_ANDROID_MAVEN_GRADLE_PLUGIN, useAndroidMavenGradlePlugin);
|
||||
|
||||
supportingFiles.add(new SupportingFile("settings.gradle.mustache", "", "settings.gradle"));
|
||||
supportingFiles.add(new SupportingFile("build.mustache", "", "build.gradle"));
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.swagger.codegen.languages;
|
||||
|
||||
import io.swagger.codegen.CodegenConfig;
|
||||
import io.swagger.codegen.CodegenConstants;
|
||||
import io.swagger.codegen.CodegenType;
|
||||
import io.swagger.codegen.DefaultCodegen;
|
||||
import io.swagger.codegen.SupportingFile;
|
||||
@@ -79,8 +80,8 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
typeMapping.put("object", "Object");
|
||||
|
||||
cliOptions.clear();
|
||||
cliOptions.add(new CliOption("packageName", "C# package name (convention: Camel.Case), default: IO.Swagger"));
|
||||
cliOptions.add(new CliOption("packageVersion", "C# package version, default: 1.0.0"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "C# package name (convention: Camel.Case), default: IO.Swagger"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "C# package version, default: 1.0.0"));
|
||||
|
||||
}
|
||||
|
||||
@@ -88,19 +89,19 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
public void processOpts() {
|
||||
super.processOpts();
|
||||
|
||||
if (additionalProperties.containsKey("packageVersion")) {
|
||||
packageVersion = (String) additionalProperties.get("packageVersion");
|
||||
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_VERSION)) {
|
||||
setPackageVersion((String) additionalProperties.get(CodegenConstants.PACKAGE_VERSION));
|
||||
} else {
|
||||
additionalProperties.put("packageVersion", packageVersion);
|
||||
additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("packageName")) {
|
||||
packageName = (String) additionalProperties.get("packageName");
|
||||
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
|
||||
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
|
||||
apiPackage = packageName + ".Api";
|
||||
modelPackage = packageName + ".Model";
|
||||
clientPackage = packageName + ".Client";
|
||||
} else {
|
||||
additionalProperties.put("packageName", packageName);
|
||||
additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName);
|
||||
}
|
||||
|
||||
additionalProperties.put("clientPackage", clientPackage);
|
||||
@@ -252,4 +253,11 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
return camelize(sanitizeName(operationId));
|
||||
}
|
||||
|
||||
public void setPackageName(String packageName) {
|
||||
this.packageName = packageName;
|
||||
}
|
||||
|
||||
public void setPackageVersion(String packageVersion) {
|
||||
this.packageVersion = packageVersion;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.swagger.codegen.languages;
|
||||
|
||||
import io.swagger.codegen.CodegenConfig;
|
||||
import io.swagger.codegen.CodegenConstants;
|
||||
import io.swagger.codegen.CodegenType;
|
||||
import io.swagger.codegen.DefaultCodegen;
|
||||
import io.swagger.codegen.SupportingFile;
|
||||
@@ -15,6 +16,7 @@ import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class CsharpDotNet2ClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
public static final String CLIENT_PACKAGE = "clientPackage";
|
||||
protected String packageName = "IO.Swagger";
|
||||
protected String packageVersion = "1.0.0";
|
||||
protected String clientPackage = "IO.Swagger.Client";
|
||||
@@ -77,34 +79,34 @@ public class CsharpDotNet2ClientCodegen extends DefaultCodegen implements Codege
|
||||
typeMapping.put("object", "Object");
|
||||
|
||||
cliOptions.clear();
|
||||
cliOptions.add(new CliOption("packageName", "C# package name (convention: Camel.Case), default: IO.Swagger"));
|
||||
cliOptions.add(new CliOption("packageVersion", "C# package version, default: 1.0.0"));
|
||||
|
||||
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "C# package name (convention: Camel.Case), default: IO.Swagger"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "C# package version, default: 1.0.0"));
|
||||
cliOptions.add(new CliOption(CLIENT_PACKAGE, "C# client package name (convention: Camel.Case), default: IO.Swagger.Client"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processOpts() {
|
||||
super.processOpts();
|
||||
|
||||
if (additionalProperties.containsKey("packageVersion")) {
|
||||
packageVersion = (String) additionalProperties.get("packageVersion");
|
||||
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_VERSION)) {
|
||||
setPackageVersion((String) additionalProperties.get(CodegenConstants.PACKAGE_VERSION));
|
||||
} else {
|
||||
additionalProperties.put("packageVersion", packageVersion);
|
||||
additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("packageName")) {
|
||||
packageName = (String) additionalProperties.get("packageName");
|
||||
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
|
||||
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
|
||||
apiPackage = packageName + ".Api";
|
||||
modelPackage = packageName + ".Model";
|
||||
clientPackage = packageName + ".Client";
|
||||
} else {
|
||||
additionalProperties.put("packageName", packageName);
|
||||
additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("clientPackage")) {
|
||||
this.setClientPackage((String) additionalProperties.get("clientPackage"));
|
||||
if (additionalProperties.containsKey(CLIENT_PACKAGE)) {
|
||||
this.setClientPackage((String) additionalProperties.get(CLIENT_PACKAGE));
|
||||
} else {
|
||||
additionalProperties.put("clientPackage", clientPackage);
|
||||
additionalProperties.put(CLIENT_PACKAGE, clientPackage);
|
||||
}
|
||||
|
||||
supportingFiles.add(new SupportingFile("Configuration.mustache",
|
||||
@@ -123,6 +125,14 @@ public class CsharpDotNet2ClientCodegen extends DefaultCodegen implements Codege
|
||||
this.clientPackage = clientPackage;
|
||||
}
|
||||
|
||||
public void setPackageName(String packageName) {
|
||||
this.packageName = packageName;
|
||||
}
|
||||
|
||||
public void setPackageVersion(String packageVersion) {
|
||||
this.packageVersion = packageVersion;
|
||||
}
|
||||
|
||||
public CodegenType getTag() {
|
||||
return CodegenType.CLIENT;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.swagger.codegen.languages;
|
||||
|
||||
import io.swagger.codegen.CliOption;
|
||||
import io.swagger.codegen.CodegenConfig;
|
||||
import io.swagger.codegen.CodegenConstants;
|
||||
import io.swagger.codegen.CodegenType;
|
||||
import io.swagger.codegen.DefaultCodegen;
|
||||
import io.swagger.codegen.SupportingFile;
|
||||
@@ -15,6 +16,10 @@ import java.util.HashSet;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class DartClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
public static final String BROWSER_CLIENT = "browserClient";
|
||||
public static final String PUB_NAME = "pubName";
|
||||
public static final String PUB_VERSION = "pubVersion";
|
||||
public static final String PUB_DESCRIPTION = "pubDescription";
|
||||
protected boolean browserClient = true;
|
||||
protected String pubName = "swagger";
|
||||
protected String pubVersion = "1.0.0";
|
||||
@@ -72,10 +77,11 @@ public class DartClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
typeMapping.put("date", "DateTime");
|
||||
typeMapping.put("File", "MultipartFile");
|
||||
|
||||
cliOptions.add(new CliOption("browserClient", "Is the client browser based"));
|
||||
cliOptions.add(new CliOption("pubName", "Name in generated pubspec"));
|
||||
cliOptions.add(new CliOption("pubVersion", "Version in generated pubspec"));
|
||||
cliOptions.add(new CliOption("sourceFolder", "source folder for generated code"));
|
||||
cliOptions.add(new CliOption(BROWSER_CLIENT, "Is the client browser based"));
|
||||
cliOptions.add(new CliOption(PUB_NAME, "Name in generated pubspec"));
|
||||
cliOptions.add(new CliOption(PUB_VERSION, "Version in generated pubspec"));
|
||||
cliOptions.add(new CliOption(PUB_DESCRIPTION, "Description in generated pubspec"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, "source folder for generated code"));
|
||||
}
|
||||
|
||||
public CodegenType getTag() {
|
||||
@@ -94,37 +100,37 @@ public class DartClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
public void processOpts() {
|
||||
super.processOpts();
|
||||
|
||||
if (additionalProperties.containsKey("browserClient")) {
|
||||
this.setBrowserClient(Boolean.parseBoolean((String) additionalProperties.get("browserClient")));
|
||||
additionalProperties.put("browserClient", browserClient);
|
||||
if (additionalProperties.containsKey(BROWSER_CLIENT)) {
|
||||
this.setBrowserClient(Boolean.parseBoolean((String) additionalProperties.get(BROWSER_CLIENT)));
|
||||
additionalProperties.put(BROWSER_CLIENT, browserClient);
|
||||
} else {
|
||||
//not set, use to be passed to template
|
||||
additionalProperties.put("browserClient", browserClient);
|
||||
additionalProperties.put(BROWSER_CLIENT, browserClient);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("pubName")) {
|
||||
this.setPubName((String) additionalProperties.get("pubName"));
|
||||
if (additionalProperties.containsKey(PUB_NAME)) {
|
||||
this.setPubName((String) additionalProperties.get(PUB_NAME));
|
||||
} else {
|
||||
//not set, use to be passed to template
|
||||
additionalProperties.put("pubName", pubName);
|
||||
additionalProperties.put(PUB_NAME, pubName);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("pubVersion")) {
|
||||
this.setPubVersion((String) additionalProperties.get("pubVersion"));
|
||||
if (additionalProperties.containsKey(PUB_VERSION)) {
|
||||
this.setPubVersion((String) additionalProperties.get(PUB_VERSION));
|
||||
} else {
|
||||
//not set, use to be passed to template
|
||||
additionalProperties.put("pubVersion", pubVersion);
|
||||
additionalProperties.put(PUB_VERSION, pubVersion);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("pubDescription")) {
|
||||
this.setPubDescription((String) additionalProperties.get("pubDescription"));
|
||||
if (additionalProperties.containsKey(PUB_DESCRIPTION)) {
|
||||
this.setPubDescription((String) additionalProperties.get(PUB_DESCRIPTION));
|
||||
} else {
|
||||
//not set, use to be passed to template
|
||||
additionalProperties.put("pubDescription", pubDescription);
|
||||
additionalProperties.put(PUB_DESCRIPTION, pubDescription);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("sourceFolder")) {
|
||||
this.setSourceFolder((String) additionalProperties.get("sourceFolder"));
|
||||
if (additionalProperties.containsKey(CodegenConstants.SOURCE_FOLDER)) {
|
||||
this.setSourceFolder((String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER));
|
||||
}
|
||||
|
||||
final String libFolder = sourceFolder + File.separator + "lib";
|
||||
|
||||
@@ -68,15 +68,19 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
importMapping.put("File", "flash.filesystem.File");
|
||||
|
||||
// from
|
||||
reservedWords = new HashSet<String>(
|
||||
Arrays.asList(
|
||||
"add", "for", "lt", "tellTarget", "and", "function", "ne", "this", "break", "ge", "new", "typeof", "continue", "gt", "not", "var", "delete", "if", "on", "void", "do", "ifFrameLoaded", "onClipEvent", "while", "else", "in", "or", "with", "eq", "le", "return"));
|
||||
reservedWords = new HashSet<String>(Arrays.asList("add", "for", "lt", "tellTarget", "and",
|
||||
"function", "ne", "this", "break", "ge", "new", "typeof", "continue", "gt", "not",
|
||||
"var", "delete", "if", "on", "void", "do", "ifFrameLoaded", "onClipEvent", "while",
|
||||
"else", "in", "or", "with", "eq", "le", "return"));
|
||||
|
||||
cliOptions.clear();
|
||||
cliOptions.add(new CliOption("packageName", "flash package name (convention: package.name), default: io.swagger"));
|
||||
cliOptions.add(new CliOption("packageVersion", "flash package version, default: 1.0.0"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "flash package name (convention:" +
|
||||
" package.name), default: io.swagger"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "flash package version, " +
|
||||
"default: 1.0.0"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, CodegenConstants.INVOKER_PACKAGE_DESC));
|
||||
cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, "source folder for generated code. e.g. src/main/flex"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, "source folder for generated " +
|
||||
"code. e.g. src/main/flex"));
|
||||
|
||||
}
|
||||
|
||||
@@ -95,8 +99,8 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
this.setSourceFolder((String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("packageName")) {
|
||||
setPackageName((String) additionalProperties.get("packageName"));
|
||||
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
|
||||
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
|
||||
apiPackage = packageName + ".client.api";
|
||||
modelPackage = packageName + ".client.model";
|
||||
}
|
||||
@@ -104,20 +108,21 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
setPackageName("io.swagger");
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("packageVersion")) {
|
||||
setPackageVersion((String) additionalProperties.get("packageVersion"));
|
||||
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_VERSION)) {
|
||||
setPackageVersion((String) additionalProperties.get(CodegenConstants.PACKAGE_VERSION));
|
||||
}
|
||||
else {
|
||||
setPackageVersion("1.0.0");
|
||||
}
|
||||
|
||||
additionalProperties.put("packageName", packageName);
|
||||
additionalProperties.put("packageVersion", packageVersion);
|
||||
additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName);
|
||||
additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion);
|
||||
|
||||
//modelPackage = invokerPackage + File.separatorChar + "client" + File.separatorChar + "model";
|
||||
//apiPackage = invokerPackage + File.separatorChar + "client" + File.separatorChar + "api";
|
||||
|
||||
final String invokerFolder = (sourceFolder + File.separator + invokerPackage + File.separator + "swagger" + File.separator).replace(".", File.separator).replace('.', File.separatorChar);
|
||||
final String invokerFolder = (sourceFolder + File.separator + invokerPackage + File.separator
|
||||
+ "swagger" + File.separator).replace(".", File.separator).replace('.', File.separatorChar);
|
||||
|
||||
supportingFiles.add(new SupportingFile("ApiInvoker.as", invokerFolder + "common", "ApiInvoker.as"));
|
||||
supportingFiles.add(new SupportingFile("ApiUrlHelper.as", invokerFolder + "common", "ApiUrlHelper.as"));
|
||||
@@ -131,13 +136,20 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
supportingFiles.add(new SupportingFile("Response.as", invokerFolder + "event", "Response.as"));
|
||||
supportingFiles.add(new SupportingFile("build.properties", sourceFolder, "build.properties"));
|
||||
supportingFiles.add(new SupportingFile("build.xml", sourceFolder, "build.xml"));
|
||||
supportingFiles.add(new SupportingFile("AirExecutorApp-app.xml", sourceFolder + File.separatorChar + "bin", "AirExecutorApp-app.xml"));
|
||||
supportingFiles.add(new SupportingFile("ASAXB-0.1.1.swc", sourceFolder + File.separatorChar + "lib", "ASAXB-0.1.1.swc"));
|
||||
supportingFiles.add(new SupportingFile("as3corelib.swc", sourceFolder + File.separatorChar + "lib", "as3corelib.swc"));
|
||||
supportingFiles.add(new SupportingFile("flexunit-4.1.0_RC2-28-flex_3.5.0.12683.swc", sourceFolder + File.separator + "lib" + File.separator + "ext", "flexunit-4.1.0_RC2-28-flex_3.5.0.12683.swc"));
|
||||
supportingFiles.add(new SupportingFile("flexunit-aircilistener-4.1.0_RC2-28-3.5.0.12683.swc", sourceFolder + File.separator + "lib" + File.separator + "ext", "flexunit-aircilistener-4.1.0_RC2-28-3.5.0.12683.swc"));
|
||||
supportingFiles.add(new SupportingFile("flexunit-cilistener-4.1.0_RC2-28-3.5.0.12683.swc", sourceFolder + File.separator + "lib" + File.separator + "ext", "flexunit-cilistener-4.1.0_RC2-28-3.5.0.12683.swc"));
|
||||
supportingFiles.add(new SupportingFile("flexunit-core-flex-4.0.0.2-sdk3.5.0.12683.swc", sourceFolder + File.separator + "lib" + File.separator + "ext", "flexunit-core-flex-4.0.0.2-sdk3.5.0.12683.swc"));
|
||||
supportingFiles.add(new SupportingFile("AirExecutorApp-app.xml", sourceFolder + File.separatorChar
|
||||
+ "bin", "AirExecutorApp-app.xml"));
|
||||
supportingFiles.add(new SupportingFile("ASAXB-0.1.1.swc", sourceFolder + File.separatorChar
|
||||
+ "lib", "ASAXB-0.1.1.swc"));
|
||||
supportingFiles.add(new SupportingFile("as3corelib.swc", sourceFolder + File.separatorChar
|
||||
+ "lib", "as3corelib.swc"));
|
||||
supportingFiles.add(new SupportingFile("flexunit-4.1.0_RC2-28-flex_3.5.0.12683.swc", sourceFolder
|
||||
+ File.separator + "lib" + File.separator + "ext", "flexunit-4.1.0_RC2-28-flex_3.5.0.12683.swc"));
|
||||
supportingFiles.add(new SupportingFile("flexunit-aircilistener-4.1.0_RC2-28-3.5.0.12683.swc", sourceFolder
|
||||
+ File.separator + "lib" + File.separator + "ext", "flexunit-aircilistener-4.1.0_RC2-28-3.5.0.12683.swc"));
|
||||
supportingFiles.add(new SupportingFile("flexunit-cilistener-4.1.0_RC2-28-3.5.0.12683.swc", sourceFolder
|
||||
+ File.separator + "lib" + File.separator + "ext", "flexunit-cilistener-4.1.0_RC2-28-3.5.0.12683.swc"));
|
||||
supportingFiles.add(new SupportingFile("flexunit-core-flex-4.0.0.2-sdk3.5.0.12683.swc", sourceFolder
|
||||
+ File.separator + "lib" + File.separator + "ext", "flexunit-core-flex-4.0.0.2-sdk3.5.0.12683.swc"));
|
||||
}
|
||||
|
||||
private static String dropDots(String str) {
|
||||
@@ -163,11 +175,13 @@ public class FlashClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
|
||||
@Override
|
||||
public String apiFileFolder() {
|
||||
return (outputFolder + File.separatorChar + sourceFolder + File.separatorChar + apiPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar);
|
||||
return (outputFolder + File.separatorChar + sourceFolder + File.separatorChar
|
||||
+ apiPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar);
|
||||
}
|
||||
|
||||
public String modelFileFolder() {
|
||||
return (outputFolder + File.separatorChar + sourceFolder + File.separatorChar + modelPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar);
|
||||
return (outputFolder + File.separatorChar + sourceFolder + File.separatorChar
|
||||
+ modelPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -31,6 +31,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(JavaClientCodegen.class);
|
||||
public static final String FULL_JAVA_UTIL = "fullJavaUtil";
|
||||
|
||||
protected String invokerPackage = "io.swagger.client";
|
||||
protected String groupId = "io.swagger";
|
||||
@@ -84,7 +85,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC));
|
||||
cliOptions.add(new CliOption(CodegenConstants.LOCAL_VARIABLE_PREFIX, CodegenConstants.LOCAL_VARIABLE_PREFIX_DESC));
|
||||
cliOptions.add(new CliOption(CodegenConstants.SERIALIZABLE_MODEL, CodegenConstants.SERIALIZABLE_MODEL_DESC));
|
||||
cliOptions.add(new CliOption("fullJavaUtil", "whether to use fully qualified name for classes under java.util (default to false)"));
|
||||
cliOptions.add(new CliOption(FULL_JAVA_UTIL, "whether to use fully qualified name for classes under java.util (default to false)"));
|
||||
|
||||
supportedLibraries.put("<default>", "HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2");
|
||||
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.6");
|
||||
@@ -150,7 +151,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.SERIALIZABLE_MODEL)) {
|
||||
this.setSerializableModel(Boolean.valueOf((String)additionalProperties.get(CodegenConstants.SERIALIZABLE_MODEL).toString()));
|
||||
this.setSerializableModel(Boolean.valueOf(additionalProperties.get(CodegenConstants.SERIALIZABLE_MODEL).toString()));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.LIBRARY)) {
|
||||
@@ -160,13 +161,13 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
// need to put back serializableModel (boolean) into additionalProperties as value in additionalProperties is string
|
||||
additionalProperties.put(CodegenConstants.SERIALIZABLE_MODEL, serializableModel);
|
||||
|
||||
if (additionalProperties.containsKey("fullJavaUtil")) {
|
||||
fullJavaUtil = Boolean.valueOf(additionalProperties.get("fullJavaUtil").toString());
|
||||
if (additionalProperties.containsKey(FULL_JAVA_UTIL)) {
|
||||
this.setFullJavaUtil(Boolean.valueOf(additionalProperties.get(FULL_JAVA_UTIL).toString()));
|
||||
}
|
||||
if (fullJavaUtil) {
|
||||
javaUtilPrefix = "java.util.";
|
||||
}
|
||||
additionalProperties.put("fullJavaUtil", fullJavaUtil);
|
||||
additionalProperties.put(FULL_JAVA_UTIL, fullJavaUtil);
|
||||
additionalProperties.put("javaUtilPrefix", javaUtilPrefix);
|
||||
|
||||
if (fullJavaUtil) {
|
||||
@@ -378,7 +379,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
public String toOperationId(String operationId) {
|
||||
// throw exception if method name is empty
|
||||
if (StringUtils.isEmpty(operationId)) {
|
||||
throw new RuntimeException("Empty method name (operationId) not allowed");
|
||||
throw new RuntimeException("Empty method/operation name (operationId) not allowed");
|
||||
}
|
||||
|
||||
// method name cannot use reserved keyword, e.g. return
|
||||
@@ -507,6 +508,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
List<CodegenProperty> codegenProperties = codegenModel.vars;
|
||||
|
||||
// Iterate over all of the parent model properties
|
||||
boolean removedChildEnum = false;
|
||||
for (CodegenProperty parentModelCodegenPropery : parentModelCodegenProperties) {
|
||||
// Look for enums
|
||||
if (parentModelCodegenPropery.isEnum) {
|
||||
@@ -519,12 +521,21 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
// We found an enum in the child class that is
|
||||
// a duplicate of the one in the parent, so remove it.
|
||||
iterator.remove();
|
||||
removedChildEnum = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
codegenModel.vars = codegenProperties;
|
||||
|
||||
if(removedChildEnum) {
|
||||
// If we removed an entry from this model's vars, we need to ensure hasMore is updated
|
||||
int count = 0, numVars = codegenProperties.size();
|
||||
for(CodegenProperty codegenProperty : codegenProperties) {
|
||||
count += 1;
|
||||
codegenProperty.hasMore = (count < numVars) ? true : null;
|
||||
}
|
||||
codegenModel.vars = codegenProperties;
|
||||
}
|
||||
}
|
||||
|
||||
return codegenModel;
|
||||
@@ -572,4 +583,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
return packageName;
|
||||
}
|
||||
|
||||
public void setFullJavaUtil(boolean fullJavaUtil) {
|
||||
this.fullJavaUtil = fullJavaUtil;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,11 @@ import java.util.HashSet;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
public static final String VARIABLE_NAMING_CONVENTION = "variableNamingConvention";
|
||||
public static final String PACKAGE_PATH = "packagePath";
|
||||
public static final String SRC_BASE_PATH = "srcBasePath";
|
||||
public static final String COMPOSER_VENDOR_NAME = "composerVendorName";
|
||||
public static final String COMPOSER_PROJECT_NAME = "composerProjectName";
|
||||
protected String invokerPackage = "Swagger\\Client";
|
||||
protected String composerVendorName = "swagger";
|
||||
protected String composerProjectName = "swagger-client";
|
||||
@@ -86,12 +91,12 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
typeMapping.put("object", "object");
|
||||
typeMapping.put("DateTime", "\\DateTime");
|
||||
|
||||
cliOptions.add(new CliOption("variableNamingConvention", "naming convention of variable name, e.g. camelCase. Default: snake_case"));
|
||||
cliOptions.add(new CliOption(VARIABLE_NAMING_CONVENTION, "naming convention of variable name, e.g. camelCase. Default: snake_case"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.INVOKER_PACKAGE, "The main namespace to use for all classes. e.g. Yay\\Pets"));
|
||||
cliOptions.add(new CliOption("packagePath", "The main package name for classes. e.g. GeneratedPetstore"));
|
||||
cliOptions.add(new CliOption("srcBasePath", "The directory under packagePath to serve as source root."));
|
||||
cliOptions.add(new CliOption("composerVendorName", "The vendor name used in the composer package name. The template uses {{composerVendorName}}/{{composerProjectName}} for the composer package name. e.g. yaypets"));
|
||||
cliOptions.add(new CliOption("composerProjectName", "The project name used in the composer package name. The template uses {{composerVendorName}}/{{composerProjectName}} for the composer package name. e.g. petstore-client"));
|
||||
cliOptions.add(new CliOption(PACKAGE_PATH, "The main package name for classes. e.g. GeneratedPetstore"));
|
||||
cliOptions.add(new CliOption(SRC_BASE_PATH, "The directory under packagePath to serve as source root."));
|
||||
cliOptions.add(new CliOption(COMPOSER_VENDOR_NAME, "The vendor name used in the composer package name. The template uses {{composerVendorName}}/{{composerProjectName}} for the composer package name. e.g. yaypets"));
|
||||
cliOptions.add(new CliOption(COMPOSER_PROJECT_NAME, "The project name used in the composer package name. The template uses {{composerVendorName}}/{{composerProjectName}} for the composer package name. e.g. petstore-client"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.ARTIFACT_VERSION, "The version to use in the composer package version field. e.g. 1.2.3"));
|
||||
}
|
||||
|
||||
@@ -144,16 +149,16 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
public void processOpts() {
|
||||
super.processOpts();
|
||||
|
||||
if (additionalProperties.containsKey("packagePath")) {
|
||||
this.setPackagePath((String) additionalProperties.get("packagePath"));
|
||||
if (additionalProperties.containsKey(PACKAGE_PATH)) {
|
||||
this.setPackagePath((String) additionalProperties.get(PACKAGE_PATH));
|
||||
} else {
|
||||
additionalProperties.put("packagePath", packagePath);
|
||||
additionalProperties.put(PACKAGE_PATH, packagePath);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("srcBasePath")) {
|
||||
this.setSrcBasePath((String) additionalProperties.get("srcBasePath"));
|
||||
if (additionalProperties.containsKey(SRC_BASE_PATH)) {
|
||||
this.setSrcBasePath((String) additionalProperties.get(SRC_BASE_PATH));
|
||||
} else {
|
||||
additionalProperties.put("srcBasePath", srcBasePath);
|
||||
additionalProperties.put(SRC_BASE_PATH, srcBasePath);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.INVOKER_PACKAGE)) {
|
||||
@@ -162,28 +167,24 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
additionalProperties.put(CodegenConstants.INVOKER_PACKAGE, invokerPackage);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) {
|
||||
this.setModelPackage((String) additionalProperties.get(CodegenConstants.MODEL_PACKAGE));
|
||||
} else {
|
||||
if (!additionalProperties.containsKey(CodegenConstants.MODEL_PACKAGE)) {
|
||||
additionalProperties.put(CodegenConstants.MODEL_PACKAGE, modelPackage);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) {
|
||||
this.setApiPackage((String) additionalProperties.get(CodegenConstants.API_PACKAGE));
|
||||
} else {
|
||||
if (!additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) {
|
||||
additionalProperties.put(CodegenConstants.API_PACKAGE, apiPackage);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("composerProjectName")) {
|
||||
this.setComposerProjectName((String) additionalProperties.get("composerProjectName"));
|
||||
if (additionalProperties.containsKey(COMPOSER_PROJECT_NAME)) {
|
||||
this.setComposerProjectName((String) additionalProperties.get(COMPOSER_PROJECT_NAME));
|
||||
} else {
|
||||
additionalProperties.put("composerProjectName", composerProjectName);
|
||||
additionalProperties.put(COMPOSER_PROJECT_NAME, composerProjectName);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("composerVendorName")) {
|
||||
this.setComposerVendorName((String) additionalProperties.get("composerVendorName"));
|
||||
if (additionalProperties.containsKey(COMPOSER_VENDOR_NAME)) {
|
||||
this.setComposerVendorName((String) additionalProperties.get(COMPOSER_VENDOR_NAME));
|
||||
} else {
|
||||
additionalProperties.put("composerVendorName", composerVendorName);
|
||||
additionalProperties.put(COMPOSER_VENDOR_NAME, composerVendorName);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.ARTIFACT_VERSION)) {
|
||||
@@ -191,6 +192,10 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
} else {
|
||||
additionalProperties.put(CodegenConstants.ARTIFACT_VERSION, artifactVersion);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(VARIABLE_NAMING_CONVENTION)) {
|
||||
this.setParameterNamingConvention((String) additionalProperties.get(VARIABLE_NAMING_CONVENTION));
|
||||
}
|
||||
|
||||
additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\"));
|
||||
|
||||
@@ -286,7 +291,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
this.variableNamingConvention = variableNamingConvention;
|
||||
}
|
||||
|
||||
private void setComposerVendorName(String composerVendorName) {
|
||||
public void setComposerVendorName(String composerVendorName) {
|
||||
this.composerVendorName = composerVendorName;
|
||||
}
|
||||
|
||||
@@ -296,10 +301,6 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
@Override
|
||||
public String toVarName(String name) {
|
||||
if (additionalProperties.containsKey("variableNamingConvention")) {
|
||||
this.setParameterNamingConvention((String) additionalProperties.get("variableNamingConvention"));
|
||||
}
|
||||
|
||||
// sanitize name
|
||||
name = sanitizeName(name);
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.swagger.codegen.languages;
|
||||
|
||||
import io.swagger.codegen.CliOption;
|
||||
import io.swagger.codegen.CodegenConfig;
|
||||
import io.swagger.codegen.CodegenConstants;
|
||||
import io.swagger.codegen.CodegenType;
|
||||
import io.swagger.codegen.DefaultCodegen;
|
||||
import io.swagger.codegen.SupportingFile;
|
||||
@@ -62,30 +63,31 @@ public class PythonClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
"return", "def", "for", "lambda", "try"));
|
||||
|
||||
cliOptions.clear();
|
||||
cliOptions.add(new CliOption("packageName", "python package name (convention: snake_case), default: swagger_client"));
|
||||
cliOptions.add(new CliOption("packageVersion", "python package version, default: 1.0.0"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "python package name (convention: snake_case)," +
|
||||
" default: swagger_client"));
|
||||
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "python package version, default: 1.0.0"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processOpts() {
|
||||
super.processOpts();
|
||||
|
||||
if (additionalProperties.containsKey("packageName")) {
|
||||
setPackageName((String) additionalProperties.get("packageName"));
|
||||
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
|
||||
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
|
||||
}
|
||||
else {
|
||||
setPackageName("swagger_client");
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("packageVersion")) {
|
||||
setPackageVersion((String) additionalProperties.get("packageVersion"));
|
||||
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_VERSION)) {
|
||||
setPackageVersion((String) additionalProperties.get(CodegenConstants.PACKAGE_VERSION));
|
||||
}
|
||||
else {
|
||||
setPackageVersion("1.0.0");
|
||||
}
|
||||
|
||||
additionalProperties.put("packageName", packageName);
|
||||
additionalProperties.put("packageVersion", packageVersion);
|
||||
additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName);
|
||||
additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion);
|
||||
|
||||
String swaggerFolder = packageName;
|
||||
|
||||
|
||||
@@ -16,6 +16,9 @@ import java.util.HashSet;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
public static final String GEM_NAME = "gemName";
|
||||
public static final String MODULE_NAME = "moduleName";
|
||||
public static final String GEM_VERSION = "gemVersion";
|
||||
protected String gemName = null;
|
||||
protected String moduleName = null;
|
||||
protected String gemVersion = "1.0.0";
|
||||
@@ -68,20 +71,20 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
// remove modelPackage and apiPackage added by default
|
||||
cliOptions.clear();
|
||||
cliOptions.add(new CliOption("gemName", "gem name (convention: underscore_case), default: swagger_client"));
|
||||
cliOptions.add(new CliOption("moduleName", "top module name (convention: CamelCase, usually corresponding to gem name), default: SwaggerClient"));
|
||||
cliOptions.add(new CliOption("gemVersion", "gem version, default: 1.0.0"));
|
||||
cliOptions.add(new CliOption(GEM_NAME, "gem name (convention: underscore_case), default: swagger_client"));
|
||||
cliOptions.add(new CliOption(MODULE_NAME, "top module name (convention: CamelCase, usually corresponding to gem name), default: SwaggerClient"));
|
||||
cliOptions.add(new CliOption(GEM_VERSION, "gem version, default: 1.0.0"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processOpts() {
|
||||
super.processOpts();
|
||||
|
||||
if (additionalProperties.containsKey("gemName")) {
|
||||
setGemName((String) additionalProperties.get("gemName"));
|
||||
if (additionalProperties.containsKey(GEM_NAME)) {
|
||||
setGemName((String) additionalProperties.get(GEM_NAME));
|
||||
}
|
||||
if (additionalProperties.containsKey("moduleName")) {
|
||||
setModuleName((String) additionalProperties.get("moduleName"));
|
||||
if (additionalProperties.containsKey(MODULE_NAME)) {
|
||||
setModuleName((String) additionalProperties.get(MODULE_NAME));
|
||||
}
|
||||
|
||||
if (gemName == null && moduleName == null) {
|
||||
@@ -93,14 +96,14 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
setModuleName(generateModuleName(gemName));
|
||||
}
|
||||
|
||||
additionalProperties.put("gemName", gemName);
|
||||
additionalProperties.put("moduleName", moduleName);
|
||||
additionalProperties.put(GEM_NAME, gemName);
|
||||
additionalProperties.put(MODULE_NAME, moduleName);
|
||||
|
||||
if (additionalProperties.containsKey("gemVersion")) {
|
||||
setGemVersion((String) additionalProperties.get("gemVersion"));
|
||||
if (additionalProperties.containsKey(GEM_VERSION)) {
|
||||
setGemVersion((String) additionalProperties.get(GEM_VERSION));
|
||||
} else {
|
||||
// not set, pass the default value to template
|
||||
additionalProperties.put("gemVersion", gemVersion);
|
||||
additionalProperties.put(GEM_VERSION, gemVersion);
|
||||
}
|
||||
|
||||
// use constant model/api package (folder path)
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package io.swagger.codegen.languages;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
|
||||
import com.google.common.collect.Iterators;
|
||||
import com.google.common.collect.Lists;
|
||||
import io.swagger.codegen.*;
|
||||
import io.swagger.models.Swagger;
|
||||
import io.swagger.models.Model;
|
||||
import io.swagger.models.Operation;
|
||||
import io.swagger.models.parameters.HeaderParameter;
|
||||
@@ -13,6 +15,7 @@ import io.swagger.models.properties.MapProperty;
|
||||
import io.swagger.models.properties.Property;
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.WordUtils;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
@@ -234,7 +237,7 @@ public class SwiftCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
List<String> values = (List<String>) codegenProperty.allowableValues.get("values");
|
||||
for (String value : values) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("enum", StringUtils.capitalize(value));
|
||||
map.put("enum", toSwiftyEnumName(value));
|
||||
map.put("raw", value);
|
||||
swiftEnums.add(map);
|
||||
}
|
||||
@@ -248,6 +251,16 @@ public class SwiftCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
return codegenProperty;
|
||||
}
|
||||
|
||||
public String toSwiftyEnumName(String value) {
|
||||
// Prevent from breaking properly cased identifier
|
||||
if (value.matches("[A-Z][a-z0-9]+[a-zA-Z0-9]*")) {
|
||||
return value;
|
||||
}
|
||||
char[] separators = {'-', '_', ' '};
|
||||
return WordUtils.capitalizeFully(StringUtils.lowerCase(value), separators).replaceAll("[-_ ]", "");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toApiName(String name) {
|
||||
if(name.length() == 0)
|
||||
@@ -256,7 +269,7 @@ public class SwiftCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
|
||||
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
|
||||
path = normalizePath(path);
|
||||
List<Parameter> parameters = operation.getParameters();
|
||||
parameters = Lists.newArrayList(Iterators.filter(parameters.iterator(), new Predicate<Parameter>() {
|
||||
@@ -266,7 +279,7 @@ public class SwiftCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
}));
|
||||
operation.setParameters(parameters);
|
||||
return super.fromOperation(path, httpMethod, operation, definitions);
|
||||
return super.fromOperation(path, httpMethod, operation, definitions, swagger);
|
||||
}
|
||||
|
||||
private static String normalizePath(String path) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package io.swagger.codegen.languages;
|
||||
|
||||
import io.swagger.codegen.SupportingFile;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCodegen {
|
||||
@@ -12,7 +11,7 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Generates a TypeScript AngurlarJS client library.";
|
||||
return "Generates a TypeScript AngularJS client library.";
|
||||
}
|
||||
|
||||
public TypeScriptAngularClientCodegen() {
|
||||
@@ -25,4 +24,4 @@ public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCode
|
||||
modelPackage = "API.Client";
|
||||
supportingFiles.add(new SupportingFile("api.d.mustache", apiPackage().replace('.', File.separatorChar), "api.d.ts"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user