Skip to content

Commit d0ccac5

Browse files
rubmsJFCote
authored andcommitted
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
1 parent 06263d7 commit d0ccac5

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ public static Schema unaliasSchema(Map<String, Schema> allSchemas, Schema schema
648648
} else if (isStringSchema(ref) && (ref.getEnum() != null && !ref.getEnum().isEmpty())) {
649649
// top-level enum class
650650
return schema;
651-
} else if (isMapSchema(ref) || isArraySchema(ref)) { // map/array def should be created as models
651+
} else if (isMapSchema(ref) || isArraySchema(ref) || isComposedSchema(ref)) { // map/array def should be created as models
652652
return schema;
653653
} else {
654654
return unaliasSchema(allSchemas, allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())));

modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919

2020
import io.swagger.parser.OpenAPIParser;
2121
import io.swagger.v3.oas.models.OpenAPI;
22-
import io.swagger.v3.oas.models.media.IntegerSchema;
23-
import io.swagger.v3.oas.models.media.ObjectSchema;
24-
import io.swagger.v3.oas.models.media.Schema;
25-
import io.swagger.v3.oas.models.media.StringSchema;
22+
import io.swagger.v3.oas.models.media.*;
2623
import io.swagger.v3.oas.models.parameters.Parameter;
2724
import io.swagger.v3.oas.models.parameters.RequestBody;
2825
import io.swagger.v3.oas.models.responses.ApiResponse;
@@ -32,7 +29,10 @@
3229
import org.testng.Assert;
3330
import org.testng.annotations.Test;
3431

32+
import java.util.Arrays;
33+
import java.util.HashMap;
3534
import java.util.List;
35+
import java.util.Map;
3636

3737
public class ModelUtilsTest {
3838

@@ -173,4 +173,24 @@ public void testReferencedParameter() {
173173
Parameter result2 = ModelUtils.getReferencedParameter(openAPI, new Parameter().$ref("#/components/parameters/OtherParameter"));
174174
Assert.assertEquals(result2, otherParameter);
175175
}
176+
177+
/**
178+
* Issue https://github.com/OpenAPITools/openapi-generator/issues/582.
179+
* Composed schemas should not get unaliased when generating model properties, in order to properly
180+
* generate the property data type name.
181+
*/
182+
@Test
183+
public void testComposedSchemasAreNotUnaliased() {
184+
ComposedSchema composedSchema = new ComposedSchema().allOf(Arrays.asList(
185+
new Schema<>().$ref("#/components/schemas/SomeSchema"),
186+
new ObjectSchema()
187+
));
188+
Schema refToComposedSchema = new Schema().$ref("#/components/schemas/SomeComposedSchema");
189+
190+
191+
Map<String, Schema> allSchemas = new HashMap<>();
192+
allSchemas.put("SomeComposedSchema", composedSchema);
193+
194+
Assert.assertEquals(refToComposedSchema, ModelUtils.unaliasSchema(allSchemas, refToComposedSchema));
195+
}
176196
}

0 commit comments

Comments
 (0)