openapi-generator: [BUG] No more inheritance for components generated by version 5.0.0
Description
No schema inheritance.
openapi-generator version
5.0.0
OpenAPI declaration file content or url
schemas:
UserDTO:
type: object
properties:
id:
type: string
minLength: 1
maxLength: 20
QueryUserResponseDTO:
type: object
allOf:
- $ref: '#/components/schemas/UserDTO'
Generation Details
The above YAML produces:
public class QueryUserResponseDTO {
private @Valid String id;
}
Instead as for the 4.3.1:
public class QueryUserResponseDTO extends UserDTO {
}
Suggest a fix
Does exist a configuration parameter to enforce the DTO inheritance generation? Or it is a bug?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 16
- Comments: 21 (10 by maintainers)
The problem is that the base class can’t always know all the classes that will extend it. In my case, #9756 it isn’t even necessary as the base class isn’t used for Serialization/Deserialization. But I need the base class for methods that operate on the fields it provides. In my environment we have a “BasePage” object for paged requests. All the services that need paging extend that. BasePage can’t possibly know all the services that will support paging as it is in a common library that the services import. This change has made it impossible to support this.
Just to confirm. I updated from 4.3.1 -> 5.0.0 and I’m seeing the same issue (with dotnet-core generator) and AllOf inheritance. For us, this makes vers 5.0.0 unusable.
If any help, here is a git diff of 4.3.1 generated class and 5.0.0 generated class which shows the issue:
Hi all, I’ve filed https://github.com/OpenAPITools/openapi-generator/pull/14172 to allow using $ref as parent in allOf with a new option called “openapi-normalizer”.
Please give it a try as follows:
We hit the same issue with model composition as described here: https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/
@antonio-petricca I’m getting this from the log when running the generator:
I havn’t had time to dive into it fully yet (we’ve reverted to 4.3.1 for now) but from what I can gather it seems that my swagger.json doesnt have the discriminator field on the supertype. However I’m using asp.net swashbuckle to generate the swagger.json/.yaml which is setup to OAS3. So what I havn’t figured out yet is if the problem really is the yaml not being generated correctly by swashbuckle (i.e. missing the discriminator field). I cant seem to get it to generate the discriminator field. (yet - working on it when I have time)
You need to actually add a discriminator to your base class and then add the annotation [SwaggerDiscriminator(“discriminatorPropertyName”)] as well. That has worked for me.
I am going to revert to 4.3.1. 😦