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

Commit

Permalink
fix: fallback to parsing non-json raw body as text (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
MilyMilo committed Sep 22, 2021
1 parent b8d6931 commit abb85ec
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,22 @@ function parseBody (body = {}, method) {
switch (mode) {
case 'raw': {
const { raw: { language } } = options

let example = ''
if (language === 'json') {
if (raw) {
try {
example = JSON.parse(raw)
} catch (e) {
example = raw
}
}

content = {
'application/json': {
schema: {
type: 'object',
example: raw ? JSON.parse(raw) : ''
example
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { version } = require('../package.json')
const OUTPUT_PATH = path.join(__dirname, '/openAPIRes.yml')

const COLLECTION_NO_OPTIONS = './test/resources/input/NoOptionsInBody.json'
const COLLECTION_RAW_BODY = './test/resources/input/RawBody.json'

const EXPECTED_BASIC = readFileSync('./test/resources/output/Basic.yml', 'utf8')
const EXPECTED_INFO_OPTS = readFileSync('./test/resources/output/InfoOpts.yml', 'utf8')
Expand Down Expand Up @@ -53,6 +54,7 @@ const EXPECTED_FORM_URLENCODED = readFileSync('./test/resources/output/FormUrlen
const EXPECTED_VARIABLES = readFileSync('./test/resources/output/Variables.yml', 'utf8')
const EXPECTED_VARIABLES_ADDITIONAL = readFileSync('./test/resources/output/VariablesAdditional.yml', 'utf8')
const EXPECTED_BASEPATH_VAR = readFileSync('./test/resources/output/BasepathVar.yml', 'utf8')
const EXPECTED_RAW_BODY = readFileSync('./test/resources/output/RawBody.yml', 'utf8')

const AUTH_DEFINITIONS = {
myCustomAuth: {
Expand Down Expand Up @@ -459,6 +461,11 @@ describe('Library specs', function () {
equal(result, EXPECTED_BASIC)
})

it('should try to parse raw body as json but fallback to text', async function () {
const result = await postmanToOpenApi(COLLECTION_RAW_BODY, OUTPUT_PATH, {})
equal(result, EXPECTED_RAW_BODY)
})

it('should expose the version of the library', async function () {
equal(postmanToOpenApi.version, version)
})
Expand Down
37 changes: 37 additions & 0 deletions test/resources/input/RawBody.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"info": {
"_postman_id": "2a45baa5-352f-4831-8483-1a0fcbc7da37",
"name": "Postman to OpenAPI",
"description": "Mi super test collection from postman",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Test Raw Body",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "testesttestest"
},
"url": {
"raw": "https://api.io/test",
"protocol": "https",
"host": [
"api",
"io"
],
"path": [
"test"
]
},
"description": "Test Raw Body"
},
"response": []
}
],
"event": [],
"variable": [],
"protocolProfileBehavior": {}
}
25 changes: 25 additions & 0 deletions test/resources/output/RawBody.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
openapi: 3.0.0
info:
title: Postman to OpenAPI
description: Mi super test collection from postman
version: 1.0.0
servers:
- url: https://api.io
paths:
/test:
post:
tags:
- default
summary: Test Raw Body
description: Test Raw Body
requestBody:
content:
application/json:
schema:
type: object
example: testesttestest
responses:
'200':
description: Successful response
content:
application/json: {}

0 comments on commit abb85ec

Please sign in to comment.