Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}