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

Commit 473db6b

Browse files
committed
feat: if no language is chosen in body raw mode then use '*/*'
1 parent 53cee26 commit 473db6b

4 files changed

Lines changed: 70 additions & 12 deletions

File tree

lib/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function parseExternalDocs (variables, optsExternalDocs) {
141141
function parseBody (body = {}, method) {
142142
// Swagger validation return an error if GET has body
143143
if (['GET', 'DELETE'].includes(method)) return {}
144-
const { mode, raw, options = { raw: { language: 'json' } } } = body
144+
const { mode, raw, options = { raw } } = body
145145
let content = {}
146146
switch (mode) {
147147
case 'raw': {
@@ -175,10 +175,11 @@ function parseBody (body = {}, method) {
175175
}
176176
} else {
177177
content = {
178-
'application/json': {
178+
'*/*': {
179179
schema: {
180180
type: 'string',
181-
example: raw
181+
// To protect from object types we always stringify this
182+
example: JSON.stringify(raw)
182183
}
183184
}
184185
}

test/index.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const COLLECTION_NO_OPTIONS = './test/resources/input/NoOptionsInBody.json'
1414
const COLLECTION_NULL_HEADERS = './test/resources/input/NullHeaders.json'
1515

1616
const EXPECTED_BASIC = readFileSync('./test/resources/output/Basic.yml', 'utf8')
17+
const EXPECTED_BASIC_NO_OPTS = readFileSync('./test/resources/output/BasicNoOptions.yml', 'utf8')
1718
const EXPECTED_INFO_OPTS = readFileSync('./test/resources/output/InfoOpts.yml', 'utf8')
1819
const EXPECTED_NO_VERSION = readFileSync('./test/resources/output/NoVersion.yml', 'utf8')
1920
const EXPECTED_CUSTOM_TAG = readFileSync('./test/resources/output/CustomTag.yml', 'utf8')
@@ -495,7 +496,7 @@ describe('Library specs', function () {
495496

496497
it('should work if no options in request body', async function () {
497498
const result = await postmanToOpenApi(COLLECTION_NO_OPTIONS, OUTPUT_PATH, {})
498-
equal(result, EXPECTED_BASIC)
499+
equal(result, EXPECTED_BASIC_NO_OPTS)
499500
})
500501

501502
it('should expose the version of the library', async function () {
@@ -510,6 +511,6 @@ describe('Library specs', function () {
510511
it('should work with string as input (instead of a file path)', async function () {
511512
const collectionString = await readFile(COLLECTION_NO_OPTIONS, 'utf8')
512513
const result = await postmanToOpenApi(collectionString, OUTPUT_PATH, {})
513-
equal(result, EXPECTED_BASIC)
514+
equal(result, EXPECTED_BASIC_NO_OPTS)
514515
})
515516
})
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Postman to OpenAPI
4+
description: Mi super test collection from postman
5+
version: 1.1.0
6+
servers:
7+
- url: https://api.io
8+
paths:
9+
/users:
10+
post:
11+
tags:
12+
- default
13+
summary: Create new User
14+
description: Create a new user into your amazing API
15+
requestBody:
16+
content:
17+
'*/*':
18+
schema:
19+
type: string
20+
example: >-
21+
"{\n \"example\": \"field\",\n \"other\": {\n
22+
\"data1\": \"yes\",\n \"data2\": \"no\"\n }\n}"
23+
responses:
24+
'200':
25+
description: Successful response
26+
content:
27+
application/json: {}
28+
/posts:
29+
post:
30+
tags:
31+
- default
32+
summary: Create a post
33+
requestBody:
34+
content:
35+
text/plain: {}
36+
responses:
37+
'200':
38+
description: Successful response
39+
content:
40+
application/json: {}
41+
/note:
42+
post:
43+
tags:
44+
- default
45+
summary: Create a note
46+
description: Just an example of text raw body
47+
requestBody:
48+
content:
49+
text/plain:
50+
schema:
51+
type: string
52+
example: This is an example Note
53+
responses:
54+
'200':
55+
description: Successful response
56+
content:
57+
application/json: {}

test/resources/output/NullHeader.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ paths:
1717
Retourne un token en cas de succès
1818
requestBody:
1919
content:
20-
application/json:
20+
'*/*':
2121
schema:
22-
type: object
23-
example:
24-
contenu:
25-
compte: '{{compte}}'
26-
motDePasse: '{{motDePasse}}'
27-
identifiant: '{{identifiant}}'
22+
type: string
23+
example: >-
24+
"{\r\n \"contenu\":\r\n {\r\n \"compte\": \"{{compte}}\",\r\n
25+
\"motDePasse\": \"{{motDePasse}}\",\r\n \"identifiant\":
26+
\"{{identifiant}}\"\r\n }\r\n}"
2827
parameters:
2928
- name: Content-Type
3029
in: header

0 commit comments

Comments
 (0)