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

Commit

Permalink
fix: error when no "options" in body #64
Browse files Browse the repository at this point in the history
Close #64
  • Loading branch information
joolfe committed Aug 25, 2020
1 parent aff566b commit a4e8af4
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function parseContact (variables, optsContact = {}) {
function parseBody (body = {}, method) {
// Swagger validation return an error if GET has body
if (['GET'].includes(method)) return {}
const { mode, raw, options } = body
const { mode, raw, options = { raw: { language: 'json' } } } = body
let content = {}
switch (mode) {
case 'raw': {
Expand Down
8 changes: 7 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const COLLECTION_LICENSE_CONTACT = './test/resources/input/LicenseContact.json'
const COLLECTION_DEPTH_PATH_PARAMS = './test/resources/input/DepthPathParams.json'
const COLLECTION_PARSE_STATUS_CODE = './test/resources/input/ParseStatusCode.json'
const COLLECTION_NO_PATH = './test/resources/input/NoPath.json'
const COLLECTION_NO_OPTIONS = './test/resources/input/NoOptionsInBody.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 @@ -226,8 +227,13 @@ describe('Library specs', function () {
equal(result, EXPECTED_PARSE_STATUS_CODE)
})

it.only('should parse operation when no path (only domain)', async function () {
it('should parse operation when no path (only domain)', async function () {
const result = await postmanToOpenApi(COLLECTION_NO_PATH)
equal(result, EXPECTED_NO_PATH)
})

it('should work if no options in request body', async function () {
const result = await postmanToOpenApi(COLLECTION_NO_OPTIONS, OUTPUT_PATH, {})
equal(result, EXPECTED_BASIC)
})
})
121 changes: 121 additions & 0 deletions test/resources/input/NoOptionsInBody.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"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": "Create new User",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"example\": \"field\",\n \"other\": {\n \"data1\": \"yes\",\n \"data2\": \"no\"\n }\n}"
},
"url": {
"raw": "https://api.io/users",
"protocol": "https",
"host": [
"api",
"io"
],
"path": [
"users"
]
},
"description": "Create a new user into your amazing API"
},
"response": []
},
{
"name": "Create a post",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "file",
"file": {},
"options": {
"raw": {
"language": "text"
}
}
},
"url": {
"raw": "https://api.io/posts",
"protocol": "https",
"host": [
"api",
"io"
],
"path": [
"posts"
]
}
},
"response": []
},
{
"name": "Create a note",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "This is an example Note",
"options": {
"raw": {
"language": "text"
}
}
},
"url": {
"raw": "https://api.io/note",
"protocol": "https",
"host": [
"api",
"io"
],
"path": [
"note"
]
},
"description": "Just an example of text raw body"
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"id": "ef248fcc-2944-4cba-837f-680b10a45b30",
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"id": "8e7453aa-5712-4a90-bc32-965c7d47906a",
"type": "text/javascript",
"exec": [
""
]
}
}
],
"variable": [
{
"id": "d04439bd-c604-4758-bde2-3c873140f38c",
"key": "version",
"value": "1.1.0"
}
],
"protocolProfileBehavior": {}
}

0 comments on commit a4e8af4

Please sign in to comment.