From db77e070d18b4aae3276c4b3c1eddb473d2fd922 Mon Sep 17 00:00:00 2001 From: Hippolyte HENRY Date: Thu, 21 Nov 2019 03:30:11 +0100 Subject: [PATCH] [Java][Go] Unalias schema in getTypeDeclaration when not generating model for alias (#4527) * [Java][Go] Unalias schema in getTypeDeclaration when not generating model for alias * Add tests * Don't use isGenerateAliasAsModel --- .../codegen/languages/AbstractGoCodegen.java | 4 +- .../languages/AbstractJavaCodegen.java | 4 +- .../codegen/go/AbstractGoCodegenTest.java | 39 +++++++++++++++++++ .../codegen/java/AbstractJavaCodegenTest.java | 32 +++++++++++++-- 4 files changed, 72 insertions(+), 7 deletions(-) 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 5a2ccc8154d0..83f9c5511c2b 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 @@ -278,10 +278,10 @@ public String getTypeDeclaration(Schema p) { if (ModelUtils.isArraySchema(p)) { ArraySchema ap = (ArraySchema) p; Schema inner = ap.getItems(); - return "[]" + getTypeDeclaration(inner); + return "[]" + getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, inner)); } else if (ModelUtils.isMapSchema(p)) { Schema inner = ModelUtils.getAdditionalProperties(p); - return getSchemaType(p) + "[string]" + getTypeDeclaration(inner); + return getSchemaType(p) + "[string]" + getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, inner)); } //return super.getTypeDeclaration(p); diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java index 823278ca949a..4d99af1c0584 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java @@ -689,7 +689,7 @@ public String toModelFilename(String name) { public String getTypeDeclaration(Schema p) { if (ModelUtils.isArraySchema(p)) { Schema items = getSchemaItems((ArraySchema) p); - return getSchemaType(p) + "<" + getTypeDeclaration(items) + ">"; + return getSchemaType(p) + "<" + getTypeDeclaration(ModelUtils.unaliasSchema(this.openAPI, items)) + ">"; } else if (ModelUtils.isMapSchema(p)) { Schema inner = ModelUtils.getAdditionalProperties(p); if (inner == null) { @@ -697,7 +697,7 @@ public String getTypeDeclaration(Schema p) { inner = new StringSchema().description("TODO default missing map inner type to string"); p.setAdditionalProperties(inner); } - return getSchemaType(p) + ""; + return getSchemaType(p) + ""; } 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 3663571d413e..50805b447223 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 @@ -17,9 +17,17 @@ package org.openapitools.codegen.go; +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.media.ArraySchema; +import io.swagger.v3.oas.models.media.IntegerSchema; +import io.swagger.v3.oas.models.media.MapSchema; +import io.swagger.v3.oas.models.media.Schema; import org.openapitools.codegen.CodegenConstants; import org.openapitools.codegen.CodegenType; +import org.openapitools.codegen.java.AbstractJavaCodegenTest; import org.openapitools.codegen.languages.AbstractGoCodegen; +import org.openapitools.codegen.utils.ModelUtils; import org.testng.Assert; import org.testng.annotations.Test; @@ -54,6 +62,37 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception { Assert.assertEquals(codegen.isHideGenerationTimestamp(), true); } + @Test + public void getTypeDeclarationTest() { + final AbstractGoCodegen codegen = new P_AbstractGoCodegen(); + + // Create an alias to an array schema + Schema nestedArraySchema = new ArraySchema().items(new IntegerSchema().format("int32")); + codegen.setOpenAPI(new OpenAPI().components(new Components().addSchemas("NestedArray", nestedArraySchema))); + + // Create an array schema with item type set to the array alias + Schema schema = new ArraySchema().items(new Schema().$ref("#/components/schemas/NestedArray")); + + ModelUtils.setGenerateAliasAsModel(false); + String defaultValue = codegen.getTypeDeclaration(schema); + Assert.assertEquals(defaultValue, "[][]int32"); + + ModelUtils.setGenerateAliasAsModel(true); + defaultValue = codegen.getTypeDeclaration(schema); + Assert.assertEquals(defaultValue, "[]NestedArray"); + + // Create a map schema with additionalProperties type set to array alias + schema = new MapSchema().additionalProperties(new Schema().$ref("#/components/schemas/NestedArray")); + + ModelUtils.setGenerateAliasAsModel(false); + defaultValue = codegen.getTypeDeclaration(schema); + Assert.assertEquals(defaultValue, "map[string][]int32"); + + ModelUtils.setGenerateAliasAsModel(true); + defaultValue = codegen.getTypeDeclaration(schema); + Assert.assertEquals(defaultValue, "map[string]NestedArray"); + } + private static class P_AbstractGoCodegen extends AbstractGoCodegen { @Override public CodegenType getTag() { diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java index 73ee84e92659..32478998bd73 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/AbstractJavaCodegenTest.java @@ -17,15 +17,15 @@ package org.openapitools.codegen.java; +import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; -import io.swagger.v3.oas.models.media.IntegerSchema; -import io.swagger.v3.oas.models.media.ObjectSchema; -import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.oas.models.media.*; import org.openapitools.codegen.CodegenConstants; import org.openapitools.codegen.CodegenType; import org.openapitools.codegen.TestUtils; import org.openapitools.codegen.languages.AbstractJavaCodegen; +import org.openapitools.codegen.utils.ModelUtils; import org.testng.Assert; import org.testng.annotations.Test; @@ -420,6 +420,32 @@ public void getTypeDeclarationTest() { Schema schema = createObjectSchemaWithMinItems(); String defaultValue = codegen.getTypeDeclaration(schema); Assert.assertEquals(defaultValue, "Object"); + + // Create an alias to an array schema + Schema nestedArraySchema = new ArraySchema().items(new IntegerSchema().format("int32")); + codegen.setOpenAPI(new OpenAPI().components(new Components().addSchemas("NestedArray", nestedArraySchema))); + + // Create an array schema with item type set to the array alias + schema = new ArraySchema().items(new Schema().$ref("#/components/schemas/NestedArray")); + + ModelUtils.setGenerateAliasAsModel(false); + defaultValue = codegen.getTypeDeclaration(schema); + Assert.assertEquals(defaultValue, "List>"); + + ModelUtils.setGenerateAliasAsModel(true); + defaultValue = codegen.getTypeDeclaration(schema); + Assert.assertEquals(defaultValue, "List"); + + // Create a map schema with additionalProperties type set to array alias + schema = new MapSchema().additionalProperties(new Schema().$ref("#/components/schemas/NestedArray")); + + ModelUtils.setGenerateAliasAsModel(false); + defaultValue = codegen.getTypeDeclaration(schema); + Assert.assertEquals(defaultValue, "Map>"); + + ModelUtils.setGenerateAliasAsModel(true); + defaultValue = codegen.getTypeDeclaration(schema); + Assert.assertEquals(defaultValue, "Map"); } @Test