cyclosproject / ng-openapi-gen

An OpenAPI 3.0 codegen for Angular
MIT License
397 stars 132 forks source link

Don't collect model dependencies from oneOf #22

Closed remkoboschker closed 5 years ago

remkoboschker commented 5 years ago

We have an openapi file

{
    "x-generator": "NSwag v12.0.12.0 (NJsonSchema v9.13.15.0 (Newtonsoft.Json v11.0.0.0))",
    "openapi": "3.0.0",
    "info": {
        "title": "Our API 1.0",
        "version": "1.0"
    },
    "consumes": ["application/json-patch+json", "application/json", "text/json", "application/*+json"],
    "servers": [
        {
            "url": "http://localhost:60367"
        }
    ],
    "paths": {
        "/api/v1/Modules/{id}": {
            "post": {
                "tags": ["Modules"],
                "operationId": "ApiV1ModulesByIdPost",
                "parameters": [
                    {
                        "name": "id",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "string",
                            "format": "guid"
                        },
                        "x-position": 1
                    }
                ],
                "requestBody": {
                    "x-name": "contractData",
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/ContractDto"
                            }
                        }
                    },
                    "required": true,
                    "x-position": 2
                },
                "responses": {
                    "200": {
                        "description": ""
                    },
                    "400": {
                        "description": "",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "string",
                                    "nullable": true
                                }
                            }
                        }
                    },
                    "404": {
                        "description": "",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "string",
                                    "nullable": true
                                }
                            }
                        }
                    },
                    "500": {
                        "description": "",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "string",
                                    "nullable": true
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "ContractDto": {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "id": {
                        "type": "string",
                        "format": "guid",
                        "nullable": true
                    },
                    "modules": {
                        "type": "array",
                        "nullable": true,
                        "items": {
                            "$ref": "#/components/schemas/Module"
                        }
                    }
                }
            },
            "Module": {
                "type": "object",
                "x-abstract": true,
                "additionalProperties": false,
                "properties": {
                    "inUse": {
                        "type": "boolean"
                    },
                    "naam": {
                        "type": "string",
                        "nullable": true
                    }
                },
                "oneOf": [
                    {
                        "$ref": "#/components/schemas/Submodule1"
                    },
                    {
                        "$ref": "#/components/schemas/Submodule2"
                    }
                ]
            },
            "Submodule1": {
                "allOf": [
                    {
                        "$ref": "#/components/schemas/Module"
                    },
                    {
                        "type": "object",
                        "additionalProperties": false
                    }
                ]
            },
            "Submodule2": {
                "allOf": [
                    {
                        "$ref": "#/components/schemas/Module"
                    },
                    {
                        "type": "object",
                        "additionalProperties": false
                    }
                ]
            }
        }
    }
}

ng-openapi-gen generates the following model for Module:

import { Submodule1 } from './submodule-1';
import { Submodule2 } from './submodule-2';
export interface Module  {
  inUse?: boolean;
  naam?: null | string;
}

The typescript compiler will nog compile because Submodule1 and Submodule2 are imported but not used.

Is this the intended behaviour?

remkoboschker commented 5 years ago

Can you confirm this is a bug? I would be willing to submit a PR.

luisfpg commented 5 years ago

Yes, it is a bug, because we're collecting dependencies from oneOf, but not using it in any other way in the generation. I'm changing the title accordingly.

remkoboschker commented 5 years ago

I updated to release 0.5.0 but the issue persists.