OpenAPITools / OpenAPITools/openapi-generator

[BUG][C#] Should use fully-qualified class name for models

Open
#16,123 10 comments 2 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

Description

I have exactly the same issue as this: https://stackoverflow.com/questions/59692538/ambiguous-reference-error-in-openapi-generated-c-sharp-code

I have a model class called Environment and using the csharp-netcore generator this causes compile errors due to the ambiguous reference.

I could not see this listed as a bug for OpenAPI Generator.

There is an open issue for Swagger codegen https://github.com/swagger-api/swagger-codegen/issues/2630 as mentioned.

Error is:

Api\EnvironmentApi.cs(1053,173): error CS0104: 'Environment' is an ambiguous reference between 'Org.OpenAPITools.Model.Environment' and 'System.Environment'

openapi-generator version

6.6.0

OpenAPI declaration file content or url
{
  "openapi": "3.0.1",
  "info": {
    "title": "CloudData",
    "version": "1.0"
  },
  "paths": {
    "/api/v1/environments": {
      "get": {
        "tags": [
          "Environment"
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Environment"
                  }
                }
              },
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Environment"
                  }
                }
              },
              "text/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Environment"
                  }
                }
              }
            }
          }
        }
      },
      "post": {
        "tags": [
          "Environment"
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Environment"
              }
            },
            "text/json": {
              "schema": {
                "$ref": "#/components/schemas/Environment"
              }
            },
            "application/*+json": {
              "schema": {
                "$ref": "#/components/schemas/Environment"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "text/plain": {
                "schema": {
                  "$ref": "#/components/schemas/Environment"
                }
              },
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Environment"
                }
              },
              "text/json": {
                "schema": {
                  "$ref": "#/components/schemas/Environment"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/environments/{id}": {
      "get": {
        "tags": [
          "Environment"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "text/plain": {
                "schema": {
                  "$ref": "#/components/schemas/Environment"
                }
              },
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Environment"
                }
              },
              "text/json": {
                "schema": {
                  "$ref": "#/components/schemas/Environment"
                }
              }
            }
          }
        }
      },
      "put": {
        "tags": [
          "Environment"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Environment"
              }
            },
            "text/json": {
              "schema": {
                "$ref": "#/components/schemas/Environment"
              }
            },
            "application/*+json": {
              "schema": {
                "$ref": "#/components/schemas/Environment"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Success"
          }
        }
      },
      "delete": {
        "tags": [
          "Environment"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Environment": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string",
            "nullable": true
          }
        },
        "additionalProperties": false
      }
    },
    "securitySchemes": {
      "Bearer": {
        "type": "apiKey",
        "description": "JWT Token issued for your account using Bearer scheme.",
        "name": "Authorization",
        "in": "header"
      }
    }
  },
  "security": [
    {
      "Bearer": [ ]
    }
  ]
}
Generation Details

npm i @openapitools/openapi-generator-cli
npx openapi-generator-cli generate -i swagger.json -g csharp-netcore -o output

Steps to reproduce

cd output
dotnet build

Related issues/PRs

https://stackoverflow.com/questions/59692538/ambiguous-reference-error-in-openapi-generated-c-sharp-code
https://github.com/swagger-api/swagger-codegen/issues/2630

Suggest a fix

C# generator should be changed to use fully qualified class names.

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 by generating the supplied OpenAPI declaration with the documented csharp-netcore command, then run dotnet build in the output directory. Inspect the generated Api/EnvironmentApi.cs references that produce the ambiguity; done means the generated project builds successfully when a model is named Environment.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.