OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Kotlin] Parsing the keyword "anyOf" results in hardcoding only the last model listed

Open
#17,602 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

When describing an array of values with the keyword anyOf, all the models seem correctly generated but only the last one in the list is actually used.

openapi-generator version
openapi-generator-cli 7.2.0
  commit : fe638d0
  built  : -999999999-01-01T00:00:00+18:00
  source : https://github.com/openapitools/openapi-generator
  docs   : https://openapi-generator.tech/

Also reproducible with:

openapi-generator-cli 7.3.0-SNAPSHOT
  commit : 75ef9e0
  built  : -999999999-01-01T00:00:00+18:00
  source : https://github.com/openapitools/openapi-generator
  docs   : https://openapi-generator.tech/
OpenAPI declaration file content or url

Here is a minimal example that reproduces the issue:
https://gist.github.com/mvettosi/d646be95fd43e732cd5dcce680b60597

Generation Details

The file MyapiPut200ResponseIncludedInner.kt correctly imports the models listed by anyOf (Cat.kt and Dog.kt), also their individual type enums are merged in here as one would reasonably expect.
However, the attributes field is not a merge of Cat and Dog, nor a superclass of either, but it's hardcoded to be Dog, making it impossible to access the fields that are specific to Cat.

Steps to reproduce

Simply run

openapi-generator generate -g kotlin -i minimal.yml
Related issues/PRs

I saw this PR which was supposed to address the anyOf implementation, but it was recently closed without an explanation: https://github.com/OpenAPITools/openapi-generator/pull/12924
Moreover, I downloaded and compiled the original work from https://github.com/vfouqueron/openapi-generator/tree/release/vf-7.0.x and the same error seems to occur.

I tried to apply the workaround suggested here: https://stackoverflow.com/a/67197819/21581836
However, the result didn't compile despite various attempts to fix from my side by changing the template files.

Suggest a fix

I'm not sure how the keyword is supposed to be implemented by kotlin currently, I can only assume an interface should be generated that both Cat and Dog should implement, and that some code in the generated client should figure out which actual model to use based on the discriminator.

Alternatively, I would personally be very happy with a simpler approach like a single model that merges all field from all possible models listed, so that they can all be accessed.

Any workaround, that includes either spec or template manipulation would be very welcome, too! I really need to be unblocked by this.

Thanks!

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 minimal.yml reproduction and run openapi-generator generate -g kotlin -i minimal.yml; inspect the generated MyapiPut200ResponseIncludedInner.kt alongside Cat.kt and Dog.kt. Determine how the Kotlin generator represents the anyOf attributes field, then verify that generation preserves access to both model alternatives rather than hardcoding Dog.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.