Skip to content

Commit

Permalink
Fixed the generation of model properties whose data type is a compose…
Browse files Browse the repository at this point in the history
…d (allOf) schema (OpenAPITools#704)

* OpenAPITools#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 OpenAPITools#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
  • Loading branch information
rubms authored and JFCote committed Aug 8, 2018
1 parent 9fb4c8c commit 5eb3347
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ public static Schema unaliasSchema(Map<String, Schema> 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())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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<String, Schema> allSchemas = new HashMap<>();
allSchemas.put("SomeComposedSchema", composedSchema);

Assert.assertEquals(refToComposedSchema, ModelUtils.unaliasSchema(allSchemas, refToComposedSchema));
}
}

0 comments on commit 5eb3347

Please sign in to comment.