Skip to content

[BUG] OneOf-Generation does not work with more than two child schemas #21773

@fkellner

Description

@fkellner
Description

When the schema contains a oneOf-field, the vars field of CodegenModel only contains the fields of two of the subschemas. This is not visible with every generator, since some generate models that use inheritance, but for generators that generate a single object with all possible fields, this results in incorrect models being generated.

I noticed this bug in a downstream library, which uses the CodegenModel directly (see quarkiverse/quarkus-openapi-generator#1275 ), but you can also reproduce it using e.g. the java-vertx generator.

Example:

  • Schema Mouse:
    • species
    • mouseId
  • Schema Cat:
    • species
    • catId
  • Schema Dog:
    • species
    • dogId
  • Schema Animal:
    • oneOf
      • Mouse,
      • Cat
      • Dog

Results in:

  • Model Animal:
    • species
    • catId
    • dogId
    • missing: mouseId
openapi-generator version

The downstream project uses 7.14, but I could reproduce it using the latest JAR cli build (openapi-generator-cli-8.0.0-20240727.184357-6.jar).

OpenAPI declaration file content or url

See https://github.com/fkellner/quarkus-openapi-generator/blob/bugreport/one-of-three/client/deployment/src/test/resources/openapi/one-of-three.yaml

Generation Details

For reproducing with the java-vertx client:

java -jar openapi-generator-cli-8.0.0-20240727.184357-6.jar generate -i one-of-three.yaml -g java-vertx -o out/java-vertx

Then look at the generated model RandomAnimalsResponseAnimalsInner.

Steps to reproduce
Related issues/PRs

I did not find any.

Issue in the downstream project:
quarkiverse/quarkus-openapi-generator#1275

Suggest a fix

Problem seems to be somewhere in the DefaultGenerator, in the generateModels step (schema looks okay).
The CodegenModel class I keep mentioning is this one: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenModel.java

I could not find the unit tests for the DefaultGenerator yet, but if I have more time or somebody points me in the right direction, I might be able to provide a unit test demonstrating the problem or even a pull request.

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