Skip to content

remove-unused does not remove all unused definitions #2657

@said-saifi

Description

@said-saifi

Problem statement

swagger flatten --with-flatten=remove-unused does not remove models that were only referenced by models that were just removed.

That is, if I have model Book which uses model Author, if both of them are not used, and I run swagger flatten --with-flatten=remove-unused then it will only remove Book model, but will keep Author as it was considered as used, but it's not really used anymore.

Steps to reproduce

  1. Let's say you have this simple schema.json
{
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "schemes": [
    "http",
    "https"
  ],
  "swagger": "2.0",
  "info": {
    "title": "Sample API.",
    "version": "1.0.0"
  },
  "paths": {
    "/hello": {
      "get": {
        "description": "Hello",
        "operationId": "hello",
        "responses": {
          "200": {
            "description": "success"
          }
        }
      }
    }
  },
  "definitions": {
    "Author": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string"
        }
      }
    },
    "Book": {
      "type": "object",
      "properties": {
        "author": {
          "$ref": "#/definitions/Author"
        }
      }
    }
  }
}

as you can see Author and Book are not referenced by any route, however Book references Author.

  1. run swagger flatten --with-flatten=remove-unused -o=schema.json schema.json
    The update schema.json does not have Book anymore as expected, but still has the Author, which is not really being used as well. So Author should have been removed as well but it was not.

Temporary Solution

You can run swagger flatten --with-flatten=remove-unused -o=schema.json schema.json one more time and Author will be removed, but ideally this tool should be able to remove all unused definitions in one single execution.

Environment

swagger version: 0.26.1
go version: 1.17
OS: macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions