OpenAPITools / OpenAPITools/openapi-generator

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

Open
#24,959 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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
  • #12412 fixed enum discriminator discovery for one schema arrangement, but comments after its closure report that other arrangements still generate the incompatible String return type.
  • #22541 / #24158 fixed discriminator discovery through oneOfallOf inheritance.

The schema above does not use allOf, and the problem remains reproducible with 7.25.0.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the minimal OpenAPI schema with the Java generator, restclient library, and useOneOfInterfaces=true. Trace Java generator handling of the oneOf discriminator and compare the generated interface and implementation getters; done means the generated models compile with compatible return types.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
70/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.