OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] OneOf polymorphism code wasn't generated correctly

Open
#7,414 4 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Issue: Bug
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 (example)?
  • 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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I used open-api generator for creating Java client. In one of the endpoint, I used oneOf which must follow polymorphism property to generate codes. It generated a OneOfBody empty interface and two classes ObjectA and ObjectB which implements OneOfBody. So ideally the endpoint's method should expect the OneOfBody parameter but it expects a Body parameter which is an empty class implementing OneOfBody. So this way, passing ObjectA OR ObjectB seems impossible.

openapi-generator version

4.3.1

OpenAPI declaration file content or url

https://gist.github.com/swarnim-sib/94a6ad4b31f63d1c6ad45b8de5961333

Generation Details

openapi-generator generate -i ~/Documents/Project/api/client/file.yml -g java -o ~/JavaClient

Steps to reproduce
  • Take any yml file, for example Swagger Petstore example and add configurations mentioned here (related to oneOf)
  • Run openapi-generator script mentioned above to generate Java client
  • Check files, OneOfBody.java, possibly Body.java, and API class mentioned in above endpoint
Related issues/PRs
Suggest a fix
  • If we could tell the API classes to accept OneOfBody parameters instead of Body parameters then ObjectA and ObjectB could be type casted to OneOfBody and the API class would process the passed parameters OR
  • If we could define a method in OneOfBody interface which could be used for typecasting between ObjectA/ObjectB and Body then too the API method could accept the parameter and would process it

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

Reproduce with the stated openapi-generator command and the linked OpenAPI declaration, then inspect the generated OneOfBody.java, Body.java, ObjectA, ObjectB, and the affected API class. Compare the generated parameter type with the oneOf models and review related issue #5565. Done means the generated Java client handles the documented oneOf polymorphism as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.