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); usage(options);
return; return;
} }
try{ try{
codegenInput codegenInput
.opts(clientArgs) .opts(clientArgs)

View File

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

View File

@ -7,7 +7,7 @@ import java.util.*;
public class CodegenProperty { public class CodegenProperty {
public String baseName, complexType, getter, setter, description, datatype, 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 Double minimum, maximum, exclusiveMinimum, exclusiveMaximum;
public Boolean hasMore = null, required = null, secondaryParam = null; public Boolean hasMore = null, required = null, secondaryParam = null;
public Boolean isPrimitiveType, isContainer, isNotContainer; public Boolean isPrimitiveType, isContainer, isNotContainer;

View File

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

View File

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

View File

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