added subclass for ArrayModel support #273, removed path param brackets for auto-generated operationId #274

This commit is contained in:
Tony Tam 2014-09-23 21:44:09 -07:00
parent 5bc907f1d4
commit ba94dc3d68
6 changed files with 44 additions and 17 deletions

View File

@ -42,7 +42,6 @@ public class Codegen extends DefaultGenerator {
usage(options);
return;
}
try{
codegenInput
.opts(clientArgs)

View File

@ -6,6 +6,7 @@ import com.wordnik.swagger.models.properties.*;
import java.util.*;
class CodegenModel {
public String parent;
public String name, classname, description;
public String defaultValue;
public List<CodegenProperty> vars = new ArrayList<CodegenProperty>();

View File

@ -7,7 +7,7 @@ import java.util.*;
public class CodegenProperty {
public String baseName, complexType, getter, setter, description, datatype,
name, min, max, defaultValue, baseType;
name, min, max, defaultValue, baseType, containerType;
public Double minimum, maximum, exclusiveMinimum, exclusiveMaximum;
public Boolean hasMore = null, required = null, secondaryParam = null;
public Boolean isPrimitiveType, isContainer, isNotContainer;

View File

@ -12,6 +12,7 @@ public class DefaultCodegen {
protected String outputFolder = "";
protected Set<String> defaultIncludes = new HashSet<String>();
protected Map<String, String> typeMapping = new HashMap<String, String>();
protected Map<String, String> instantiationTypes = new HashMap<String, String>();
protected Set<String> reservedWords = new HashSet<String>();
protected Set<String> languageSpecificPrimitives = new HashSet<String>();
protected Map<String, String> importMapping = new HashMap<String, String>();
@ -153,7 +154,6 @@ public class DefaultCodegen {
);
typeMapping = new HashMap<String, String>();
typeMapping.put("Array", "List");
typeMapping.put("array", "List");
typeMapping.put("List", "List");
typeMapping.put("boolean", "Boolean");
@ -169,6 +169,9 @@ public class DefaultCodegen {
typeMapping.put("object", "Object");
typeMapping.put("integer", "Integer");
instantiationTypes = new HashMap<String, String>();
instantiationTypes.put("array", "ArrayList");
reservedWords = new HashSet<String> (
Arrays.asList(
"abstract", "continue", "for", "new", "switch", "assert",
@ -196,6 +199,21 @@ public class DefaultCodegen {
importMapping.put("LocalTime", "org.joda.time.*");
}
public String toInstantiationType(Property p) {
if (p instanceof MapProperty) {
MapProperty ap = (MapProperty) p;
String inner = getSwaggerType(ap.getAdditionalProperties());
return "HashMap<String, " + inner + ">";
}
else if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
String inner = getSwaggerType(ap.getItems());
return "ArrayList<" + inner + ">";
}
else
return null;
}
public String toDefaultValue(Property p) {
if(p instanceof StringProperty)
return "null";
@ -290,16 +308,24 @@ public class DefaultCodegen {
m.name = name;
m.description = model.getDescription();
m.classname = toModelName(name);
int count = 0;
if(model instanceof ArrayModel) {
ArrayModel am = (ArrayModel) model;
ArrayProperty arrayProperty = new ArrayProperty(am.getItems());
CodegenProperty cp = fromProperty(name, arrayProperty);
m.vars.add(cp);
// m.vars.add(cp);
if(cp.complexType != null && !defaultIncludes.contains(cp.complexType))
m.imports.add(cp.complexType);
m.parent = cp.baseType;
String containerType = cp.containerType;
if(typeMapping.containsKey(containerType)) {
containerType = typeMapping.get(containerType);
cp.containerType = containerType;
m.imports.add(containerType);
}
}
else if (model instanceof RefModel) {
// TODO
}
else {
ModelImpl impl = (ModelImpl) model;
@ -389,9 +415,11 @@ public class DefaultCodegen {
if(p instanceof ArrayProperty) {
property.isContainer = true;
property.containerType = "array";
ArrayProperty ap = (ArrayProperty) p;
CodegenProperty cp = fromProperty("inner", ap.getItems());
property.baseType = cp.baseType;
property.baseType = toInstantiationType(p);
if(!languageSpecificPrimitives.contains(cp.baseType))
property.complexType = cp.baseType;
}
@ -409,7 +437,8 @@ public class DefaultCodegen {
String operationId = operation.getOperationId();
if(operationId == null) {
operationId = path.replaceAll("/", "") + "_" + httpMethod;
path = path.replaceAll("\\{", "");
path = path.replaceAll("\\}", "");
String[] parts = (path + "/" + httpMethod).split("/");
StringBuilder builder = new StringBuilder();
for(int i = 0; i < parts.length; i++) {
@ -596,7 +625,6 @@ public class DefaultCodegen {
name = getTypeDeclaration(name);
}
p.dataType = name;
}
}
p.paramName = toParamName(bp.getName());

View File

@ -247,13 +247,11 @@ public class DefaultGenerator implements Generator {
Set<String> allImports = new HashSet<String>();
for(String key: definitions.keySet()) {
Model mm = definitions.get(key);
if(mm instanceof ModelImpl) {
CodegenModel cm = config.fromModel(key, (ModelImpl) mm);
Map<String, Object> mo = new HashMap<String, Object>();
mo.put("model", cm);
models.add(mo);
allImports.addAll(cm.imports);
}
CodegenModel cm = config.fromModel(key, mm);
Map<String, Object> mo = new HashMap<String, Object>();
mo.put("model", cm);
models.add(mo);
allImports.addAll(cm.imports);
}
objs.put("models", models);

View File

@ -8,7 +8,7 @@ package {{package}};
/**
* {{description}}
**/{{/description}}
public class {{classname}} { {{#vars}}
public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} { {{#vars}}
/**{{#description}}
* {{{description}}}{{/description}}
* required: {{required}}{{#minimum}}
@ -33,6 +33,7 @@ public class {{classname}} { {{#vars}}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class {{classname}} {\n");
{{#parent}}sb.append(" " + super.toString()).append("\n");{{/parent}}
{{#vars}}sb.append(" {{name}}: ").append({{name}}).append("\n");
{{/vars}}sb.append("}\n");
return sb.toString();