diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index 2bb6481c49cb..c5afa2dd8b18 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -648,7 +648,7 @@ public static Schema unaliasSchema(Map allSchemas, Schema schema } else if (isStringSchema(ref) && (ref.getEnum() != null && !ref.getEnum().isEmpty())) { // top-level enum class return schema; - } else if (isMapSchema(ref) || isArraySchema(ref)) { // map/array def should be created as models + } else if (isMapSchema(ref) || isArraySchema(ref) || isComposedSchema(ref)) { // map/array def should be created as models return schema; } else { return unaliasSchema(allSchemas, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref()))); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java index ac9648e0243e..e09d27dbb950 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java @@ -19,10 +19,7 @@ import io.swagger.parser.OpenAPIParser; 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.StringSchema; +import io.swagger.v3.oas.models.media.*; import io.swagger.v3.oas.models.parameters.Parameter; import io.swagger.v3.oas.models.parameters.RequestBody; import io.swagger.v3.oas.models.responses.ApiResponse; @@ -32,7 +29,10 @@ import org.testng.Assert; import org.testng.annotations.Test; +import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class ModelUtilsTest { @@ -173,4 +173,24 @@ public void testReferencedParameter() { Parameter result2 = ModelUtils.getReferencedParameter(openAPI, new Parameter().$ref("#/components/parameters/OtherParameter")); Assert.assertEquals(result2, otherParameter); } + + /** + * Issue https://github.com/OpenAPITools/openapi-generator/issues/582. + * Composed schemas should not get unaliased when generating model properties, in order to properly + * generate the property data type name. + */ + @Test + public void testComposedSchemasAreNotUnaliased() { + ComposedSchema composedSchema = new ComposedSchema().allOf(Arrays.asList( + new Schema<>().$ref("#/components/schemas/SomeSchema"), + new ObjectSchema() + )); + Schema refToComposedSchema = new Schema().$ref("#/components/schemas/SomeComposedSchema"); + + + Map allSchemas = new HashMap<>(); + allSchemas.put("SomeComposedSchema", composedSchema); + + Assert.assertEquals(refToComposedSchema, ModelUtils.unaliasSchema(allSchemas, refToComposedSchema)); + } }