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

Commit 14093e3

Browse files
committed
Fix: support null body in examples responses
1 parent 78a0895 commit 14093e3

4 files changed

Lines changed: 153 additions & 1 deletion

File tree

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ function parseExamples (bodies, language) {
542542
function safeSampleParse (body, name, language) {
543543
if (language === 'json') {
544544
const errors = []
545-
const parsedBody = jsonc.parse((body.trim().length === 0) ? '{}' : body, errors)
545+
const parsedBody = jsonc.parse((body === null || body.trim().length === 0) ? '{}' : body, errors)
546546
if (errors.length > 0) {
547547
throw new Error('Error parsing response example "' + name + '"')
548548
}

test/resources/input/v2/ResponsesEmpty.json

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,74 @@
159159
],
160160
"cookie": [],
161161
"body": ""
162+
},
163+
{
164+
"name": "Create new User automatic id (null)",
165+
"originalRequest": {
166+
"method": "POST",
167+
"header": [],
168+
"body": {
169+
"mode": "raw",
170+
"raw": "{\n \"createdAt\": \"2021-06-04T15:50:38.568Z\",\n \"name\": \"Carol\",\n \"avatar\": \"https://cdn.fakercloud.com/avatars/nelsonjoyce_128.jpg\"\n}",
171+
"options": {
172+
"raw": {
173+
"language": "json"
174+
}
175+
}
176+
},
177+
"url": "https://60bb37ab42e1d000176206c3.mockapi.io/api/v1/users"
178+
},
179+
"status": "Created",
180+
"code": 201,
181+
"_postman_previewlanguage": "json",
182+
"header": [
183+
{
184+
"key": "Server",
185+
"value": "Cowboy"
186+
},
187+
{
188+
"key": "Connection",
189+
"value": "keep-alive"
190+
},
191+
{
192+
"key": "X-Powered-By",
193+
"value": "Express"
194+
},
195+
{
196+
"key": "Access-Control-Allow-Origin",
197+
"value": "*"
198+
},
199+
{
200+
"key": "Access-Control-Allow-Methods",
201+
"value": "GET,PUT,POST,DELETE,OPTIONS"
202+
},
203+
{
204+
"key": "Access-Control-Allow-Headers",
205+
"value": "X-Requested-With,Content-Type,Cache-Control,access_token"
206+
},
207+
{
208+
"key": "Content-Type",
209+
"value": "application/json"
210+
},
211+
{
212+
"key": "Content-Length",
213+
"value": "131"
214+
},
215+
{
216+
"key": "Vary",
217+
"value": "Accept-Encoding"
218+
},
219+
{
220+
"key": "Date",
221+
"value": "Sat, 05 Jun 2021 08:49:09 GMT"
222+
},
223+
{
224+
"key": "Via",
225+
"value": "1.1 vegur"
226+
}
227+
],
228+
"cookie": [],
229+
"body": null
162230
}
163231
]
164232
},

test/resources/input/v21/ResponsesEmpty.json

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,87 @@
198198
],
199199
"cookie": [],
200200
"body": ""
201+
},
202+
{
203+
"name": "Create new User automatic id (null)",
204+
"originalRequest": {
205+
"method": "POST",
206+
"header": [],
207+
"body": {
208+
"mode": "raw",
209+
"raw": "{\n \"createdAt\": \"2021-06-04T15:50:38.568Z\",\n \"name\": \"Carol\",\n \"avatar\": \"https://cdn.fakercloud.com/avatars/nelsonjoyce_128.jpg\"\n}",
210+
"options": {
211+
"raw": {
212+
"language": "json"
213+
}
214+
}
215+
},
216+
"url": {
217+
"raw": "https://60bb37ab42e1d000176206c3.mockapi.io/api/v1/users",
218+
"protocol": "https",
219+
"host": [
220+
"60bb37ab42e1d000176206c3",
221+
"mockapi",
222+
"io"
223+
],
224+
"path": [
225+
"api",
226+
"v1",
227+
"users"
228+
]
229+
}
230+
},
231+
"status": "Created",
232+
"code": 201,
233+
"_postman_previewlanguage": "json",
234+
"header": [
235+
{
236+
"key": "Server",
237+
"value": "Cowboy"
238+
},
239+
{
240+
"key": "Connection",
241+
"value": "keep-alive"
242+
},
243+
{
244+
"key": "X-Powered-By",
245+
"value": "Express"
246+
},
247+
{
248+
"key": "Access-Control-Allow-Origin",
249+
"value": "*"
250+
},
251+
{
252+
"key": "Access-Control-Allow-Methods",
253+
"value": "GET,PUT,POST,DELETE,OPTIONS"
254+
},
255+
{
256+
"key": "Access-Control-Allow-Headers",
257+
"value": "X-Requested-With,Content-Type,Cache-Control,access_token"
258+
},
259+
{
260+
"key": "Content-Type",
261+
"value": "application/json"
262+
},
263+
{
264+
"key": "Content-Length",
265+
"value": "131"
266+
},
267+
{
268+
"key": "Vary",
269+
"value": "Accept-Encoding"
270+
},
271+
{
272+
"key": "Date",
273+
"value": "Sat, 05 Jun 2021 08:49:09 GMT"
274+
},
275+
{
276+
"key": "Via",
277+
"value": "1.1 vegur"
278+
}
279+
],
280+
"cookie": [],
281+
"body": null
201282
}
202283
]
203284
},

test/resources/output/ResponsesEmpty.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ paths:
8585
example-1:
8686
summary: Create new User automatic id
8787
value: {}
88+
example-2:
89+
summary: Create new User automatic id (null)
90+
value: {}
8891
/users/{user_id}:
8992
get:
9093
tags:

0 commit comments

Comments
 (0)