swagger-api / swagger-api/swagger-codegen

[Java] OneOf polymorphism code wasn't generated correctly

Open
#10,459 5 comments 15 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I used swagger-codegen 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.

Swagger-codegen version

3.0.21

Swagger declaration file content or url

Specific part of the endpoint: https://gist.github.com/swarnim-sib/94a6ad4b31f63d1c6ad45b8de5961333

Command line used for generation

java -jar ~/swagger-codegen-cli-3.0.21.jar generate -i ~/Documents/Project/api/client/file.yml -l java -o ~/JavaClient

Steps to reproduce
  • Take any yml file, for example Swagger Petstore example and add configurations mentioned here (related to oneOf)
  • Run swagger-codegen 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/enhancement
  • 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
  • https://github.com/swagger-api/swagger-codegen/issues/10011#issuecomment-579839110

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 with the generated OneOfBody.java, Body.java, and the API class from the Java client produced by the documented swagger-codegen command. Compare the generated parameter types with the oneOf declaration and related issue #10011; done means ObjectA and ObjectB can be passed through the generated endpoint correctly.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.