Merge pull request #852 from tomtit/issue837

Fixed #837: Generation of default values for complex arrays and maps has been improved.
This commit is contained in:
wing328 2015-07-21 11:28:43 +08:00
commit f1304022b0
3 changed files with 38 additions and 14 deletions

View File

@ -426,14 +426,6 @@ public class DefaultCodegen {
return dp.getDefault().toString(); return dp.getDefault().toString();
} }
return "null"; return "null";
} else if (p instanceof MapProperty) {
MapProperty ap = (MapProperty) p;
String inner = getSwaggerType(ap.getAdditionalProperties());
return "new HashMap<String, " + inner + ">() ";
} else if (p instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) p;
String inner = getSwaggerType(ap.getItems());
return "new ArrayList<" + inner + ">() ";
} else { } else {
return "null"; return "null";
} }

View File

@ -205,6 +205,18 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
return super.getTypeDeclaration(p); return super.getTypeDeclaration(p);
} }
@Override
public String toDefaultValue(Property p) {
if (p instanceof ArrayProperty) {
final ArrayProperty ap = (ArrayProperty) p;
return String.format("new ArrayList<%s>()", getTypeDeclaration(ap.getItems()));
} else if (p instanceof MapProperty) {
final MapProperty ap = (MapProperty) p;
return String.format("new HashMap<String, %s>()", getTypeDeclaration(ap.getAdditionalProperties()));
}
return super.toDefaultValue(p);
}
@Override @Override
public String getSwaggerType(Property p) { public String getSwaggerType(Property p) {
String swaggerType = super.getSwaggerType(p); String swaggerType = super.getSwaggerType(p);

View File

@ -122,7 +122,7 @@ class JavaModelTest extends FlatSpec with Matchers {
} }
ignore should "convert a model with a map with complex list property" in { it should "convert a model with a map with complex list property" in {
val model = new ModelImpl() val model = new ModelImpl()
.description("a sample model") .description("a sample model")
.property("translations", new MapProperty() .property("translations", new MapProperty()
@ -152,6 +152,26 @@ class JavaModelTest extends FlatSpec with Matchers {
vars.get(0).isContainer should equal(true) vars.get(0).isContainer should equal(true)
} }
it should "convert a model with a 2D list property" in {
val model = new ModelImpl().name("sample").property("list2D", new ArrayProperty().items(
new ArrayProperty().items(new RefProperty("Pet"))))
val codegen = new JavaClientCodegen()
val cm = codegen.fromModel("sample", model)
val vars = cm.vars
vars.size should be (1)
val list = vars.get(0)
list.baseName should be("list2D")
list.getter should be("getList2D")
list.setter should be("setList2D")
list.datatype should be("List<List<Pet>>")
list.name should be("list2D")
list.defaultValue should be ("new ArrayList<List<Pet>>()")
list.baseType should be("List")
list.containerType should be("array")
list.required should equal(null)
list.isContainer should equal(true)
}
it should "convert a model with complex properties" in { it should "convert a model with complex properties" in {
val model = new ModelImpl() val model = new ModelImpl()
.description("a sample model") .description("a sample model")