mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-06-03 15:30:59 +00:00
fix: ensure superclass model is generated when using discriminator without properties
Previously, models acting as superclasses with only a discriminator and no defined properties were incorrectly treated as free-form objects, preventing class generation. This fix updates the free-form object detection logic to correctly handle such cases, ensuring base models are generated when a discriminator is present. Closes #7638
This commit is contained in:
parent
d6c4634269
commit
5686b9c0c3
@ -881,7 +881,7 @@ public class ModelUtils {
|
|||||||
} else if (schema.getAdditionalProperties() instanceof JsonSchema) {
|
} else if (schema.getAdditionalProperties() instanceof JsonSchema) {
|
||||||
return true;
|
return true;
|
||||||
} else if (schema.getTypes() != null) {
|
} else if (schema.getTypes() != null) {
|
||||||
if (schema.getTypes().size() == 1) { // types = [object]
|
if (schema.getTypes().size() == 1 && schema.getDiscriminator() == null) { // types = [object]
|
||||||
return SchemaTypeUtil.OBJECT_TYPE.equals(schema.getTypes().iterator().next());
|
return SchemaTypeUtil.OBJECT_TYPE.equals(schema.getTypes().iterator().next());
|
||||||
} else { // has more than 1 type, e.g. types = [integer, string]
|
} else { // has more than 1 type, e.g. types = [integer, string]
|
||||||
return false;
|
return false;
|
||||||
|
@ -4815,6 +4815,25 @@ public class DefaultCodegenTest {
|
|||||||
assertEquals(openAPI, codegen.openAPI);
|
assertEquals(openAPI, codegen.openAPI);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParentModelGeneratedEvenWithoutProperties() throws Exception {
|
||||||
|
File output = Files.createTempDirectory("test_7638_").toFile();
|
||||||
|
|
||||||
|
final CodegenConfigurator configurator = new CodegenConfigurator()
|
||||||
|
.setGeneratorName("java")
|
||||||
|
.setInputSpec("src/test/resources/3_1/issue_7638.yaml")
|
||||||
|
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
|
||||||
|
|
||||||
|
final ClientOptInput clientOptInput = configurator.toClientOptInput();
|
||||||
|
DefaultGenerator generator = new DefaultGenerator();
|
||||||
|
List<File> files = generator.opts(clientOptInput).generate();
|
||||||
|
|
||||||
|
TestUtils.ensureContainsFile(files, output, "src/main/java/org/openapitools/client/model/Pet.java");
|
||||||
|
TestUtils.ensureContainsFile(files, output, "src/main/java/org/openapitools/client/model/Dog.java");
|
||||||
|
|
||||||
|
output.deleteOnExit();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReferencedEnumType() {
|
public void testReferencedEnumType() {
|
||||||
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue-5676-enums.yaml");
|
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue-5676-enums.yaml");
|
||||||
|
@ -0,0 +1,34 @@
|
|||||||
|
{
|
||||||
|
"openapi": "3.1.0",
|
||||||
|
"info": {
|
||||||
|
"title": "Parent Model Generated Even Without Properties",
|
||||||
|
"version": "1.0.0-SNAPSHOT"
|
||||||
|
},
|
||||||
|
"servers": [ ],
|
||||||
|
"paths": { },
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"Pet": {
|
||||||
|
"type": "object",
|
||||||
|
"discriminator": {
|
||||||
|
"propertyName": "petType"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Dog": {
|
||||||
|
"allOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/components/schemas/Pet"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"legs": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user