Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 of 6 tasks
leonluc-dev opened this issue Jan 8, 2021 · 3 comments
Open
5 of 6 tasks

Comments

@leonluc-dev
Copy link
Contributor

leonluc-dev commented Jan 8, 2021

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.

@leonluc-dev
Copy link
Contributor Author

Looking further into this it seems this does indeed occur before the templates are invoked.
Debugging the operations using --global-property debugOperations=true yields the following metadata for the mustache templates:
"openApiType" : "OneOfProjectModelCustomerModel"

@mutyasaisrikar
Copy link

@leonluc-dev
I am seeing a similar issue in Java for one of the fields. We have a field called ipAddress and the description in swagger is as below.

ipAddress: description: The location the content is trying to be played from type: string oneOf: [ {format: ipv4}, {format: ipv6} ]

Instead of creating it as a String, the openapi-generator-maven-plugin is creating it as OneOfAnyTypeAnyType

@wing328
Copy link
Member

wing328 commented Sep 6, 2022

csharp-netcore generator supports oneOf already so please give it a try to see if the implementation meets your requirement.

Then you can plot the implementation to aspnetcore-generator or sponsor someone from the community to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants