forked from loafle/openapi-generator-original
Merge remote-tracking branch 'origin/develop_2.0' into java-auth
Conflicts: modules/swagger-codegen/src/main/resources/Java/apiInvoker.mustache samples/client/petstore/java/src/main/java/io/swagger/client/ApiInvoker.java
This commit is contained in:
@@ -3,6 +3,8 @@ package com.wordnik.swagger.codegen;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
@@ -12,12 +14,17 @@ import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.wordnik.swagger.codegen.examples.ExampleGenerator;
|
||||
import com.wordnik.swagger.models.ArrayModel;
|
||||
import com.wordnik.swagger.models.ComposedModel;
|
||||
import com.wordnik.swagger.models.Model;
|
||||
import com.wordnik.swagger.models.ModelImpl;
|
||||
import com.wordnik.swagger.models.Operation;
|
||||
@@ -53,8 +60,9 @@ import com.wordnik.swagger.models.properties.RefProperty;
|
||||
import com.wordnik.swagger.models.properties.StringProperty;
|
||||
import com.wordnik.swagger.util.Json;
|
||||
|
||||
|
||||
public class DefaultCodegen {
|
||||
Logger LOGGER = LoggerFactory.getLogger(DefaultCodegen.class);
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultCodegen.class);
|
||||
|
||||
protected String outputFolder = "";
|
||||
protected Set<String> defaultIncludes = new HashSet<String>();
|
||||
@@ -192,6 +200,7 @@ public class DefaultCodegen {
|
||||
}
|
||||
|
||||
public String toParamName(String name) {
|
||||
name = removeNonNameElementToCamelCase(name);
|
||||
if(reservedWords.contains(name)) {
|
||||
return escapeReservedWord(name);
|
||||
}
|
||||
@@ -466,100 +475,28 @@ public class DefaultCodegen {
|
||||
m.classVarName = toVarName(name);
|
||||
m.modelJson = Json.pretty(model);
|
||||
m.externalDocs = model.getExternalDocs();
|
||||
int count = 0;
|
||||
if(model instanceof ArrayModel) {
|
||||
ArrayModel am = (ArrayModel) model;
|
||||
ArrayProperty arrayProperty = new ArrayProperty(am.getItems());
|
||||
CodegenProperty cp = fromProperty(name, arrayProperty);
|
||||
if(cp.complexType != null && !defaultIncludes.contains(cp.complexType))
|
||||
m.imports.add(cp.complexType);
|
||||
m.parent = toInstantiationType(arrayProperty);
|
||||
String containerType = cp.containerType;
|
||||
if(instantiationTypes.containsKey(containerType))
|
||||
m.imports.add(instantiationTypes.get(containerType));
|
||||
if(typeMapping.containsKey(containerType)) {
|
||||
containerType = typeMapping.get(containerType);
|
||||
cp.containerType = containerType;
|
||||
m.imports.add(containerType);
|
||||
}
|
||||
addParentContainer(m, name, arrayProperty);
|
||||
}
|
||||
else if (model instanceof RefModel) {
|
||||
// TODO
|
||||
}
|
||||
else {
|
||||
} else if (model instanceof ComposedModel) {
|
||||
final ComposedModel composed = (ComposedModel) model;
|
||||
final RefModel parent = (RefModel) composed.getParent();
|
||||
final String parentModel = toModelName(parent.getSimpleRef());
|
||||
m.parent = parentModel;
|
||||
addImport(m, parentModel);
|
||||
final ModelImpl child = (ModelImpl) composed.getChild();
|
||||
addVars(m, child.getProperties(), child.getRequired());
|
||||
} else {
|
||||
ModelImpl impl = (ModelImpl) model;
|
||||
if(impl.getAdditionalProperties() != null) {
|
||||
MapProperty mapProperty = new MapProperty(impl.getAdditionalProperties());
|
||||
CodegenProperty cp = fromProperty(name, mapProperty);
|
||||
if(cp.complexType != null && !defaultIncludes.contains(cp.complexType))
|
||||
m.imports.add(cp.complexType);
|
||||
m.parent = toInstantiationType(mapProperty);
|
||||
String containerType = cp.containerType;
|
||||
if(instantiationTypes.containsKey(containerType))
|
||||
m.imports.add(instantiationTypes.get(containerType));
|
||||
if(typeMapping.containsKey(containerType)) {
|
||||
containerType = typeMapping.get(containerType);
|
||||
cp.containerType = containerType;
|
||||
m.imports.add(containerType);
|
||||
}
|
||||
}
|
||||
if(impl.getProperties() != null && impl.getProperties().size() > 0) {
|
||||
m.hasVars = true;
|
||||
m.hasEnums = false;
|
||||
for(String key: impl.getProperties().keySet()) {
|
||||
Property prop = impl.getProperties().get(key);
|
||||
|
||||
if(prop == null) {
|
||||
LOGGER.warn("null property for " + key);
|
||||
}
|
||||
else {
|
||||
CodegenProperty cp;
|
||||
try{
|
||||
cp = fromProperty(key, prop);
|
||||
}
|
||||
catch(Exception e) {
|
||||
System.out.println("failed to process model " + name);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
cp.required = null;
|
||||
if(impl.getRequired() != null) {
|
||||
for(String req : impl.getRequired()) {
|
||||
if(key.equals(req))
|
||||
cp.required = true;
|
||||
}
|
||||
}
|
||||
if(cp.complexType != null && !defaultIncludes.contains(cp.complexType)) {
|
||||
m.imports.add(cp.complexType);
|
||||
}
|
||||
m.vars.add(cp);
|
||||
count += 1;
|
||||
if (cp.isEnum)
|
||||
m.hasEnums = true;
|
||||
if(count != impl.getProperties().keySet().size())
|
||||
cp.hasMore = new Boolean(true);
|
||||
if(cp.isContainer != null) {
|
||||
String arrayImport = typeMapping.get("array");
|
||||
if(arrayImport != null &&
|
||||
!languageSpecificPrimitives.contains(arrayImport) &&
|
||||
!defaultIncludes.contains(arrayImport))
|
||||
m.imports.add(arrayImport);
|
||||
}
|
||||
|
||||
if(cp.complexType != null &&
|
||||
!languageSpecificPrimitives.contains(cp.complexType) &&
|
||||
!defaultIncludes.contains(cp.complexType))
|
||||
m.imports.add(cp.complexType);
|
||||
|
||||
if(cp.baseType != null &&
|
||||
!languageSpecificPrimitives.contains(cp.baseType) &&
|
||||
!defaultIncludes.contains(cp.baseType))
|
||||
m.imports.add(cp.baseType);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
m.emptyVars = true;
|
||||
addParentContainer(m, name, mapProperty);
|
||||
}
|
||||
addVars(m, impl.getProperties(), impl.getRequired());
|
||||
}
|
||||
return m;
|
||||
}
|
||||
@@ -718,6 +655,7 @@ public class DefaultCodegen {
|
||||
operationId = builder.toString();
|
||||
LOGGER.warn("generated operationId " + operationId);
|
||||
}
|
||||
operationId = removeNonNameElementToCamelCase(operationId);
|
||||
op.path = path;
|
||||
op.operationId = toOperationId(operationId);
|
||||
op.summary = escapeText(operation.getSummary());
|
||||
@@ -1141,6 +1079,62 @@ public class DefaultCodegen {
|
||||
co.baseName = tag;
|
||||
}
|
||||
|
||||
private void addParentContainer(CodegenModel m, String name, Property property) {
|
||||
final CodegenProperty tmp = fromProperty(name, property);
|
||||
addImport(m, tmp.complexType);
|
||||
m.parent = toInstantiationType(property);
|
||||
final String containerType = tmp.containerType;
|
||||
final String instantiationType = instantiationTypes.get(containerType);
|
||||
if (instantiationType != null) {
|
||||
addImport(m, instantiationType);
|
||||
}
|
||||
final String mappedType = typeMapping.get(containerType);
|
||||
if (mappedType != null) {
|
||||
addImport(m, mappedType);
|
||||
}
|
||||
}
|
||||
|
||||
private void addImport(CodegenModel m, String type) {
|
||||
if (type != null && !languageSpecificPrimitives.contains(type) && !defaultIncludes.contains(type)) {
|
||||
m.imports.add(type);
|
||||
}
|
||||
}
|
||||
|
||||
private void addVars(CodegenModel m, Map<String, Property> properties, Collection<String> required) {
|
||||
if (properties != null && properties.size() > 0) {
|
||||
m.hasVars = true;
|
||||
m.hasEnums = false;
|
||||
final int totalCount = properties.size();
|
||||
final Set<String> mandatory = required == null ? Collections.<String> emptySet() : new HashSet<String>(required);
|
||||
int count = 0;
|
||||
for (Map.Entry<String, Property> entry : properties.entrySet()) {
|
||||
final String key = entry.getKey();
|
||||
final Property prop = entry.getValue();
|
||||
|
||||
if (prop == null) {
|
||||
LOGGER.warn("null property for " + key);
|
||||
} else {
|
||||
final CodegenProperty cp = fromProperty(key, prop);
|
||||
cp.required = mandatory.contains(key) ? true : null;
|
||||
if (cp.isEnum) {
|
||||
m.hasEnums = true;
|
||||
}
|
||||
count += 1;
|
||||
if (count != totalCount)
|
||||
cp.hasMore = true;
|
||||
if (cp.isContainer != null) {
|
||||
addImport(m, typeMapping.get("array"));
|
||||
}
|
||||
addImport(m, cp.baseType);
|
||||
addImport(m, cp.complexType);
|
||||
m.vars.add(cp);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
m.emptyVars = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* 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
|
||||
*/
|
||||
@@ -1166,6 +1160,26 @@ public class DefaultCodegen {
|
||||
return word;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove characters not suitable for variable or method name from the input and camelize it
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
public String removeNonNameElementToCamelCase(String name) {
|
||||
String nonNameElementPattern = "[-_:;#]";
|
||||
name = StringUtils.join(Lists.transform(Lists.newArrayList(name.split(nonNameElementPattern)), new Function<String, String>() {
|
||||
@Nullable
|
||||
@Override
|
||||
public String apply(String input) {
|
||||
return StringUtils.capitalize(input);
|
||||
}
|
||||
}), "");
|
||||
if (name.length() > 0) {
|
||||
name = name.substring(0, 1).toLowerCase() + name.substring(1);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
public static String camelize(String word) {
|
||||
return camelize(word, false);
|
||||
}
|
||||
|
||||
@@ -83,6 +83,7 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
typeMapping.put("file", "string"); // path to file
|
||||
typeMapping.put("array", "List");
|
||||
typeMapping.put("map", "Dictionary");
|
||||
typeMapping.put("object", "Object");
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,250 @@
|
||||
package com.wordnik.swagger.codegen.languages;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Iterators;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.wordnik.swagger.codegen.*;
|
||||
import com.wordnik.swagger.models.Model;
|
||||
import com.wordnik.swagger.models.Operation;
|
||||
import com.wordnik.swagger.models.parameters.HeaderParameter;
|
||||
import com.wordnik.swagger.models.parameters.Parameter;
|
||||
import com.wordnik.swagger.models.properties.*;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
import java.io.File;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class SwiftGenerator extends DefaultCodegen implements CodegenConfig {
|
||||
private static final Pattern PATH_PARAM_PATTERN = Pattern.compile("\\{[a-zA-Z_]+\\}");
|
||||
protected String sourceFolder = "Classes/Swaggers";
|
||||
|
||||
public CodegenType getTag() {
|
||||
return CodegenType.CLIENT;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return "swift";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Generates a swift client library.";
|
||||
}
|
||||
|
||||
public SwiftGenerator() {
|
||||
super();
|
||||
outputFolder = "generated-code/swift";
|
||||
modelTemplateFiles.put("model.mustache", ".swift");
|
||||
apiTemplateFiles.put("api.mustache", ".swift");
|
||||
templateDir = "swift";
|
||||
apiPackage = "/APIs";
|
||||
modelPackage = "/Models";
|
||||
|
||||
// Inject application name
|
||||
String appName = System.getProperty("appName");
|
||||
if (appName == null) {
|
||||
appName = "SwaggerClient";
|
||||
}
|
||||
additionalProperties.put("projectName", appName);
|
||||
|
||||
// Inject base url override
|
||||
String basePathOverride = System.getProperty("basePathOverride");
|
||||
if (basePathOverride != null) {
|
||||
additionalProperties.put("basePathOverride", basePathOverride);
|
||||
}
|
||||
|
||||
sourceFolder = appName + "/" + sourceFolder;
|
||||
|
||||
supportingFiles.add(new SupportingFile("Cartfile.mustache", "", "Cartfile"));
|
||||
supportingFiles.add(new SupportingFile("APIHelper.mustache", sourceFolder, "APIHelper.swift"));
|
||||
supportingFiles.add(new SupportingFile("AlamofireImplementations.mustache", sourceFolder, "AlamofireImplementations.swift"));
|
||||
supportingFiles.add(new SupportingFile("Extensions.mustache", sourceFolder, "Extensions.swift"));
|
||||
supportingFiles.add(new SupportingFile("Models.mustache", sourceFolder, "Models.swift"));
|
||||
supportingFiles.add(new SupportingFile("APIs.mustache", sourceFolder, "APIs.swift"));
|
||||
|
||||
languageSpecificPrimitives = new HashSet<String>(
|
||||
Arrays.asList(
|
||||
"Int",
|
||||
"Float",
|
||||
"Double",
|
||||
"Bool",
|
||||
"Void",
|
||||
"String",
|
||||
"Character")
|
||||
);
|
||||
defaultIncludes = new HashSet<String>(
|
||||
Arrays.asList(
|
||||
"NSDate",
|
||||
"Array",
|
||||
"Dictionary",
|
||||
"Set",
|
||||
"Any",
|
||||
"Empty",
|
||||
"AnyObject")
|
||||
);
|
||||
reservedWords = new HashSet<String>(
|
||||
Arrays.asList(
|
||||
"class", "break", "as", "associativity", "deinit", "case", "dynamicType", "convenience", "enum", "continue",
|
||||
"false", "dynamic", "extension", "default", "is", "didSet", "func", "do", "nil", "final", "import", "else",
|
||||
"self", "get", "init", "fallthrough", "Self", "infix", "internal", "for", "super", "inout", "let", "if",
|
||||
"true", "lazy", "operator", "in", "COLUMN", "left", "private", "return", "FILE", "mutating", "protocol",
|
||||
"switch", "FUNCTION", "none", "public", "where", "LINE", "nonmutating", "static", "while", "optional",
|
||||
"struct", "override", "subscript", "postfix", "typealias", "precedence", "var", "prefix", "Protocol",
|
||||
"required", "right", "set", "Type", "unowned", "weak")
|
||||
);
|
||||
|
||||
typeMapping = new HashMap<String, String>();
|
||||
typeMapping.put("array", "Array");
|
||||
typeMapping.put("List", "Array");
|
||||
typeMapping.put("map", "Dictionary");
|
||||
typeMapping.put("date", "NSDate");
|
||||
typeMapping.put("Date", "NSDate");
|
||||
typeMapping.put("DateTime", "NSDate");
|
||||
typeMapping.put("boolean", "Bool");
|
||||
typeMapping.put("string", "String");
|
||||
typeMapping.put("char", "Character");
|
||||
typeMapping.put("short", "Int");
|
||||
typeMapping.put("int", "Int");
|
||||
typeMapping.put("long", "Int");
|
||||
typeMapping.put("integer", "Int");
|
||||
typeMapping.put("Integer", "Int");
|
||||
typeMapping.put("float", "Float");
|
||||
typeMapping.put("number", "Double");
|
||||
typeMapping.put("double", "Double");
|
||||
typeMapping.put("object", "AnyObject");
|
||||
typeMapping.put("file", "NSData");
|
||||
|
||||
importMapping = new HashMap<String, String>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String escapeReservedWord(String name) {
|
||||
return "_" + name; // add an underscore to the name
|
||||
}
|
||||
|
||||
@Override
|
||||
public String modelFileFolder() {
|
||||
return outputFolder + "/" + sourceFolder + modelPackage().replace('.', File.separatorChar);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apiFileFolder() {
|
||||
return outputFolder + "/" + sourceFolder + apiPackage().replace('.', File.separatorChar);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTypeDeclaration(Property p) {
|
||||
if (p instanceof ArrayProperty) {
|
||||
ArrayProperty ap = (ArrayProperty) p;
|
||||
Property inner = ap.getItems();
|
||||
return "[" + getTypeDeclaration(inner) + "]";
|
||||
} else if (p instanceof MapProperty) {
|
||||
MapProperty mp = (MapProperty) p;
|
||||
Property inner = mp.getAdditionalProperties();
|
||||
return "[String:" + getTypeDeclaration(inner) + "]";
|
||||
}
|
||||
return super.getTypeDeclaration(p);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSwaggerType(Property p) {
|
||||
String swaggerType = super.getSwaggerType(p);
|
||||
String type = null;
|
||||
if (typeMapping.containsKey(swaggerType)) {
|
||||
type = typeMapping.get(swaggerType);
|
||||
if (languageSpecificPrimitives.contains(type))
|
||||
return toModelName(type);
|
||||
} else
|
||||
type = swaggerType;
|
||||
return toModelName(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toDefaultValue(Property p) {
|
||||
// nil
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toInstantiationType(Property p) {
|
||||
if (p instanceof MapProperty) {
|
||||
MapProperty ap = (MapProperty) p;
|
||||
String inner = getSwaggerType(ap.getAdditionalProperties());
|
||||
return "[String:" + inner + "]";
|
||||
} else if (p instanceof ArrayProperty) {
|
||||
ArrayProperty ap = (ArrayProperty) p;
|
||||
String inner = getSwaggerType(ap.getItems());
|
||||
return "[" + inner + "]";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodegenProperty fromProperty(String name, Property p) {
|
||||
CodegenProperty codegenProperty = super.fromProperty(name, p);
|
||||
if (codegenProperty.isEnum) {
|
||||
List<Map<String, String>> swiftEnums = new ArrayList<Map<String, String>>();
|
||||
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("raw", value);
|
||||
swiftEnums.add(map);
|
||||
}
|
||||
codegenProperty.allowableValues.put("values", swiftEnums);
|
||||
codegenProperty.datatypeWithEnum =
|
||||
StringUtils.left(codegenProperty.datatypeWithEnum, codegenProperty.datatypeWithEnum.length() - "Enum".length());
|
||||
}
|
||||
return codegenProperty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toApiName(String name) {
|
||||
if(name.length() == 0)
|
||||
return "DefaultAPI";
|
||||
return initialCaps(name) + "API";
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions) {
|
||||
path = normalizePath(path);
|
||||
List<Parameter> parameters = operation.getParameters();
|
||||
parameters = Lists.newArrayList(Iterators.filter(parameters.iterator(), new Predicate<Parameter>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable Parameter parameter) {
|
||||
return !(parameter instanceof HeaderParameter);
|
||||
}
|
||||
}));
|
||||
operation.setParameters(parameters);
|
||||
return super.fromOperation(path, httpMethod, operation, definitions);
|
||||
}
|
||||
|
||||
private static String normalizePath(String path) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
int cursor = 0;
|
||||
Matcher matcher = PATH_PARAM_PATTERN.matcher(path);
|
||||
boolean found = matcher.find();
|
||||
while (found) {
|
||||
String stringBeforeMatch = path.substring(cursor, matcher.start());
|
||||
builder.append(stringBeforeMatch);
|
||||
|
||||
String group = matcher.group().substring(1, matcher.group().length() - 1);
|
||||
group = camelize(group, true);
|
||||
builder
|
||||
.append("{")
|
||||
.append(group)
|
||||
.append("}");
|
||||
|
||||
cursor = matcher.end();
|
||||
found = matcher.find();
|
||||
}
|
||||
|
||||
String stringAfterMatch = path.substring(cursor);
|
||||
builder.append(stringAfterMatch);
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user