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

Commit

Permalink
Fix: support null body in examples responses
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed Jul 25, 2022
1 parent 78a0895 commit 14093e3
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ function parseExamples (bodies, language) {
function safeSampleParse (body, name, language) {
if (language === 'json') {
const errors = []
const parsedBody = jsonc.parse((body.trim().length === 0) ? '{}' : body, errors)
const parsedBody = jsonc.parse((body === null || body.trim().length === 0) ? '{}' : body, errors)
if (errors.length > 0) {
throw new Error('Error parsing response example "' + name + '"')
}
Expand Down
68 changes: 68 additions & 0 deletions test/resources/input/v2/ResponsesEmpty.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,74 @@
],
"cookie": [],
"body": ""
},
{
"name": "Create new User automatic id (null)",
"originalRequest": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"createdAt\": \"2021-06-04T15:50:38.568Z\",\n \"name\": \"Carol\",\n \"avatar\": \"https://cdn.fakercloud.com/avatars/nelsonjoyce_128.jpg\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": "https://60bb37ab42e1d000176206c3.mockapi.io/api/v1/users"
},
"status": "Created",
"code": 201,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Server",
"value": "Cowboy"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "X-Powered-By",
"value": "Express"
},
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,PUT,POST,DELETE,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "X-Requested-With,Content-Type,Cache-Control,access_token"
},
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Content-Length",
"value": "131"
},
{
"key": "Vary",
"value": "Accept-Encoding"
},
{
"key": "Date",
"value": "Sat, 05 Jun 2021 08:49:09 GMT"
},
{
"key": "Via",
"value": "1.1 vegur"
}
],
"cookie": [],
"body": null
}
]
},
Expand Down
81 changes: 81 additions & 0 deletions test/resources/input/v21/ResponsesEmpty.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,87 @@
],
"cookie": [],
"body": ""
},
{
"name": "Create new User automatic id (null)",
"originalRequest": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"createdAt\": \"2021-06-04T15:50:38.568Z\",\n \"name\": \"Carol\",\n \"avatar\": \"https://cdn.fakercloud.com/avatars/nelsonjoyce_128.jpg\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "https://60bb37ab42e1d000176206c3.mockapi.io/api/v1/users",
"protocol": "https",
"host": [
"60bb37ab42e1d000176206c3",
"mockapi",
"io"
],
"path": [
"api",
"v1",
"users"
]
}
},
"status": "Created",
"code": 201,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Server",
"value": "Cowboy"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "X-Powered-By",
"value": "Express"
},
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Methods",
"value": "GET,PUT,POST,DELETE,OPTIONS"
},
{
"key": "Access-Control-Allow-Headers",
"value": "X-Requested-With,Content-Type,Cache-Control,access_token"
},
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Content-Length",
"value": "131"
},
{
"key": "Vary",
"value": "Accept-Encoding"
},
{
"key": "Date",
"value": "Sat, 05 Jun 2021 08:49:09 GMT"
},
{
"key": "Via",
"value": "1.1 vegur"
}
],
"cookie": [],
"body": null
}
]
},
Expand Down
3 changes: 3 additions & 0 deletions test/resources/output/ResponsesEmpty.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ paths:
example-1:
summary: Create new User automatic id
value: {}
example-2:
summary: Create new User automatic id (null)
value: {}
/users/{user_id}:
get:
tags:
Expand Down

0 comments on commit 14093e3

Please sign in to comment.