OpenAPITools / OpenAPITools/openapi-generator

[BUG] [cpp-oatpp-client] Model generator fails with oneOf

Open
#23,195 0 comments 0 reactions 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

Bug Report Checklist
  • [ x] Have you provided a full/minimal spec to reproduce the issue?
  • [ x] Have you validated the input using an OpenAPI validator?
  • [ x] Have you tested with the latest master to confirm the issue still exists?
  • [ x] Have you searched for related issues/PRs?
Description

Generating a client using the cpp-oatpp-client generator fails with a ClassCastException if a oneOf is present in the schema.

Exception in thread "main" java.lang.RuntimeException: Could not process model 'Filter_name'.Please make sure that your schema is correct!
 	at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:519)
 	at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:444)
 	at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:1292)
 	at org.openapitools.codegen.cmd.Generate.execute(Generate.java:535)
 	at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
 	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.ClassCastException: class io.swagger.v3.oas.models.media.JsonSchema cannot be cast to class io.swagger.v3.oas.models.media.ArraySchema (io.swagger.v3.oas.models.media.JsonSchema and io.swagger.v3.oas.models.media.ArraySchema are in unnamed module of loader 'app')
 	at org.openapitools.codegen.languages.CppOatppClientCodegen.getTypeDeclaration(CppOatppClientCodegen.java:315)
 	at org.openapitools.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:4184)
 	at org.openapitools.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:3971)
	at org.openapitools.codegen.languages.AbstractCppCodegen.fromProperty(AbstractCppCodegen.java:275)
 	at org.openapitools.codegen.DefaultCodegen.addVars(DefaultCodegen.java:6181)
 	at org.openapitools.codegen.DefaultCodegen.addVars(DefaultCodegen.java:6102)
 	at org.openapitools.codegen.DefaultCodegen.updateModelForComposedSchema(DefaultCodegen.java:2863)
 	at org.openapitools.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:3153)
 	at org.openapitools.codegen.languages.CppOatppClientCodegen.fromModel(CppOatppClientCodegen.java:182)
 	at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1754)
 	at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:514)
 	... 5 more
openapi-generator version

7.21.0-SNAPSHOT

OpenAPI declaration file content or url
  "components" : {
    "schemas" : {
      "Filter" : {
        "type" : "object",
        "properties" : {
          "name" : {
            "oneOf" : [ {
              "$ref" : "#/components/schemas/EqualsPropertyFilter"
            }, {
              "$ref" : "#/components/schemas/EndsWithPropertyFilter"
            }, {
              "$ref" : "#/components/schemas/InPropertyFilter"
            }, {
              "type" : "null"
            } ]
          },
          ....
      },
      "EndsWithPropertyFilter" : {
        "type" : "object",
        "properties" : {
          "endsWith" : {
            "type" : "string"
          }
        },
        "required" : [ "endsWith" ]
      },
      "EqualsPropertyFilter" : {
        "type" : "object",
        "properties" : {
          "eq" : {
            "type" : "string"
          }
        },
        "required" : [ "eq" ]
      },
      "InPropertyFilter" : {
        "type" : "object",
        "properties" : {
          "in" : {
            "type" : "array",
            "items" : {
              "type" : "string"
            }
          }
        },
        "required" : [ "in" ]
      }
    }
  }

Generation Details

openapi-generator-cli generate -i openapi-spec.json -o api-cpp -g cpp-oatpp-client

Suggest a fix

Other C++ generators use a std::variant<Type1, Type2, ...> to represent oneOf, anyOf. This code is missing from the cpp-oatpp-client generator.

See for example https://github.com/OpenAPITools/openapi-generator/blob/a2fd144758a68c107c618eb1393b6f3f02e159bd/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppHttplibServerCodegen.java#L1502

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

Reproduce the failure with the supplied OpenAPI declaration and the openapi-generator-cli generate -i openapi-spec.json -o api-cpp -g cpp-oatpp-client command. Start at CppOatppClientCodegen.java:315 and :182, then compare the oneOf/anyOf handling in CppHttplibServerCodegen.java:1502. Done means generation succeeds for the provided schema without the ClassCastException.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.