Skip to content
This repository has been archived by the owner on Dec 27, 2024. It is now read-only.

Commit

Permalink
feat: transform disabled option WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed Sep 17, 2022
1 parent 490d2cc commit b4858a2
Show file tree
Hide file tree
Showing 7 changed files with 424 additions and 7 deletions.
12 changes: 12 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,18 @@ Indicates the resulting format of the OpenAPI document between `json` and `yaml`

Default value is `yaml`, if you use a unknown value `yaml` will be used.

### disabledParams (object)

By default all parameters in the postman collection that has the field `"disabled": true` is ignored and not included in the resulting OpenAPI doc, you can customize this behaviour with this options
| Param | Description |
|------------------|------------------------------------------------------------------------------------|
| `includeQuery` | Boolean. Indicates if the "query" paraneters disabled should be included into the OpenAPI. |
| `includeHeader` | Boolean. Indicates if the "header" paraneters disabled should be included into the OpenAPI. |

* When is not disabled
* When one exist disabled
* When exist more than one parameter with same name and disabled.

# Features

## Basic conversion
Expand Down
17 changes: 10 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const jsonc = require('jsonc-parser')
async function postmanToOpenApi (input, output, {
info = {}, defaultTag = 'default', pathDepth = 0,
auth: optsAuth, servers, externalDocs = {}, folders = {},
responseHeaders = true, replaceVars = false, additionalVars = {}, outputFormat = 'yaml'
responseHeaders = true, replaceVars = false, additionalVars = {}, outputFormat = 'yaml',
disabledParams = { includeQuery: false, includeHeader: false }
} = {}) {
// TODO validate?
let collectionFile = await resolveInput(input)
Expand Down Expand Up @@ -51,7 +52,7 @@ async function postmanToOpenApi (input, output, {
...(description ? { description } : {}),
...parseBody(body, method),
...parseOperationAuth(auth, securitySchemes, optsAuth),
...parseParameters(query, header, joinedPath, paramsMeta, pathVars),
...parseParameters(query, header, joinedPath, paramsMeta, pathVars, disabledParams),
...parseResponse(response, events, responseHeaders)
}
}
Expand Down Expand Up @@ -228,19 +229,21 @@ function mapFormData () {
}

/* Parse the Postman query and header and transform into OpenApi parameters */
function parseParameters (query, header, paths, paramsMeta = {}, pathVars) {
function parseParameters (query, header, paths, paramsMeta = {}, pathVars,
{ includeQuery = false, includeHeader = false }) {
// parse Headers
let parameters = header.reduce(mapParameters('header'), [])
let parameters = header.reduce(mapParameters('header', includeHeader), [])
// parse Query
parameters = query.reduce(mapParameters('query'), parameters)
parameters = query.reduce(mapParameters('query', includeQuery), parameters)
// Path params
parameters.push(...extractPathParameters(paths, paramsMeta, pathVars))
return (parameters.length) ? { parameters } : {}
}

/* Accumulator function for different types of parameters */
function mapParameters (type) {
return (parameters, { key, description, value }) => {
function mapParameters (type, includeDisabled) {
return (parameters, { key, description, value, disabled }) => {
if (!includeDisabled && disabled === true) return parameters
const required = /\[required\]/gi.test(description)
parameters.push({
name: key,
Expand Down
21 changes: 21 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ const EXPECTED_RAW_BODY = readFileSync('./test/resources/output/RawBody.yml', 'u
const EXPECTED_NULL_HEADER = readFileSync('./test/resources/output/NullHeader.yml', 'utf8')
const EXPECTED_COLLECTION_WRAPPER = readFileSync('./test/resources/output/CollectionWrapper.yml', 'utf8')
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 AUTH_DEFINITIONS = {
myCustomAuth: {
Expand Down Expand Up @@ -125,6 +127,7 @@ describe('Library specs', function () {
const COLLECTION_RESPONSES_JSON_ERROR = `./test/resources/input/${version}/ResponsesJsonError.json`
const COLLECTION_RESPONSES_EMPTY = `./test/resources/input/${version}/ResponsesEmpty.json`
const COLLECTION_JSON_COMMENTS = `./test/resources/input/${version}/JsonComments.json`
const COLLECTION_DISABLED = `./test/resources/input/${version}/DisabledParams.json`

it('should work with a basic transform', async function () {
const result = await postmanToOpenApi(COLLECTION_BASIC, OUTPUT_PATH, {})
Expand Down Expand Up @@ -497,6 +500,24 @@ describe('Library specs', function () {
const result = await postmanToOpenApi(COLLECTION_BASIC, OUTPUT_PATH, { outputFormat: 'json' })
equal(result, EXPECTED_BASIC_JSON)
})

it('should not parse `disabled` parameters', async function () {
const result = await postmanToOpenApi(COLLECTION_DISABLED, OUTPUT_PATH)
equal(result, EXPECTED_DISABLED_PARAMS_DEFAULT)
})

it.only('should parse `disabled` parameters if option is used', async function () {
const result = await postmanToOpenApi(COLLECTION_DISABLED, OUTPUT_PATH, {
disabledParams: {
includeQuery: true,
includeHeader: true
}
})
equal(result, EXPECTED_DISABLED_PARAMS_ALL)
})

// should include query but not header
// should include headers but not query
})
})

Expand Down
121 changes: 121 additions & 0 deletions test/resources/input/v2/DisabledParams.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"info": {
"_postman_id": "43c37e23-7cc8-4e6b-acf1-46396b4f4bfd",
"name": "DisabledParams",
"description": "Test API for disabled parameters feature",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json",
"_exporter_id": "64956"
},
"item": [
{
"name": "Get list of users",
"request": {
"method": "GET",
"header": [
{
"key": "X-My-Header",
"value": "hudjilksns78jsijns090",
"description": "Custom header [required]",
"type": "text"
},
{
"key": "X-My-Header",
"value": "1234567890",
"description": "Custom header [required]",
"type": "text",
"disabled": true
},
{
"key": "X-Other",
"value": "other",
"description": "Another header [REQUIRED]",
"type": "text"
},
{
"key": "No-description",
"value": "header without description",
"type": "text"
},
{
"key": "No-value",
"value": "",
"description": "header without value",
"type": "text"
},
{
"key": "X-Disabled-Header",
"value": "QWERTY",
"description": "Disabled parameter",
"type": "text",
"disabled": true
}
],
"url": {
"raw": "https://api.io/:section/users?name=Jhon&review=true",
"protocol": "https",
"host": [
"api",
"io"
],
"path": [
":section",
"users"
],
"query": [
{
"key": "age",
"value": "45",
"description": "Disabled param",
"disabled": true
},
{
"key": "name",
"value": "Jhon",
"description": "Filter by name"
},
{
"key": "review",
"value": "true",
"description": "Indicate if should be reviewed or not"
},
{
"key": "name",
"value": "Mark",
"description": "Disabled param duplicated",
"disabled": true
}
],
"variable": [
{
"key": "section",
"value": "spain",
"description": "A path parameter"
}
]
},
"description": "Obtain a list of users that fullfill the conditions of the filters"
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
}
]
}
121 changes: 121 additions & 0 deletions test/resources/input/v21/DisabledParams.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"info": {
"_postman_id": "43c37e23-7cc8-4e6b-acf1-46396b4f4bfd",
"name": "DisabledParams",
"description": "Test API for disabled parameters feature",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "64956"
},
"item": [
{
"name": "Get list of users",
"request": {
"method": "GET",
"header": [
{
"key": "X-My-Header",
"value": "hudjilksns78jsijns090",
"description": "Custom header [required]",
"type": "text"
},
{
"key": "X-My-Header",
"value": "1234567890",
"description": "Custom header [required]",
"type": "text",
"disabled": true
},
{
"key": "X-Other",
"value": "other",
"description": "Another header [REQUIRED]",
"type": "text"
},
{
"key": "No-description",
"value": "header without description",
"type": "text"
},
{
"key": "No-value",
"value": "",
"description": "header without value",
"type": "text"
},
{
"key": "X-Disabled-Header",
"value": "QWERTY",
"description": "Disabled parameter",
"type": "text",
"disabled": true
}
],
"url": {
"raw": "https://api.io/:section/users?name=Jhon&review=true",
"protocol": "https",
"host": [
"api",
"io"
],
"path": [
":section",
"users"
],
"query": [
{
"key": "age",
"value": "45",
"description": "Disabled param",
"disabled": true
},
{
"key": "name",
"value": "Jhon",
"description": "Filter by name"
},
{
"key": "review",
"value": "true",
"description": "Indicate if should be reviewed or not"
},
{
"key": "name",
"value": "Mark",
"description": "Disabled param duplicated",
"disabled": true
}
],
"variable": [
{
"key": "section",
"value": "spain",
"description": "A path parameter"
}
]
},
"description": "Obtain a list of users that fullfill the conditions of the filters"
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
}
]
}
Loading

0 comments on commit b4858a2

Please sign in to comment.