Skip to content

[BUG][ASPNETCORE] Using anyOf or oneOf generates uncompilable server code #8382

@leonluc-dev

Description

@leonluc-dev

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Whenever the OpenAPI spec contains one of the anyOf or oneOf sections it will generate uncompilable .NET Core server code.
It will generate attributes and method signatures which contain a reference to the class AnyOf<modelnames>, or OneOf<modelnames> (where the <modelnames> part is the names of the models in that section).

For example, the following OpenApi spec snippet (taken from the example spec below):

"oneOf": [
  {
    "$ref": "#/components/schemas/CustomerModel"
  },
  {
    "$ref": "#/components/schemas/ProjectModel"
  }
]

would generate the following method (operation) signature:

[ProducesResponseType(statusCode: 200, type: typeof(OneOfCustomerModelProjectModel))]

It seems the referenced type in this signature was intended to be a base class to the generated CustomerModel and ProjectModel classes, but the base class is never generated (and the generated models don't inherit from it) so this method signature throws a compiler error.

openapi-generator version

Tested with versions:

5.0.0
5.0.1-SNAPSHOT (20210108.082455-27)
5.1.0-SNAPSHOT (20201221.091745-1)
6.0.0-SNAPSHOT (20201221.092151-1)

OpenAPI declaration file content or url
"responses": {
    "200": {
      "description": "Success",
      "content": {
        "application/json": {
          "schema": {
            "oneOf": [
                {
                  "$ref": "#/components/schemas/CustomerModel"
                },
                {
                  "$ref": "#/components/schemas/ProjectModel"
                }
            ]
          }
        }
      }
}

Full example spec

Generation Details

Code generated using:

java -jar openapi-generator-cli.jar generate -i ./swagger.json -g aspnetcore -o ./output --additional-properties="generateBody=false,classModifier=abstract,operationResultTask=true,aspnetCoreVersion=3.1,buildTarget=library,isLibrary=true,packageName=MyProject.GeneratedStubs,useSwashbuckle=false"

Steps to reproduce

Generate the example spec using the above generation cli
The resulting code should be uncompilable due to the missing OneOfProjectModelCustomerModel class

Related issues/PRs

Issues #5938 and #7414 seem to be similar issues occurring on other languages

Suggest a fix

I haven't been able to solve this using templates (this issue seemingly occurs before the templates are invoked)
I'm not familiar enough with the source code of the generator to be able to pinpoint the exact moment the issue occurs.

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