Description
With useOneOfInterfaces=true, the Java client generator can produce code that does not compile when the members of a discriminated oneOf define the discriminator as inline enums.
The generated one-of interface declares:
public interface SchemaProperty {
String getType();
}
But its generated implementations declare their own enum return types:
public TypeEnum getType() {
return type;
}
This fails compilation because TypeEnum is not covariant with String.
OpenAPI Generator version
7.25.0
Generator configuration
- Generator:
java
- Library:
restclient
useOneOfInterfaces=true
Minimal OpenAPI schema
openapi: 3.0.3
info:
title: OneOf discriminator reproduction
version: 1.0.0
paths: {}
components:
schemas:
SchemaProperty:
discriminator:
propertyName: type
mapping:
array: '#/components/schemas/ArrayProperty'
string: '#/components/schemas/BasicProperty'
number: '#/components/schemas/BasicProperty'
oneOf:
- $ref: '#/components/schemas/ArrayProperty'
- $ref: '#/components/schemas/BasicProperty'
ArrayProperty:
type: object
required:
- type
- items
properties:
type:
type: string
enum:
- array
items:
$ref: '#/components/schemas/SchemaProperty'
BasicProperty:
type: object
required:
- type
properties:
type:
type: string
enum:
- string
- number
Generated result
The generated interface has:
public interface SchemaProperty {
String getType();
}
The generated implementations have methods such as:
public ArrayProperty.TypeEnum getType()
and:
public BasicProperty.TypeEnum getType()
Compilation consequently fails with errors such as:
ArrayProperty is not abstract and does not override abstract method getType() in SchemaProperty
getType() in ArrayProperty cannot implement getType() in SchemaProperty
return type ArrayProperty.TypeEnum is not compatible with java.lang.String
Expected behavior
The generated models should compile when useOneOfInterfaces=true.
The interface discriminator getter must use a return type compatible with all generated implementations. For example, generating Object getType() avoids the incompatible return types, although there may be a more strongly typed solution.
Related issues
The schema above does not use allOf, and the problem remains reproducible with 7.25.0.
Description
With
useOneOfInterfaces=true, the Java client generator can produce code that does not compile when the members of a discriminatedoneOfdefine the discriminator as inline enums.The generated one-of interface declares:
But its generated implementations declare their own enum return types:
This fails compilation because
TypeEnumis not covariant withString.OpenAPI Generator version
7.25.0
Generator configuration
javarestclientuseOneOfInterfaces=trueMinimal OpenAPI schema
Generated result
The generated interface has:
The generated implementations have methods such as:
and:
Compilation consequently fails with errors such as:
Expected behavior
The generated models should compile when
useOneOfInterfaces=true.The interface discriminator getter must use a return type compatible with all generated implementations. For example, generating
Object getType()avoids the incompatible return types, although there may be a more strongly typed solution.Related issues
Stringreturn type.oneOf→allOfinheritance.The schema above does not use
allOf, and the problem remains reproducible with 7.25.0.