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

Allow parsing request bodies and examples that has JSON with comments #190

Merged
merged 1 commit into from
Jun 18, 2022
Merged
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
19 changes: 12 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ const { dump } = require('js-yaml')
const { parseMdTable } = require('./md-utils')
const { version } = require('../package.json')
const replacePostmanVariables = require('./var-replacer')
const jsonc = require('jsonc-parser')

async function postmanToOpenApi (input, output, {
info = {}, defaultTag = 'default', pathDepth = 0,
@@ -149,9 +150,9 @@ function parseBody (body = {}, method) {
let example = ''
if (language === 'json') {
if (raw) {
try {
example = JSON.parse(raw)
} catch (e) {
const errors = []
example = jsonc.parse(raw, errors)
if (errors.length > 0) {
example = raw
}
}
@@ -539,11 +540,15 @@ function parseExamples (bodies, 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)
if (language === 'json') {
const errors = []
const parsedBody = jsonc.parse((body.trim().length === 0) ? '{}' : body, errors)
if (errors.length > 0) {
throw new Error('Error parsing response example "' + name + '"')
}
return parsedBody
}
return body
}

function parseResponseHeaders (headerArray, responseHeaders) {
11 changes: 11 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -89,6 +89,7 @@
"dependencies": {
"commander": "^8.3.0",
"js-yaml": "^4.1.0",
"jsonc-parser": "3.0.0",
"marked": "^4.0.16",
"mustache": "^4.2.0"
},
9 changes: 8 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
@@ -59,6 +59,7 @@ const EXPECTED_BASEPATH_VAR = readFileSync('./test/resources/output/BasepathVar.
const EXPECTED_RAW_BODY = readFileSync('./test/resources/output/RawBody.yml', 'utf8')
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 AUTH_DEFINITIONS = {
myCustomAuth: {
@@ -121,6 +122,7 @@ describe('Library specs', function () {
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`
const COLLECTION_JSON_COMMENTS = `./test/resources/input/${version}/JsonComments.json`

it('should work with a basic transform', async function () {
const result = await postmanToOpenApi(COLLECTION_BASIC, OUTPUT_PATH, {})
@@ -475,14 +477,19 @@ describe('Library specs', function () {
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"
message: 'Error parsing response example "Create new User automatic id"'
})
})

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)
})

it('should not fail if request body and response body have json with comments', async function () {
const result = await postmanToOpenApi(COLLECTION_JSON_COMMENTS, OUTPUT_PATH, { pathDepth: 2 })
equal(result, EXPECTED_COLLECTION_JSON_COMMENTS)
})
})
})

Loading