OpenAPITools / OpenAPITools/openapi-generator

[BUG] Generation of models with invalid imports (aspnetcore)

Open
#9,096 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: C-Sharp
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

After generating models for .Netcore 3.1 the models contains unused usings (imports). However, running dotnet build fails because one of the unused usings reference to a namespace that doesn't exist anymore. You can see it in the following image:

image

The referenced namespace exists when I generate all files. But I only need the models. The import seems to be a relict of generated files (necessary for generating the models) that are removed afterwards while the import still exists

openapi-generator version
❯ openapi-generator version
5.1.0

OpenAPI declaration file content or url

In the following you can find the openapi.json:

openapi.json

{
    "openapi": "3.0.2",
    "info": {
        "title": "TestProject",
        "version": "0.0.1"
    },
    "paths": {
        "/api/Product": {
            "get": {
                "tags": [
                    "Product"
                ],
                "summary": "Product",
                "operationId": "product_api_Product_get",
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "title": "Response Product Api Product Get",
                                    "type": "array",
                                    "items": {
                                        "$ref": "#/components/schemas/Product"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "Product": {
                "title": "Product",
                "required": [
                    "id"
                ],
                "type": "object",
                "properties": {
                    "id": {
                        "title": "Id",
                        "type": "string",
                        "format": "uuid"
                    },
                    "machine": {
                        "title": "Machine",
                        "type": "string",
                        "nullable": true
                    }
                }
            }
        }
    }
}

Generation Details

config.json:

{
    "aspnetCoreVersion": 3.1,
    "swashbuckleVersion": "5.0.0",
    "packageName": "TestProject",
    "packageTitle": "TestProject",
    "sourceFolder": ""
}
Steps to reproduce
  • use the config.json and the openapi.json
  • run openapi-generator generate -i openapi.json -g aspnetcore --config config.json --global-property models,modelDocs=false
Expected behaviour

The models were generated at least without using TestProject.Converters;. Much better would be a solution where unused usings will be removed.

Related issues/PRs
Suggest a fix

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 reproducing the issue with config.json, openapi.json, and the documented openapi-generator generate command using the aspnetcore generator. Inspect the generated model imports and verify that model-only generation no longer includes the invalid TestProject.Converters namespace; confirm the generated project passes dotnet build.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.