From 1c376cff2bc77420ae96fb188d27bac94f0c349b Mon Sep 17 00:00:00 2001 From: Jorge Date: Sat, 24 Apr 2021 20:47:30 +0200 Subject: [PATCH] fix: avoid fail `body.raw` is empty string Close #101 --- lib/index.js | 2 +- package-lock.json | 4 +-- package.json | 2 +- test/index.spec.js | 1 + test/resources/input/v2/SimplePost.json | 25 ++++++++++++++--- test/resources/input/v21/SimplePost.json | 35 +++++++++++++++++++++--- test/resources/output/CustomTag.yml | 17 ++++++++++++ test/resources/output/InfoOpts.yml | 17 ++++++++++++ 8 files changed, 91 insertions(+), 12 deletions(-) diff --git a/lib/index.js b/lib/index.js index dcb500d..6c71012 100644 --- a/lib/index.js +++ b/lib/index.js @@ -128,7 +128,7 @@ function parseBody (body = {}, method) { 'application/json': { schema: { type: 'object', - example: JSON.parse(raw) + example: raw ? JSON.parse(raw) : '' } } } diff --git a/package-lock.json b/package-lock.json index 546a083..72f3d35 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "postman-to-openapi", - "version": "1.7.2", + "version": "1.7.3", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "1.7.2", + "version": "1.7.3", "license": "MIT", "dependencies": { "js-yaml": "^4.1.0", diff --git a/package.json b/package.json index b3d166d..c6aea0d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postman-to-openapi", - "version": "1.7.2", + "version": "1.7.3", "description": "Convert postman collection to OpenAPI spec", "main": "lib/index.js", "scripts": { diff --git a/test/index.spec.js b/test/index.spec.js index e763ffa..158cebd 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -88,6 +88,7 @@ describe('Library specs', function () { termsOfService: 'http://tos.myweb.com' } }) + console.log(result) equal(result, EXPECTED_INFO_OPTS) }) diff --git a/test/resources/input/v2/SimplePost.json b/test/resources/input/v2/SimplePost.json index caa6c20..cd94f0d 100644 --- a/test/resources/input/v2/SimplePost.json +++ b/test/resources/input/v2/SimplePost.json @@ -1,6 +1,6 @@ { "info": { - "_postman_id": "56e38043-4473-4faa-a735-1815ca79ee04", + "_postman_id": "e223ab3f-2683-4759-9e41-8167a7caaf99", "name": "Simple Post", "description": "Just a simple collection for test", "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json" @@ -24,14 +24,31 @@ "description": "Create a new user into your amazing API" }, "response": [] + }, + { + "name": "Post empty raw", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": "https://api.io/raw", + "description": "Create a new user into your amazing API" + }, + "response": [] } ], "variable": [ { - "id": "a1b68638-d65e-4ee8-9a91-4d5749e9c2f2", "key": "version", "value": "2.3.0" } - ], - "protocolProfileBehavior": {} + ] } \ No newline at end of file diff --git a/test/resources/input/v21/SimplePost.json b/test/resources/input/v21/SimplePost.json index 4d52052..9e62098 100644 --- a/test/resources/input/v21/SimplePost.json +++ b/test/resources/input/v21/SimplePost.json @@ -1,6 +1,6 @@ { "info": { - "_postman_id": "3ad7e1b4-f3a1-4a8f-8bd0-56ff18f53339", + "_postman_id": "e223ab3f-2683-4759-9e41-8167a7caaf99", "name": "Simple Post", "description": "Just a simple collection for test", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" @@ -34,14 +34,41 @@ "description": "Create a new user into your amazing API" }, "response": [] + }, + { + "name": "Post empty raw", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "https://api.io/raw", + "protocol": "https", + "host": [ + "api", + "io" + ], + "path": [ + "raw" + ] + }, + "description": "Create a new user into your amazing API" + }, + "response": [] } ], "variable": [ { - "id": "a1b68638-d65e-4ee8-9a91-4d5749e9c2f2", "key": "version", "value": "2.3.0" } - ], - "protocolProfileBehavior": {} + ] } \ No newline at end of file diff --git a/test/resources/output/CustomTag.yml b/test/resources/output/CustomTag.yml index 5d5e76c..6ba793e 100644 --- a/test/resources/output/CustomTag.yml +++ b/test/resources/output/CustomTag.yml @@ -27,3 +27,20 @@ paths: description: Successful response content: application/json: {} + /raw: + post: + tags: + - Custom Tag + summary: Post empty raw + description: Create a new user into your amazing API + requestBody: + content: + application/json: + schema: + type: object + example: '' + responses: + '200': + description: Successful response + content: + application/json: {} diff --git a/test/resources/output/InfoOpts.yml b/test/resources/output/InfoOpts.yml index a7d7255..4a03db1 100644 --- a/test/resources/output/InfoOpts.yml +++ b/test/resources/output/InfoOpts.yml @@ -28,3 +28,20 @@ paths: description: Successful response content: application/json: {} + /raw: + post: + tags: + - default + summary: Post empty raw + description: Create a new user into your amazing API + requestBody: + content: + application/json: + schema: + type: object + example: '' + responses: + '200': + description: Successful response + content: + application/json: {}