[BUG] [Java] inheritance is generated without discriminator field
Created by: vitaliysapounov
Description
We see that inheritance (instead of composition) is generated where, as we think, it should not.
Namely, we have the following model file:
openapi: "3.0.2"
info:
version: "1.0.0"
title: Test
# We only care about schemas, but this prevents codegen error: "attribute paths is missing"
paths:
/dummy:
get:
responses:
'200':
description: OK
components:
schemas:
PreventInheritance:
description: We declare dummy dependency on this empty class to prevent inheritance
Base:
properties:
base1:
type: integer
base2:
type: string
Inheritance:
allOf:
- $ref: "#/components/schemas/Base"
- properties:
# doses
Inheritance1:
type: integer
Inheritance2:
type: string
NoInheritance:
allOf:
- $ref: "#/components/schemas/Base"
- $ref: "#/components/schemas/PreventInheritance"
- properties:
NoInheritance1:
type: integer
NoInheritance2:
type: string
The following two classes are of interest:
- Basically,
Inheritance
class is declared as having all the properties ofBase
and some additional. Note that there is no discriminator defined. Thus, since there is no discriminator, our understanding that composition should be used, not inheritance. In the generated Java class we see that inheritance is generated, however:
public class Inheritance extends Base implements Serializable {
OBSERVED: inheritance is generated EXPECTED: composition should be used, as there is no discriminator field (?)
- To prevent generating of inheritance, we used a hack: in
NoInheritance
class we add not onlyBase
but also emptyPreventInheritance
. It seems that in this case, the generator chooses to use composition (why?):
public class NoInheritance implements Serializable {
OBSERVED: inheritance is not generated EXPECTED: ? (we are confused what should be used, composition or inheritance, the spec is unclear in that regard)
openapi-generator version
Gradle plugin org.openapitools:openapi-generator-gradle-plugin:4.2.2
(latest as of this writing)
OpenAPI declaration file content or url
Please see above
Command line used for generation
./gradlew build
Steps to reproduce
- Unpack the attached Gradle project
- Run
./gradlew build
Related issues/PRs
Suggest a fix
We are not sure how/why inheritance vs composition should be selected, as per the spec. But, the current behavior described above seems to be very confusing (the generator seems to choose inheritance in one case and composition in another case, for no clear reason). test_openapi_inheritance.zip