Skip to content

Commit

Permalink
feat: disabled but duplicating params (not supported by OpenAPI)
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed Sep 18, 2022
1 parent 9face89 commit 4310197
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 24 deletions.
22 changes: 6 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,27 +407,17 @@ function scrapeURL (url) {
}

/**
* Calculate query parameters as postman collection
* Calculate query parameters in postman collection
* @param {*} searchParams The searchParam instance from an URL object
* @param {*} queryCollection The postman collection query section
* @returns A query params array as created by postman collections Array(Obj)
*
* NOTE: This method was created because we think that some versions of postman don´t add the `query`
* parameter in the url, but after some reasearch the reason why the `query` parameter can not be
* present is just because no query parameters are used so we just format the postman `query` array here.
*/
function compoundQueryParams (searchParams, queryCollection = []) {
// Prepare desc in query collection for easy search
const descMap = queryCollection.reduce((agr, { key, description }) => {
agr[key] = description
return agr
}, {})
// Create the query array of objects
const query = []
searchParams.forEach((value, key) => {
query.push({
key,
value,
...(descMap[key] != null ? { description: descMap[key] } : {})
})
})
return query
return queryCollection
}

/* Parse domains from operations or options */
Expand Down
21 changes: 19 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ const EXPECTED_COLLECTION_WRAPPER = readFileSync('./test/resources/output/Collec
const EXPECTED_COLLECTION_JSON_COMMENTS = readFileSync('./test/resources/output/JsonComments.yml', 'utf8')
const EXPECTED_DISABLED_PARAMS_DEFAULT = readFileSync('./test/resources/output/DisabledParamsDefault.yml', 'utf8')
const EXPECTED_DISABLED_PARAMS_ALL = readFileSync('./test/resources/output/DisabledParamsAll.yml', 'utf8')
const EXPECTED_DISABLED_PARAMS_QUERY = readFileSync('./test/resources/output/DisabledParamsQuery.yml', 'utf8')
const EXPECTED_DISABLED_PARAMS_HEADER = readFileSync('./test/resources/output/DisabledParamsHeader.yml', 'utf8')

const AUTH_DEFINITIONS = {
myCustomAuth: {
Expand Down Expand Up @@ -516,8 +518,23 @@ describe('Library specs', function () {
equal(result, EXPECTED_DISABLED_PARAMS_ALL)
})

// should include query but not header
// should include headers but not query
it('should include `disable` query but not header', async function () {
const result = await postmanToOpenApi(COLLECTION_DISABLED, OUTPUT_PATH, {
disabledParams: {
includeQuery: true
}
})
equal(result, EXPECTED_DISABLED_PARAMS_QUERY)
})

it('should include `disable` headers but not query', async function () {
const result = await postmanToOpenApi(COLLECTION_DISABLED, OUTPUT_PATH, {
disabledParams: {
includeHeader: true
}
})
equal(result, EXPECTED_DISABLED_PARAMS_HEADER)
})
})
})

Expand Down
2 changes: 1 addition & 1 deletion test/resources/input/v2/DisabledParams.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{
"key": "X-My-Header",
"value": "1234567890",
"description": "Custom header [required]",
"description": "Custom header disabled [required]",
"type": "text",
"disabled": true
},
Expand Down
2 changes: 1 addition & 1 deletion test/resources/input/v21/DisabledParams.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{
"key": "X-My-Header",
"value": "1234567890",
"description": "Custom header [required]",
"description": "Custom header disabled [required]",
"type": "text",
"disabled": true
},
Expand Down
16 changes: 14 additions & 2 deletions test/resources/output/DisabledParamsAll.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ paths:
schema:
type: integer
required: true
description: Custom header
description: Custom header disabled
example: '1234567890'
- name: X-Other
in: header
Expand All @@ -50,18 +50,30 @@ paths:
type: string
description: Disabled parameter
example: QWERTY
- name: age
in: query
schema:
type: integer
description: Disabled param
example: '45'
- name: name
in: query
schema:
type: string
description: Disabled param duplicated
description: Filter by name
example: Jhon
- name: review
in: query
schema:
type: boolean
description: Indicate if should be reviewed or not
example: 'true'
- name: name
in: query
schema:
type: string
description: Disabled param duplicated
example: Mark
- name: section
in: path
schema:
Expand Down
2 changes: 1 addition & 1 deletion test/resources/output/DisabledParamsDefault.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ paths:
in: query
schema:
type: string
description: Disabled param duplicated
description: Filter by name
example: Jhon
- name: review
in: query
Expand Down
76 changes: 76 additions & 0 deletions test/resources/output/DisabledParamsHeader.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
openapi: 3.0.0
info:
title: DisabledParams
description: Test API for disabled parameters feature
version: 1.0.0
servers:
- url: https://api.io
paths:
/{section}/users:
get:
tags:
- default
summary: Get list of users
description: Obtain a list of users that fullfill the conditions of the filters
parameters:
- name: X-My-Header
in: header
schema:
type: string
required: true
description: Custom header
example: hudjilksns78jsijns090
- name: X-My-Header
in: header
schema:
type: integer
required: true
description: Custom header disabled
example: '1234567890'
- name: X-Other
in: header
schema:
type: string
required: true
description: Another header
example: other
- name: No-description
in: header
schema:
type: string
example: header without description
- name: No-value
in: header
schema:
type: string
description: header without value
- name: X-Disabled-Header
in: header
schema:
type: string
description: Disabled parameter
example: QWERTY
- name: name
in: query
schema:
type: string
description: Filter by name
example: Jhon
- name: review
in: query
schema:
type: boolean
description: Indicate if should be reviewed or not
example: 'true'
- name: section
in: path
schema:
type: string
required: true
description: A path parameter
example: spain
responses:
'200':
description: Successful response
content:
application/json: {}
75 changes: 75 additions & 0 deletions test/resources/output/DisabledParamsQuery.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
openapi: 3.0.0
info:
title: DisabledParams
description: Test API for disabled parameters feature
version: 1.0.0
servers:
- url: https://api.io
paths:
/{section}/users:
get:
tags:
- default
summary: Get list of users
description: Obtain a list of users that fullfill the conditions of the filters
parameters:
- name: X-My-Header
in: header
schema:
type: string
required: true
description: Custom header
example: hudjilksns78jsijns090
- name: X-Other
in: header
schema:
type: string
required: true
description: Another header
example: other
- name: No-description
in: header
schema:
type: string
example: header without description
- name: No-value
in: header
schema:
type: string
description: header without value
- name: age
in: query
schema:
type: integer
description: Disabled param
example: '45'
- name: name
in: query
schema:
type: string
description: Filter by name
example: Jhon
- name: review
in: query
schema:
type: boolean
description: Indicate if should be reviewed or not
example: 'true'
- name: name
in: query
schema:
type: string
description: Disabled param duplicated
example: Mark
- name: section
in: path
schema:
type: string
required: true
description: A path parameter
example: spain
responses:
'200':
description: Successful response
content:
application/json: {}
2 changes: 1 addition & 1 deletion test/resources/output/GetMethods.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ paths:
schema:
type: string
description: Should be parsed as string
example: 2022-06-23T10:00:00.000 01:00
example: '2022-06-23T10:00:00.000+01:00'
responses:
'200':
description: Successful response
Expand Down

0 comments on commit 4310197

Please sign in to comment.