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

Commit

Permalink
feat: request bodies & examples with json comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dhwaneetbhatt authored Jun 7, 2022
1 parent 5e57b58 commit a2df3a6
Show file tree
Hide file tree
Showing 7 changed files with 1,017 additions and 8 deletions.
19 changes: 12 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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) {
Expand Down
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
Expand Up @@ -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"
},
Expand Down
9 changes: 8 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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, {})
Expand Down Expand Up @@ -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)
})
})
})

Expand Down
Loading

0 comments on commit a2df3a6

Please sign in to comment.