You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Animal:
type: objectproperties:
# Define a pet_type property of type "string" with no enum constraint.# It's just a plain string with no enum constraint.pet_type:
type: stringCat:
allOf:
- $ref: '#/components/schemas/Animal'
- type: objectproperties:
pet_type:
# Add a 'enum' constraint to the pet_type property and a 'default' value.# This is useful because it specifies there is only one possible value for this schema.# The code generator should be able to leverage this piece of information to generate# a Java class constructor where the 'pet_type' property is initialized to 'Cat' by default.# Without the enum constraint, there is no way for the code generator to determine# what are the possible values of 'pet_type'.# Without the enum, the developer creating a 'Cat' object using Java would be forced# to explicitly set the value of 'pet_type'. That would be both surprising and tedious# because humans know there is only one possible value.enum:
- Catdefault: Cat
publicclassCatextendsAnimal {
// Overrides getPetType from super class, but the return value is not compatible.publicPetTypeEnumgetPetType() {
returnpetType;
}
This causes the following compilation error:
Compilation failure
[ERROR] error: getPetType() in Cat cannot override getPetType() in Animal
[ERROR] returntype PetTypeEnum is not compatible with String
Proposed changes:
Detect when property is redefined in 'allOf'/'oneOf'/'anyOf' child.
Detect which generated methods are overridden in java subclasses, e.g. getPetType is overridden. Ensure the generated methods have the same signature.
Do not redefine field in sub-class. Java fields cannot be overridden in a subclass. If a field is redefined in a subclass with the same name as a field in the superclass, the field in the subclass will hide (shadow) the field in the superclass. If the subclass tries to access the field, it will access the field in the subclass. However, if the subclass calls up into a method in the superclass, and that method accesses the field with the same name as in the subclass, it is the field in the superclass that is accessed.
Bug Report Checklist
Description
The generated Java client code with the jersey2 library fails compilation when:
The same problem is likely to occur with 'oneOf' and 'anyOf', though I have not tested these particular scenarios.
openapi-generator version
openapi master branch as of August 19th 2020
OpenAPI declaration file content or url
Here is a minimal OpenAPI doc to reproduce the problem. The full OpenAPI doc is available in PR #7251 at https://github.com/OpenAPITools/openapi-generator/pull/7251/files#diff-aa56ad872f3213179c27da9d73dec49aR2090
Generation Details
Steps to reproduce
Related issues/PRs
I have raised PR #7251 to show how the generated code is failing.
Suggest a fix
First, let's look at the problems:
@Overrideannotation in the generated code.The code for the parent and child class is generated as shown below.
This causes the following compilation error:
Proposed changes:
getPetTypeis overridden. Ensure the generated methods have the same signature.