Mermade / openapi-filter

Filter internal paths, operations, parameters, schemas etc from OpenAPI/Swagger/AsyncAPI definitions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Filter ignores some $refs

eliasdraexler opened this issue · comments

Hello!
I wanted to use the openapi-filter to remove some internal headers from our public openapi specifications.
Unfortunately, it doesn't seem to fully work.

An example:

openapi: 3.0.2
info:
  title: Swagger Petstore - OpenAPI 3.0
  description: omit
  termsOfService: 'http://swagger.io/terms/'
  contact:
    email: apiteam@swagger.io
  license:
    name: Apache 2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
  version: 1.0.5
servers:
  - url: /api/v3
paths:
  /pet:
    post:
      tags:
        - pet
      summary: Add a new pet to the store
      description: Add a new pet to the store
      operationId: addPet
      parameters:
        - $ref: '#/components/parameters/InternalHeader'
        - $ref: '#/components/parameters/OtherInternalHeader'
        - in: header
          name: X-ThirdInternalHeader
          schema:
            type: string
          required: true
          x-internal: true
      requestBody:
        description: Create a new pet in the store
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
        required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
        '405':
          description: Invalid input

components:
  parameters:
    InternalHeader:
      in: header
      name: X-InternalHeader
      schema:
        type: string
      required: true
      x-internal: true
    OtherInternalHeader:
      in: header
      name: X-OtherInternalHeader
      schema:
        type: string
      required: true
      x-internal: true
  schemas:
    Pet:
      required:
        - name
        - photoUrls
      type: object
      properties:
        id:
          type: integer
          format: int64
          example: 10
      xml:
        name: pet

You can see there are three headers (InternalHeader, OtherInternalHeader & ThirdInternalHeader).
After running the openapi-filter the result looks like this:

... omitted because irrelevant
paths:
  /pet:
    post:
      tags:
        - pet
      summary: Add a new pet to the store
      description: Add a new pet to the store
      operationId: addPet
      parameters:
        - $ref: "#/components/parameters/OtherInternalHeader"

...
components:
  parameters: {}
... omitted because irrelevant

In the end all the parameter definitions are removed but the reference to ONE of the two headers is still there.

I can confirm some issues with $refs.

I have 3 routes. One with a x-internal extension.

After calling it, I have 2 routes - however the Output Object of the removed rout is still present, leaving an empty object in the specification.

openapi-filter --info -f x-internal -- mathservice.json mathservice-filtered.json

I would expect that "MulOutput" is removed.

{
 "openapi": "3.0.3",
 "info": {
  "title": "Math Service",
  "description": "This app showcases a trivial REST API.",
  "version": "v1.2.3"
 },
 "paths": {
  "/api/add/{a}/{b}": {
   "get": {
    "summary": "Add",
    "description": "Add two integers.",
    "operationId": "./main.createAddMethod",
    "parameters": [
     {
      "name": "a",
      "in": "path",
      "required": true,
      "schema": {
       "type": "integer"
      }
     },
     {
      "name": "b",
      "in": "path",
      "required": true,
      "schema": {
       "type": "integer"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "headers": {
       "X-Now": {
        "style": "simple",
        "schema": {
         "type": "string",
         "format": "date-time"
        }
       }
      },
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/AddOutput"
        }
       }
      }
     },
     "400": {
      "description": "Bad Request",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/RestErrResponse"
        }
       }
      }
     }
    }
   }
  },
  "/api/mul/{a}/{b}": {
   "get": {
    "summary": "Mul",
    "description": "Mul two integers.",
    "operationId": "./main.createMulMethod",
    "parameters": [
     {
      "name": "a",
      "in": "path",
      "required": true,
      "schema": {
       "type": "integer"
      }
     },
     {
      "name": "b",
      "in": "path",
      "required": true,
      "schema": {
       "type": "integer"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "headers": {
       "X-Now": {
        "style": "simple",
        "schema": {
         "type": "string",
         "format": "date-time"
        }
       }
      },
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/MulOutput"
        }
       }
      }
     },
     "400": {
      "description": "Bad Request",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/RestErrResponse"
        }
       }
      }
     }
    },
    "x-internal": true
   }
  },
  "/api/sub/{a}/{b}": {
   "get": {
    "summary": "Sub",
    "description": "Sub two integers.",
    "operationId": "./main.createSubMethod",
    "parameters": [
     {
      "name": "a",
      "in": "path",
      "required": true,
      "schema": {
       "type": "integer"
      }
     },
     {
      "name": "b",
      "in": "path",
      "required": true,
      "schema": {
       "type": "integer"
      }
     }
    ],
    "responses": {
     "200": {
      "description": "OK",
      "headers": {
       "X-Now": {
        "style": "simple",
        "schema": {
         "type": "string",
         "format": "date-time"
        }
       }
      },
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/SubOutput"
        }
       }
      }
     },
     "400": {
      "description": "Bad Request",
      "content": {
       "application/json": {
        "schema": {
         "$ref": "#/components/schemas/RestErrResponse"
        }
       }
      }
     }
    }
   }
  }
 },
 "components": {
  "schemas": {
   "AddOutput": {
    "type": "object",
    "properties": {
     "result": {
      "type": "integer"
     }
    }
   },
   "MulOutput": {
    "type": "object",
    "properties": {
     "result": {
      "type": "integer"
     }
    }
   },
   "RestErrResponse": {
    "type": "object",
    "properties": {
     "code": {
      "type": "integer",
      "description": "Application-specific error code."
     },
     "context": {
      "type": "object",
      "additionalProperties": {},
      "description": "Application context."
     },
     "error": {
      "type": "string",
      "description": "Error message."
     },
     "status": {
      "type": "string",
      "description": "Status text."
     }
    }
   },
   "SubOutput": {
    "type": "object",
    "properties": {
     "result": {
      "type": "integer"
     }
    }
   }
  }
 }
}

@MikeRalphson Thanks for the merge!
When will this be released to npm?
I would need the changes for a project.

Will try and publish today.

Is there any ETA you can give me so I can plan when to upgrade?

Aargh! Completely forgot, sorry. New job, new computer, not logged into npm! Will sort at lunchtime today.

No problem! Thanks for you effort!

Released as part of v3.2.0 - thanks for your patience!