[BUG][Java] useOneOfInterfaces generates incompatible discriminator return types for inline enums
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
- numberGenerated 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.StringExpected 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
- #12412 fixed enum discriminator discovery for one schema arrangement, but comments after its closure report that other arrangements still generate the incompatible
Stringreturn type. - #22541 / #24158 fixed discriminator discovery through
oneOf→allOfinheritance.
The schema above does not use allOf, and the problem remains reproducible with 7.25.0.
Source: OpenAPITools/openapi-generator