Skip to content

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

@mvettosi

Description

@mvettosi
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: #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!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions