Skip to content

[BUG] [Java] useOneOfInterfaces for array type leads to useless interface #23494

@Nicklas2751

Description

@Nicklas2751

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?

Description

When useOneOfInterfaces=true is set and a oneOf schema consists of array variants (e.g. List<VariantA> | List<VariantB>), the generator produces an empty, unusable Java interface:

public interface MyRequest {
}

This interface is useless because Java's List<T> cannot implement a user-defined interface retroactively. The API method parameter has type MyRequest, but there is no valid value that can be passed ? the generated code cannot be used at all.

Without useOneOfInterfaces, the generator correctly produces a working wrapper class:

public class MyRequest extends AbstractOpenApiSchema { ... }

This wrapper holds either a List<VariantA> or List<VariantB> and includes proper Gson serialization/deserialization logic.

The interface approach only makes sense for object-type oneOf variants, where the concrete classes can implement the generated interface:

public class VariantA implements MyRequest { ... }
public class VariantB implements MyRequest { ... }
openapi-generator version

7.x (confirmed on latest master)

OpenAPI declaration file content or url
components:
  schemas:
    MyRequest:
      oneOf:
        - type: array
          items:
            $ref: '#/components/schemas/VariantA'
        - type: array
          items:
            $ref: '#/components/schemas/VariantB'
    VariantA:
      type: object
      properties:
        fieldA:
          type: string
    VariantB:
      type: object
      properties:
        fieldB:
          type: integer
Generation Details
openapi-generator generate \
  -i spec.yaml \
  -g java \
  --library okhttp-gson \
  --additional-properties useOneOfInterfaces=true
Steps to reproduce
  1. Use the spec above with useOneOfInterfaces=true and any Java library.
  2. Inspect the generated MyRequest.java ? it is an empty interface.
  3. Try to call the generated API method ? no valid argument can be constructed.
Suggest a fix

When generating a oneOf interface model, check whether all variants are array types. If so, skip interface generation and fall back to the existing AbstractOpenApiSchema wrapper class, which already handles this case correctly.

The check can be added in DefaultCodegen.addOneOfInterfaceModel() or in preprocessOpenAPI() where oneOf schemas are scanned: if every entry in cm.oneOf resolves to an array schema, do not add the model to addOneOfInterfaces and do not add it to oneOfInterfaceNames.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions