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

Commit

Permalink
Merge pull request #177 from joolfe/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
joolfe authored Feb 19, 2022
2 parents 17af052 + 3dca4d5 commit d6ca857
Show file tree
Hide file tree
Showing 12 changed files with 11,052 additions and 58 deletions.
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/lib/index.js"
}
]
}
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
## [2.2.0](https://github.com/joolfe/postman-to-openapi/compare/2.1.0...2.2.0) (2022-02-19)


### Features

* Add error handling in response example parse ([acba056](https://github.com/joolfe/postman-to-openapi/commit/acba056c307cf07d143476956e115e8b7bd16f93))


### Bug Fixes

* update dependencies for moderate vulnerability ([f0ccc6f](https://github.com/joolfe/postman-to-openapi/commit/f0ccc6f252a83048cdefc04033c4d8d896fb3403))


### Build System

* update version ([5e2edff](https://github.com/joolfe/postman-to-openapi/commit/5e2edff9e352e2611f47f30ce5b6886b6047065d))

## [2.1.0](https://github.com/joolfe/postman-to-openapi/compare/2.0.1...2.1.0) (2022-01-15)


Expand Down
13 changes: 11 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,18 +525,27 @@ function parseExamples (bodies, language) {
examples: bodies.reduce((ex, { name: summary, body }, i) => {
ex[`example-${i}`] = {
summary,
value: (language === 'json') ? JSON.parse(body) : body
value: safeSampleParse(body, summary, language)
}
return ex
}, {})
}
} else {
const { body, name } = bodies[0]
return {
example: (language === 'json') ? JSON.parse(bodies[0].body) : bodies[0].body
example: safeSampleParse(body, name, language)
}
}
}

function safeSampleParse (body, name, language) {
try {
return (language === 'json') ? JSON.parse((body.trim().length === 0) ? '{}' : body) : body
} catch (error) {
throw new Error('Error parsing response example "' + name + '" parse error is: ' + error.message)
}
}

function parseResponseHeaders (headerArray, responseHeaders) {
if (!responseHeaders) {
return {}
Expand Down
114 changes: 60 additions & 54 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postman-to-openapi",
"version": "2.1.0",
"version": "2.2.0",
"description": "Convert postman collection to OpenAPI spec",
"main": "lib/index.js",
"types": "types/index.d.ts",
Expand Down
17 changes: 16 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { describe, it, afterEach } = require('mocha')
const postmanToOpenApi = require('../lib')
const path = require('path')
const { equal, ok } = require('assert').strict
const { equal, ok, rejects } = require('assert').strict
const { readFileSync, existsSync, unlinkSync } = require('fs')
const { version } = require('../package.json')
const { promises: { readFile } } = require('fs')
Expand Down Expand Up @@ -47,6 +47,7 @@ const EXPECTED_X_LOGO = readFileSync('./test/resources/output/XLogo.yml', 'utf8'
const EXPECTED_X_LOGO_VAR = readFileSync('./test/resources/output/XLogoVar.yml', 'utf8')
const EXPECTED_AUTH_OPTIONS = readFileSync('./test/resources/output/AuthOptions.yml', 'utf8')
const EXPECTED_RESPONSES = readFileSync('./test/resources/output/Responses.yml', 'utf8')
const EXPECTED_EMPTY_RESPONSES = readFileSync('./test/resources/output/ResponsesEmpty.yml', 'utf8')
const EXPECTED_RESPONSES_MULTI_LANG = readFileSync('./test/resources/output/ResponsesMultiLang.yml', 'utf8')
const EXPECTED_AUTH_REQUEST = readFileSync('./test/resources/output/AuthRequest.yml', 'utf8')
const EXPECTED_RESPONSES_NO_HEADERS = readFileSync('./test/resources/output/ResponsesNoHeaders.yml', 'utf8')
Expand Down Expand Up @@ -118,6 +119,8 @@ describe('Library specs', function () {
const COLLECTION_BASEURL_VAR = `./test/resources/input/${version}/BasepathVar.json`
const COLLECTION_RAW_BODY = `./test/resources/input/${version}/RawBody.json`
const COLLECTION_COLLECTION_WRAPPER = `./test/resources/input/${version}/CollectionWrapper.json`
const COLLECTION_RESPONSES_JSON_ERROR = `./test/resources/input/${version}/ResponsesJsonError.json`
const COLLECTION_RESPONSES_EMPTY = `./test/resources/input/${version}/ResponsesEmpty.json`

it('should work with a basic transform', async function () {
const result = await postmanToOpenApi(COLLECTION_BASIC, OUTPUT_PATH, {})
Expand Down Expand Up @@ -468,6 +471,18 @@ describe('Library specs', function () {
const result = await postmanToOpenApi(COLLECTION_COLLECTION_WRAPPER, OUTPUT_PATH, {})
equal(result, EXPECTED_COLLECTION_WRAPPER)
})

it('should return friendly error message when a response sample body has an error in JSON', async function () {
await rejects(postmanToOpenApi(COLLECTION_RESPONSES_JSON_ERROR, OUTPUT_PATH, {}), {
name: 'Error',
message: "Error parsing response example \"Create new User automatic id\" parse error is: Unexpected token ' in JSON at position 1"
})
})

it('should not fail if response body is json but empty', async function () {
const result = await postmanToOpenApi(COLLECTION_RESPONSES_EMPTY, OUTPUT_PATH, { pathDepth: 2 })
equal(result, EXPECTED_EMPTY_RESPONSES)
})
})
})

Expand Down
Loading

0 comments on commit d6ca857

Please sign in to comment.