Skip to content

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

Open
@riede

Description

@riede

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

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