OpenAPITools / OpenAPITools/openapi-generator
[BUG] [Java] useOneOfInterfaces for array type leads to useless interface
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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
- Use the spec above with
useOneOfInterfaces=trueand any Java library. - Inspect the generated
MyRequest.java? it is an empty interface. - 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in DefaultCodegen.addOneOfInterfaceModel() and the preprocessOpenAPI() path that scans oneOf schemas. Reproduce the Java okhttp-gson output from the provided YAML and inspect the generated MyRequest.java. Done means array-only oneOf schemas use the existing AbstractOpenApiSchema wrapper instead of an empty interface, while object variants retain interface generation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100