OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.62k stars 6.29k forks source link

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

Open mvettosi opened 5 months ago

mvettosi commented 5 months ago
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!

tobhaeu commented 5 months ago

My team and me were also observing progress on the mentioned PR very closely over the past months. We'd really appreciate a fix for this issue.

ash67 commented 2 months ago

I can confirm the same issue. It would be nice to have anyOf and oneOf supported asap.

jponc commented 2 months ago

Yeah same, our team currently use typescript-nestjs generator (not Kotlin), but the result is the same, looks like if we have anyOf it just use the last type.