mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-04 06:30:52 +00:00
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:
commit
f1304022b0
@ -426,14 +426,6 @@ public class DefaultCodegen {
|
||||
return dp.getDefault().toString();
|
||||
}
|
||||
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 {
|
||||
return "null";
|
||||
}
|
||||
|
@ -205,6 +205,18 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
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
|
||||
public String getSwaggerType(Property p) {
|
||||
String swaggerType = super.getSwaggerType(p);
|
||||
|
@ -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()
|
||||
.description("a sample model")
|
||||
.property("translations", new MapProperty()
|
||||
@ -152,6 +152,26 @@ class JavaModelTest extends FlatSpec with Matchers {
|
||||
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 {
|
||||
val model = new ModelImpl()
|
||||
.description("a sample model")
|
||||
|
Loading…
x
Reference in New Issue
Block a user