Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
1 change: 1 addition & 0 deletions test/asyncapi/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ components:
type: string
format: date-time
description: Date and time when the message was sent.
securitySchemes: {}
14 changes: 10 additions & 4 deletions test/checkTags1/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/checkTags2/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 12 additions & 11 deletions test/circular/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -27,6 +27,13 @@ definitions:
properties:
description:
type: string
complex:
properties:
a69:
type: object
oneOf:
- $ref: "#/definitions/b1"
- $ref: "#/definitions/b2"
comic:
properties:
alt:
Expand All @@ -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:
Expand All @@ -70,8 +70,9 @@ definitions:
ac:
type: object
oneOf:
- $ref: '#/definitions/c1'
- $ref: "#/definitions/c1"
c1:
properties:
ab:
type: string
securityDefinitions: {}
33 changes: 17 additions & 16 deletions test/codep/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -27,6 +27,13 @@ definitions:
properties:
description:
type: string
complex:
properties:
a69:
type: object
oneOf:
- $ref: "#/definitions/b1"
- $ref: "#/definitions/b2"
comic:
properties:
alt:
Expand All @@ -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:
Expand All @@ -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: {}
2 changes: 1 addition & 1 deletion test/configFile/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ paths:
/info.0.json:
x-visibility: public
get:
description: >
description: |
Fetch current comic and metadata.
parameters:
- $ref: "#/definitions/comic"
Expand Down
2 changes: 1 addition & 1 deletion test/issue105a/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ components:
ok:
description: OK
content:
'application/json':
application/json:
schema:
type: object
properties:
Expand Down
2 changes: 1 addition & 1 deletion test/issue26a/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ info:
title: API
version: 1.0.0
tags:
- "x-public": true
- x-public: true
name: Tag 2
paths: {}
4 changes: 2 additions & 2 deletions test/issue26b/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
23 changes: 12 additions & 11 deletions test/issue29_nestedReferences/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -27,6 +27,13 @@ definitions:
properties:
description:
type: string
complex:
properties:
a69:
type: object
oneOf:
- $ref: "#/definitions/b1"
- $ref: "#/definitions/b2"
comic:
properties:
alt:
Expand All @@ -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:
Expand All @@ -68,8 +68,9 @@ definitions:
ac:
type: object
oneOf:
- $ref: '#/definitions/c1'
- $ref: "#/definitions/c1"
c1:
properties:
ab:
type: string
type: string
securityDefinitions: {}
3 changes: 1 addition & 2 deletions test/issue85/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ paths:
/test:
post:
parameters:
- $ref: '#/components/parameters/ExternalHeader'

- $ref: "#/components/parameters/ExternalHeader"
components:
parameters:
ExternalHeader:
Expand Down
3 changes: 2 additions & 1 deletion test/valid/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,3 +51,4 @@ definitions:
year:
type: string
type: object
securityDefinitions: {}