Fixed the generation of model properties whose data type is a composed (allOf) schema (#704)

* #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.

* 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).

* Run ./bin/utils/ensure-up-to-date to re-generate samples run in the CI.

* Removed tabs from ModelUtilsTest.java
This commit is contained in:
Rubén Martínez 2018-08-08 19:06:42 +02:00 committed by Jean-François Côté
parent 06263d7606
commit d0ccac5663
2 changed files with 25 additions and 5 deletions

View File

@ -648,7 +648,7 @@ public class ModelUtils {
} else if (isStringSchema(ref) && (ref.getEnum() != null && !ref.getEnum().isEmpty())) { } else if (isStringSchema(ref) && (ref.getEnum() != null && !ref.getEnum().isEmpty())) {
// top-level enum class // top-level enum class
return schema; 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; return schema;
} else { } else {
return unaliasSchema(allSchemas, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref()))); return unaliasSchema(allSchemas, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())));

View File

@ -19,10 +19,7 @@ package org.openapitools.codegen.utils;
import io.swagger.parser.OpenAPIParser; import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.IntegerSchema; import io.swagger.v3.oas.models.media.*;
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.parameters.Parameter; import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody; import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.oas.models.responses.ApiResponse;
@ -32,7 +29,10 @@ import org.openapitools.codegen.TestUtils;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class ModelUtilsTest { public class ModelUtilsTest {
@ -173,4 +173,24 @@ public class ModelUtilsTest {
Parameter result2 = ModelUtils.getReferencedParameter(openAPI, new Parameter().$ref("#/components/parameters/OtherParameter")); Parameter result2 = ModelUtils.getReferencedParameter(openAPI, new Parameter().$ref("#/components/parameters/OtherParameter"));
Assert.assertEquals(result2, 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<String, Schema> allSchemas = new HashMap<>();
allSchemas.put("SomeComposedSchema", composedSchema);
Assert.assertEquals(refToComposedSchema, ModelUtils.unaliasSchema(allSchemas, refToComposedSchema));
}
} }