Skip to content

[BUG][Java] useOneOfInterfaces generates incompatible discriminator return types for inline enums #24959

Description

@Pattrigue

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions