From 61a439ede200f03550b234ec75b0fe21c95abdf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Mart=C3=ADnez?= Date: Wed, 1 Aug 2018 11:05:34 +0200 Subject: [PATCH 1/4] #582 Fixed the generation of model properties whose data type is a composed (allOf) schema. Before this fix, the data type name of the generated property was that of the first model participating in the allOf clause. After this fix the property data type is again as expected: the one of the composed schema and not one of its parents. --- .../main/java/org/openapitools/codegen/utils/ModelUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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()))); From 470c3ba40412669f2b7784df2589aadfb7a7ceb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Mart=C3=ADnez?= Date: Wed, 1 Aug 2018 12:10:12 +0200 Subject: [PATCH 2/4] Added unit test in order to have regression testing in the fix for the #582 issue (references to composed schemas should not get unaliased for them to get a proper data type name in the generation of model properties). --- .../codegen/utils/ModelUtilsTest.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) 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..7f44bdb30328 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)); + } } From 4b2af3d0dcc3b1b765c1644b9f02c51c4032f64e Mon Sep 17 00:00:00 2001 From: Ruben Martinez Date: Wed, 1 Aug 2018 13:21:57 +0000 Subject: [PATCH 3/4] Run ./bin/utils/ensure-up-to-date to re-generate samples run in the CI. --- samples/server/petstore/php-slim/phpunit.xml.dist | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/server/petstore/php-slim/phpunit.xml.dist b/samples/server/petstore/php-slim/phpunit.xml.dist index 93af3ee9b134..4a44f5ac1462 100644 --- a/samples/server/petstore/php-slim/phpunit.xml.dist +++ b/samples/server/petstore/php-slim/phpunit.xml.dist @@ -19,8 +19,8 @@ - ./lib//Api - ./lib//Model + ./lib/Api + ./lib/Model \ No newline at end of file From 3dd0af052484f3385556d07f866f1cfc967e287c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Mart=C3=ADnez?= Date: Wed, 1 Aug 2018 16:01:07 +0200 Subject: [PATCH 4/4] Removed tabs from ModelUtilsTest.java --- .../java/org/openapitools/codegen/utils/ModelUtilsTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 7f44bdb30328..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 @@ -173,7 +173,7 @@ 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 @@ -192,5 +192,5 @@ public void testComposedSchemasAreNotUnaliased() { allSchemas.put("SomeComposedSchema", composedSchema); Assert.assertEquals(refToComposedSchema, ModelUtils.unaliasSchema(allSchemas, refToComposedSchema)); - } + } }