Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,23 @@ public String getTypeDeclaration(Schema p) {
return "[]" + typDecl;
} else if (ModelUtils.isMapSchema(p)) {
Schema inner = ModelUtils.getAdditionalProperties(p);
return getSchemaType(p) + "[string]" + getTypeDeclaration(unaliasSchema(inner));
if (inner != null) {
inner = unaliasSchema(inner);
}
String typDecl;
if (inner != null) {
typDecl = getTypeDeclaration(inner);
} else {
typDecl = "interface{}";
}

// when nullable and the type of the map isn't nullable already (maps, slices, ...): make it a pointer
if (inner != null && Boolean.TRUE.equals(inner.getNullable()) && !typDecl.startsWith("map") && !typDecl.startsWith("[]")) {
typDecl = "*" + typDecl;
}

return getSchemaType(p) + "[string]" + typDecl;

}

//return super.getTypeDeclaration(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,35 @@ public void getTypeDeclarationTest() {
ModelUtils.setGenerateAliasAsModel(false);
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "map[string]interface{}");

// Create object schema with nullable set to false
ModelUtils.setGenerateAliasAsModel(false);
schema = new ObjectSchema().additionalProperties(new StringSchema().nullable(false));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "map[string]string");

// Create object schema with nullable set to true
ModelUtils.setGenerateAliasAsModel(false);
schema = new ObjectSchema().additionalProperties(new StringSchema().nullable(true));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "map[string]*string");

// slice in object schema is no pointer, even if nullable (slices are already nullable by default in Golang)
ModelUtils.setGenerateAliasAsModel(false);
schema = new ObjectSchema().additionalProperties(new ArraySchema().nullable(true).items(new IntegerSchema().format("int32")));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "map[string][]int32");

// map in nested object schema is no pointer, even if nullable (maps are already nullable by default in Golang)
ModelUtils.setGenerateAliasAsModel(false);
schema = new ObjectSchema().additionalProperties(new ObjectSchema().nullable(true));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "map[string]map[string]interface{}");

// nested object schema with nullable set to true (maps itself are already nullable by default in Golang, so no pointer type needed there)
schema = new ObjectSchema().additionalProperties(new ObjectSchema().nullable(true).additionalProperties(new StringSchema().nullable(true)));
defaultValue = codegen.getTypeDeclaration(schema);
Assert.assertEquals(defaultValue, "map[string]map[string]*string");
}

@Test(description = "test that os import is added for array of binary parameters in operations")
Expand Down
Loading