[BUG] If superclass has no properties, it will not be generated, but derived class does extend not existing/generated class
Description
I use the openapi gradle-plugin to generate java classes - but if the superclass has no properties, the class will not be generated but the derived class extends the non existing class. So the code does not compile.
Using the command-line I see the following message:
Model Pet not generated since it's a free-form object
As a workaround we modify the openapi json to add some dummy attribute in the superclass, so that it will be generated.
openapi-generator version
org.openapi.generator 4.3.1
OpenAPI declaration file content or url
{
"openapi": "3.0.1",
"info": {
"title": "Abstract Generation problem",
"description": "Abstract Generation problem",
"version": "1.0.0-SNAPSHOT"
},
"servers": [],
"paths": {},
"components": {
"schemas": {
"Pet": {
"title": "AbstractPet",
"type": "object",
"description": "model containing all the details of a pet",
"discriminator": {
"propertyName": "petTypeName"
}
},
"Dog": {
"title": "DogDetails",
"description": "The model containing the details of the dog",
"allOf": [
{
"$ref": "#/components/schemas/Pet"
},
{
"title": "DogDetails",
"type": "object",
"properties": {
"legs": {
"type": "string",
"description": "the number of legs"
}
},
"description": "The model containing the details of the dog"
}
]
}
}
}
}
Generation Details
java -jar openapi-generator-cli-4.3.1.jar generate -i openapi.json -g spring -o ./temp
Generated Models
There were two java classes generated:
- Dog.java
- DogDetails.java
Dog.java does extend Pet, that does not exist!
public class Dog extends Pet {
@JsonProperty("legs")
private String legs;
public Dog legs(String legs) {
this.legs = legs;
return this;
}
...
Workaround
change definition of Pet to:
"Pet": {
"title": "AbstractPet",
"type": "object",
"properties": {
"dummyProperty": {
"type": "string",
"description": "Workaround - OpenAPI generator does not consider definitions without properties"
}
},
"description": "model containing all the details of a pet",
"discriminator": {
"propertyName": "petTypeName"
}
},
👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.
The team will review the labels and make any necessary changes.
Will this fix be merged? I have the same problem in my project.
Are there any updates on this issue? As far as I can see, this is still broken in 6.0.1.
same issue here
same here
absolutely the same issue here.
Same issue here - is there an update on this issue and will this fix be merged?
There are two workarounds (at least for the spring-generator) for generating a Java class for an object type that has no properties:
- Add the
allOfkey with an empty array, e.g.,
MyBaseType:
type: object
discriminator:
propertyName: '@type'
required:
- '@type'
allOf: []
- Use the
x-is-free-formvendor extension with the value "false" (but omit thetype: objectvalidation in order to avoid generator warning messages), e.g.,
MyBaseType:
discriminator:
propertyName: '@type'
required:
- '@type'
x-is-free-form: false
@wing328 please fix this issue, it's very impactful