diff --git a/index.js b/index.js index dd1f690..f1e7c3b 100644 --- a/index.js +++ b/index.js @@ -139,10 +139,41 @@ function filter(obj,options) { if (options.servers && !filtered.servers && Array.isArray(src.servers)) { filtered.servers = src.servers; } + + const activeSecuritySchemes= + (src.paths?Object.keys(src.paths):[]) + .flatMap(pathUrl =>src.paths[pathUrl]) + .flatMap(pathElement => Object.keys(pathElement) + .flatMap(method =>pathElement[method])) + .filter(path => Object.keys(path).filter(value => options.flags.includes(value))) + .flatMap(path =>{ + if (!filtered.security && Array.isArray(path.security)) { + return path.security.flatMap(securityItem => Object.keys(securityItem)); + } else { + return []; + } + }) + .filter(filterUnique) + + // OAS2 + if (src.securityDefinitions) { + filtered.securityDefinitions = Object.fromEntries(Object.entries(src.securityDefinitions).filter(([key]) => activeSecuritySchemes.includes(key))); + } + // OAS3 + if (src.components && src.components.securitySchemes) { + if (!filtered.components){ + filtered.components ={}; + } + filtered.components.securitySchemes = Object.fromEntries(Object.entries(src.components.securitySchemes).filter(([key]) => activeSecuritySchemes.includes(key))); + } } return (options.inverse ? filtered : src); } +function filterUnique(value, index, array) { + return array.indexOf(value) === index; +} + module.exports = { filter : filter }; diff --git a/test/asyncapi/output.yaml b/test/asyncapi/output.yaml index 278b4f1..7f61c55 100644 --- a/test/asyncapi/output.yaml +++ b/test/asyncapi/output.yaml @@ -58,3 +58,4 @@ components: type: string format: date-time description: Date and time when the message was sent. + securitySchemes: {} diff --git a/test/checkTags1/output.yaml b/test/checkTags1/output.yaml index 6eaae1d..976494e 100644 --- a/test/checkTags1/output.yaml +++ b/test/checkTags1/output.yaml @@ -9,7 +9,10 @@ x-origin: url: https://github.com/mermade/swagger2openapi version: 2.6.3 info: - description: "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters." + description: "This is a sample server Petstore server. You can find out more + about Swagger at [http://swagger.io](http://swagger.io) or on + [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you + can use the api key `special-key` to test the authorization filters." version: 1.0.0 title: Swagger Petstore termsOfService: http://swagger.io/terms/ @@ -114,7 +117,8 @@ paths: tags: - pet summary: Finds Pets by tags - description: Muliple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. + description: Muliple tags can be provided with comma separated strings. Use + tag1, tag2, tag3 for testing. operationId: findPetsByTags parameters: - name: tags @@ -321,7 +325,8 @@ paths: tags: - store summary: Find purchase order by ID - description: For valid response try integer IDs with value >= 1 and <= 10. Other values will generated exceptions + description: For valid response try integer IDs with value >= 1 and <= 10. Other + values will generated exceptions operationId: getOrderById parameters: - name: orderId @@ -351,7 +356,8 @@ paths: tags: - store summary: Delete purchase order by ID - description: For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors + description: For valid response try integer IDs with positive integer value. + Negative or non-integer values will generate API errors operationId: deleteOrder parameters: - name: orderId diff --git a/test/checkTags2/output.yaml b/test/checkTags2/output.yaml index 65c19cb..b38750c 100644 --- a/test/checkTags2/output.yaml +++ b/test/checkTags2/output.yaml @@ -205,3 +205,16 @@ components: $ref: "#/components/schemas/User" description: List of user object required: true + securitySchemes: + petstore_auth: + type: oauth2 + flows: + implicit: + authorizationUrl: http://petstore.swagger.io/oauth/dialog + scopes: + write:pets: modify pets in your account + read:pets: read your pets + api_key: + type: apiKey + name: api_key + in: header diff --git a/test/circular/output.yaml b/test/circular/output.yaml index 08e90bc..5e14124 100644 --- a/test/circular/output.yaml +++ b/test/circular/output.yaml @@ -7,14 +7,14 @@ paths: "/{comicId}/info.0.json": x-internal: true get: - description: > + description: | Fetch comics and metadata by comic id. parameters: - in: path name: comicId required: true type: number - - $ref: '#/definitions/complex' + - $ref: "#/definitions/complex" - $ref: "#/definitions/hovertext" responses: "200": @@ -27,6 +27,13 @@ definitions: properties: description: type: string + complex: + properties: + a69: + type: object + oneOf: + - $ref: "#/definitions/b1" + - $ref: "#/definitions/b2" comic: properties: alt: @@ -52,15 +59,8 @@ definitions: year: type: string previous: - $ref: '#/definitions/comic' + $ref: "#/definitions/comic" type: object - complex: - properties: - a69: - type: object - oneOf: - - $ref: '#/definitions/b1' - - $ref: '#/definitions/b2' b1: properties: ab: @@ -70,8 +70,9 @@ definitions: ac: type: object oneOf: - - $ref: '#/definitions/c1' + - $ref: "#/definitions/c1" c1: properties: ab: type: string +securityDefinitions: {} diff --git a/test/codep/output.yaml b/test/codep/output.yaml index 3017f9d..60ded0f 100644 --- a/test/codep/output.yaml +++ b/test/codep/output.yaml @@ -7,14 +7,14 @@ paths: "/{comicId}/info.0.json": x-internal: true get: - description: > + description: | Fetch comics and metadata by comic id. parameters: - in: path name: comicId required: true type: number - - $ref: '#/definitions/complex' + - $ref: "#/definitions/complex" - $ref: "#/definitions/hovertext" responses: "200": @@ -27,6 +27,13 @@ definitions: properties: description: type: string + complex: + properties: + a69: + type: object + oneOf: + - $ref: "#/definitions/b1" + - $ref: "#/definitions/b2" comic: properties: alt: @@ -52,20 +59,8 @@ definitions: year: type: string previous: - $ref: '#/definitions/other' + $ref: "#/definitions/other" type: object - other: - type: object - properties: - comic: - $ref: '#/definitions/comic' - complex: - properties: - a69: - type: object - oneOf: - - $ref: '#/definitions/b1' - - $ref: '#/definitions/b2' b1: properties: ab: @@ -75,8 +70,14 @@ definitions: ac: type: object oneOf: - - $ref: '#/definitions/c1' + - $ref: "#/definitions/c1" + other: + type: object + properties: + comic: + $ref: "#/definitions/comic" c1: properties: ab: type: string +securityDefinitions: {} diff --git a/test/configFile/output.yaml b/test/configFile/output.yaml index df8d6ae..948fb1f 100644 --- a/test/configFile/output.yaml +++ b/test/configFile/output.yaml @@ -25,7 +25,7 @@ paths: /info.0.json: x-visibility: public get: - description: > + description: | Fetch current comic and metadata. parameters: - $ref: "#/definitions/comic" diff --git a/test/issue105a/output.yaml b/test/issue105a/output.yaml index 5af3474..1437a1e 100644 --- a/test/issue105a/output.yaml +++ b/test/issue105a/output.yaml @@ -7,7 +7,7 @@ components: ok: description: OK content: - 'application/json': + application/json: schema: type: object properties: diff --git a/test/issue26a/output.yaml b/test/issue26a/output.yaml index 37d3be1..8e45e90 100644 --- a/test/issue26a/output.yaml +++ b/test/issue26a/output.yaml @@ -3,6 +3,6 @@ info: title: API version: 1.0.0 tags: - - "x-public": true + - x-public: true name: Tag 2 paths: {} diff --git a/test/issue26b/output.yaml b/test/issue26b/output.yaml index 9a2434a..0c69c03 100644 --- a/test/issue26b/output.yaml +++ b/test/issue26b/output.yaml @@ -3,6 +3,6 @@ info: title: API version: 1.0.0 tags: - - "x-public": true - name: Tag 1 + - name: Tag 1 + x-public: true paths: {} diff --git a/test/issue29_nestedReferences/output.yaml b/test/issue29_nestedReferences/output.yaml index d82186f..ac19a8e 100644 --- a/test/issue29_nestedReferences/output.yaml +++ b/test/issue29_nestedReferences/output.yaml @@ -7,14 +7,14 @@ paths: "/{comicId}/info.0.json": x-internal: true get: - description: > + description: | Fetch comics and metadata by comic id. parameters: - in: path name: comicId required: true type: number - - $ref: '#/definitions/complex' + - $ref: "#/definitions/complex" - $ref: "#/definitions/hovertext" responses: "200": @@ -27,6 +27,13 @@ definitions: properties: description: type: string + complex: + properties: + a69: + type: object + oneOf: + - $ref: "#/definitions/b1" + - $ref: "#/definitions/b2" comic: properties: alt: @@ -52,13 +59,6 @@ definitions: year: type: string type: object - complex: - properties: - a69: - type: object - oneOf: - - $ref: '#/definitions/b1' - - $ref: '#/definitions/b2' b1: properties: ab: @@ -68,8 +68,9 @@ definitions: ac: type: object oneOf: - - $ref: '#/definitions/c1' + - $ref: "#/definitions/c1" c1: properties: ab: - type: string \ No newline at end of file + type: string +securityDefinitions: {} diff --git a/test/issue85/output.yaml b/test/issue85/output.yaml index 80b447a..e270216 100644 --- a/test/issue85/output.yaml +++ b/test/issue85/output.yaml @@ -6,8 +6,7 @@ paths: /test: post: parameters: - - $ref: '#/components/parameters/ExternalHeader' - + - $ref: "#/components/parameters/ExternalHeader" components: parameters: ExternalHeader: diff --git a/test/valid/output.yaml b/test/valid/output.yaml index 2fa0d59..f0af1fa 100644 --- a/test/valid/output.yaml +++ b/test/valid/output.yaml @@ -7,7 +7,7 @@ paths: "/{comicId}/info.0.json": x-internal: true get: - description: > + description: | Fetch comics and metadata by comic id. parameters: - in: path @@ -51,3 +51,4 @@ definitions: year: type: string type: object +securityDefinitions: {}