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 9608b46ad68b..5117cbf67d03 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 @@ -83,7 +83,9 @@ public AbstractGoCodegen() { "complex64", "complex128", "rune", - "byte") + "byte", + "interface{}" + ) ); instantiationTypes.clear(); @@ -298,12 +300,19 @@ public String getSchemaType(Schema p) { if (ref != null && !ref.isEmpty()) { type = openAPIType; - } else if (typeMapping.containsKey(openAPIType)) { - type = typeMapping.get(openAPIType); - if (languageSpecificPrimitives.contains(type)) - return (type); - } else - type = openAPIType; + } else { + // Handle "any type" as an empty interface + if (openAPIType.equals("object") && p != null && !ModelUtils.isObjectSchema(p) && !ModelUtils.isMapSchema(p)) { + return "interface{}"; + } + + if (typeMapping.containsKey(openAPIType)) { + type = typeMapping.get(openAPIType); + if (languageSpecificPrimitives.contains(type)) + return (type); + } else + type = openAPIType; + } return type; } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoModelTest.java index 2eea2e8b244e..254ab6d4026f 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoModelTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoModelTest.java @@ -47,15 +47,17 @@ public void simpleModelTest() { .addProperties("id", new IntegerSchema().format(SchemaTypeUtil.INTEGER64_FORMAT)) .addProperties("name", new StringSchema()) .addProperties("createdAt", new DateTimeSchema()) + .addProperties("anyValue", new Schema()) .addRequiredItem("id") - .addRequiredItem("name"); + .addRequiredItem("name") + .addRequiredItem("anyValue"); final DefaultCodegen codegen = new GoClientCodegen(); final CodegenModel cm = codegen.fromModel("sample", model, Collections.singletonMap("sample", model)); Assert.assertEquals(cm.name, "sample"); Assert.assertEquals(cm.classname, "Sample"); Assert.assertEquals(cm.description, "a sample model"); - Assert.assertEquals(cm.vars.size(), 3); + Assert.assertEquals(cm.vars.size(), 4); Assert.assertEquals(cm.imports.size(), 1); final CodegenProperty property1 = cm.vars.get(0); @@ -85,10 +87,21 @@ public void simpleModelTest() { Assert.assertEquals(property3.name, "CreatedAt"); Assert.assertNull(property3.defaultValue); Assert.assertEquals(property3.baseType, "time.Time"); - Assert.assertFalse(property3.hasMore); + Assert.assertTrue(property3.hasMore); Assert.assertFalse(property3.required); + + final CodegenProperty property4 = cm.vars.get(3); + Assert.assertEquals(property4.baseName, "anyValue"); + Assert.assertEquals(property4.baseType, "interface{}"); + Assert.assertEquals(property4.dataType, "interface{}"); + Assert.assertTrue(property1.isPrimitiveType); + Assert.assertEquals(property4.name, "AnyValue"); + Assert.assertNull(property4.defaultValue); + Assert.assertFalse(property4.hasMore); + Assert.assertTrue(property4.required); } + @Test(description = "convert a model with list property") public void listPropertyTest() { final Schema model = new Schema()