diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java index 1f66d31e75f5..46f071c1d0be 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java @@ -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); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/AbstractGoCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/AbstractGoCodegenTest.java index fc8c4d3e7925..a3f511ac2ec6 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/AbstractGoCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/AbstractGoCodegenTest.java @@ -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")