OpenAPITools / OpenAPITools/openapi-generator

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

Open
#8,382 3 comments 1 reaction 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
  • 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.

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

Start with the provided java -jar openapi-generator-cli.jar command, the linked swagger.json example, and the aspnetcore generator entry point; inspect the generated files under ./output. Reproduce the missing OneOfCustomerModelProjectModel reference and compare the generated operation signatures and models. Done means the generated .NET Core server compiles for the supplied oneOf specification.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.