Skip to content

Commit 3068bd0

Browse files
authored
fix: missing code would result in an undefined response status (#2)
1 parent 3bf342a commit 3068bd0

File tree

5 files changed

+348
-0
lines changed

5 files changed

+348
-0
lines changed

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,10 @@ function parseResponseFromExamples(responses, responseHeaders) {
548548
// Group responses by status code
549549
const statusCodeMap = responses.reduce(
550550
(statusMap, { name, code, status: description, header, body, _postman_previewlanguage: language }) => {
551+
if (code === undefined) {
552+
code = 'default';
553+
}
554+
551555
if (code in statusMap) {
552556
if (!(language in statusMap[code].bodies)) {
553557
statusMap[code].bodies[language] = [];

test/index.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ const EXPECTED_EMPTY_RESPONSES = readFileSync('./test/resources/output/Responses
5555
const EXPECTED_RESPONSES_MULTI_LANG = readFileSync('./test/resources/output/ResponsesMultiLang.yml', 'utf8');
5656
const EXPECTED_AUTH_REQUEST = readFileSync('./test/resources/output/AuthRequest.yml', 'utf8');
5757
const EXPECTED_RESPONSES_NO_HEADERS = readFileSync('./test/resources/output/ResponsesNoHeaders.yml', 'utf8');
58+
const EXPECTED_RESPONSES_WITHOUT_STATUS_CODE = readFileSync(
59+
'./test/resources/output/ResponsesWithoutHTTPStatusCode.yml',
60+
'utf8'
61+
);
5862
const EXPECTED_FORM_DATA = readFileSync('./test/resources/output/FormData.yml', 'utf8');
5963
const EXPECTED_FORM_URLENCODED = readFileSync('./test/resources/output/FormUrlencoded.yml', 'utf8');
6064
const EXPECTED_VARIABLES = readFileSync('./test/resources/output/Variables.yml', 'utf8');
@@ -137,6 +141,7 @@ describe('Library specs', function () {
137141
const COLLECTION_JSON_COMMENTS = `./test/resources/input/${version}/JsonComments.json`;
138142
const COLLECTION_DISABLED = `./test/resources/input/${version}/DisabledParams.json`;
139143
const COLLECTION_OPERATION_IDS = `./test/resources/input/${version}/OperationIds.json`;
144+
const COLLECTION_RESPONSES_WITHOUT_STATUS_CODE = `./test/resources/input/${version}/ResponsesWithoutHTTPStatusCode.json`;
140145

141146
it('should work with a basic transform', async function () {
142147
const result = await postmanToOpenAPI(COLLECTION_BASIC, OUTPUT_PATH, {});
@@ -424,6 +429,11 @@ describe('Library specs', function () {
424429
expect(result).to.equal(EXPECTED_AUTH_REQUEST);
425430
});
426431

432+
it('should add responses from postman examples even if they are missing an HTTP Status Code', async function () {
433+
const result = await postmanToOpenAPI(COLLECTION_RESPONSES_WITHOUT_STATUS_CODE, OUTPUT_PATH, { pathDepth: 2 });
434+
expect(result).to.equal(EXPECTED_RESPONSES_WITHOUT_STATUS_CODE);
435+
});
436+
427437
it('should avoid headers in response', async function () {
428438
const result = await postmanToOpenAPI(COLLECTION_RESPONSES, OUTPUT_PATH, {
429439
pathDepth: 2,
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
{
2+
"info": {
3+
"_postman_id": "7f7ad829-6db0-4229-9c38-5e9f341ec7bb",
4+
"name": "Responses",
5+
"description": "Postman collection with saved responses that don't have an HTTP Status Code",
6+
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
7+
},
8+
"item": [
9+
{
10+
"name": "Create new User",
11+
"request": {
12+
"method": "POST",
13+
"header": [],
14+
"body": {
15+
"mode": "raw",
16+
"raw": "{\n \"id\": \"100\",\n \"createdAt\": \"2021-06-04T15:50:38.568Z\",\n \"name\": \"Carol\",\n \"avatar\": \"https://cdn.fakercloud.com/avatars/nelsonjoyce_128.jpg\"\n }",
17+
"options": {
18+
"raw": {
19+
"language": "json"
20+
}
21+
}
22+
},
23+
"url": "https://60bb37ab42e1d000176206c3.mockapi.io/api/v1/users",
24+
"description": "Create a new user into your amazing API"
25+
},
26+
"response": [
27+
{
28+
"name": "Create new User example",
29+
"originalRequest": {
30+
"method": "POST",
31+
"header": [],
32+
"body": {
33+
"mode": "raw",
34+
"raw": "{\n \"id\": \"100\",\n \"createdAt\": \"2021-06-04T15:50:38.568Z\",\n \"name\": \"Carol\",\n \"avatar\": \"https://cdn.fakercloud.com/avatars/nelsonjoyce_128.jpg\"\n }",
35+
"options": {
36+
"raw": {
37+
"language": "json"
38+
}
39+
}
40+
},
41+
"url": "https://60bb37ab42e1d000176206c3.mockapi.io/api/v1/users"
42+
},
43+
"_postman_previewlanguage": "json",
44+
"header": [
45+
{
46+
"key": "Server",
47+
"value": "Cowboy"
48+
},
49+
{
50+
"key": "Connection",
51+
"value": "keep-alive"
52+
},
53+
{
54+
"key": "X-Powered-By",
55+
"value": "Express"
56+
},
57+
{
58+
"key": "Access-Control-Allow-Origin",
59+
"value": "*"
60+
},
61+
{
62+
"key": "Access-Control-Allow-Methods",
63+
"value": "GET,PUT,POST,DELETE,OPTIONS"
64+
},
65+
{
66+
"key": "Access-Control-Allow-Headers",
67+
"value": "X-Requested-With,Content-Type,Cache-Control,access_token"
68+
},
69+
{
70+
"key": "Content-Type",
71+
"value": "application/json"
72+
},
73+
{
74+
"key": "Content-Length",
75+
"value": "131"
76+
},
77+
{
78+
"key": "Vary",
79+
"value": "Accept-Encoding"
80+
},
81+
{
82+
"key": "Date",
83+
"value": "Sat, 05 Jun 2021 08:42:32 GMT"
84+
},
85+
{
86+
"key": "Via",
87+
"value": "1.1 vegur"
88+
}
89+
],
90+
"cookie": [],
91+
"body": "{\n \"id\": \"51\",\n \"createdAt\": \"2021-06-04T15:50:38.568Z\",\n \"name\": \"Carol\",\n \"avatar\": \"https://cdn.fakercloud.com/avatars/nelsonjoyce_128.jpg\"\n}"
92+
}
93+
]
94+
}
95+
],
96+
"event": [
97+
{
98+
"listen": "prerequest",
99+
"script": {
100+
"type": "text/javascript",
101+
"exec": [""]
102+
}
103+
},
104+
{
105+
"listen": "test",
106+
"script": {
107+
"type": "text/javascript",
108+
"exec": [""]
109+
}
110+
}
111+
],
112+
"variable": [
113+
{
114+
"key": "version",
115+
"value": "1.2.0"
116+
},
117+
{
118+
"key": "user_id",
119+
"value": "50"
120+
}
121+
]
122+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
{
2+
"info": {
3+
"_postman_id": "7f7ad829-6db0-4229-9c38-5e9f341ec7bb",
4+
"name": "Responses",
5+
"description": "Postman collection with saved responses that don't have an HTTP Status Code",
6+
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
7+
},
8+
"item": [
9+
{
10+
"name": "Create new User",
11+
"request": {
12+
"method": "POST",
13+
"header": [],
14+
"body": {
15+
"mode": "raw",
16+
"raw": "{\n \"id\": \"100\",\n \"createdAt\": \"2021-06-04T15:50:38.568Z\",\n \"name\": \"Carol\",\n \"avatar\": \"https://cdn.fakercloud.com/avatars/nelsonjoyce_128.jpg\"\n }",
17+
"options": {
18+
"raw": {
19+
"language": "json"
20+
}
21+
}
22+
},
23+
"url": {
24+
"raw": "https://60bb37ab42e1d000176206c3.mockapi.io/api/v1/users",
25+
"protocol": "https",
26+
"host": ["60bb37ab42e1d000176206c3", "mockapi", "io"],
27+
"path": ["api", "v1", "users"]
28+
},
29+
"description": "Create a new user into your amazing API"
30+
},
31+
"response": [
32+
{
33+
"name": "Create new User example",
34+
"originalRequest": {
35+
"method": "POST",
36+
"header": [],
37+
"body": {
38+
"mode": "raw",
39+
"raw": "{\n \"id\": \"100\",\n \"createdAt\": \"2021-06-04T15:50:38.568Z\",\n \"name\": \"Carol\",\n \"avatar\": \"https://cdn.fakercloud.com/avatars/nelsonjoyce_128.jpg\"\n }",
40+
"options": {
41+
"raw": {
42+
"language": "json"
43+
}
44+
}
45+
},
46+
"url": {
47+
"raw": "https://60bb37ab42e1d000176206c3.mockapi.io/api/v1/users",
48+
"protocol": "https",
49+
"host": ["60bb37ab42e1d000176206c3", "mockapi", "io"],
50+
"path": ["api", "v1", "users"]
51+
}
52+
},
53+
"_postman_previewlanguage": "json",
54+
"header": [
55+
{
56+
"key": "Server",
57+
"value": "Cowboy"
58+
},
59+
{
60+
"key": "Connection",
61+
"value": "keep-alive"
62+
},
63+
{
64+
"key": "X-Powered-By",
65+
"value": "Express"
66+
},
67+
{
68+
"key": "Access-Control-Allow-Origin",
69+
"value": "*"
70+
},
71+
{
72+
"key": "Access-Control-Allow-Methods",
73+
"value": "GET,PUT,POST,DELETE,OPTIONS"
74+
},
75+
{
76+
"key": "Access-Control-Allow-Headers",
77+
"value": "X-Requested-With,Content-Type,Cache-Control,access_token"
78+
},
79+
{
80+
"key": "Content-Type",
81+
"value": "application/json"
82+
},
83+
{
84+
"key": "Content-Length",
85+
"value": "131"
86+
},
87+
{
88+
"key": "Vary",
89+
"value": "Accept-Encoding"
90+
},
91+
{
92+
"key": "Date",
93+
"value": "Sat, 05 Jun 2021 08:42:32 GMT"
94+
},
95+
{
96+
"key": "Via",
97+
"value": "1.1 vegur"
98+
}
99+
],
100+
"cookie": [],
101+
"body": "{\n \"id\": \"51\",\n \"createdAt\": \"2021-06-04T15:50:38.568Z\",\n \"name\": \"Carol\",\n \"avatar\": \"https://cdn.fakercloud.com/avatars/nelsonjoyce_128.jpg\"\n}"
102+
}
103+
]
104+
}
105+
],
106+
"event": [
107+
{
108+
"listen": "prerequest",
109+
"script": {
110+
"type": "text/javascript",
111+
"exec": [""]
112+
}
113+
},
114+
{
115+
"listen": "test",
116+
"script": {
117+
"type": "text/javascript",
118+
"exec": [""]
119+
}
120+
}
121+
],
122+
"variable": [
123+
{
124+
"key": "version",
125+
"value": "1.2.0"
126+
},
127+
{
128+
"key": "user_id",
129+
"value": "50"
130+
}
131+
]
132+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Responses
4+
description: Postman collection with saved responses that don't have an HTTP Status Code
5+
version: 1.2.0
6+
servers:
7+
- url: https://60bb37ab42e1d000176206c3.mockapi.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+
application/json:
18+
schema:
19+
type: object
20+
example:
21+
id: '100'
22+
createdAt: '2021-06-04T15:50:38.568Z'
23+
name: Carol
24+
avatar: https://cdn.fakercloud.com/avatars/nelsonjoyce_128.jpg
25+
responses:
26+
default:
27+
headers:
28+
Server:
29+
schema:
30+
type: string
31+
example: Cowboy
32+
Connection:
33+
schema:
34+
type: string
35+
example: keep-alive
36+
X-Powered-By:
37+
schema:
38+
type: string
39+
example: Express
40+
Access-Control-Allow-Origin:
41+
schema:
42+
type: string
43+
example: '*'
44+
Access-Control-Allow-Methods:
45+
schema:
46+
type: string
47+
example: GET,PUT,POST,DELETE,OPTIONS
48+
Access-Control-Allow-Headers:
49+
schema:
50+
type: string
51+
example: X-Requested-With,Content-Type,Cache-Control,access_token
52+
Content-Type:
53+
schema:
54+
type: string
55+
example: application/json
56+
Content-Length:
57+
schema:
58+
type: integer
59+
example: '131'
60+
Vary:
61+
schema:
62+
type: string
63+
example: Accept-Encoding
64+
Date:
65+
schema:
66+
type: string
67+
example: Sat, 05 Jun 2021 08:42:32 GMT
68+
Via:
69+
schema:
70+
type: string
71+
example: 1.1 vegur
72+
content:
73+
application/json:
74+
schema:
75+
type: object
76+
example:
77+
id: '51'
78+
createdAt: '2021-06-04T15:50:38.568Z'
79+
name: Carol
80+
avatar: https://cdn.fakercloud.com/avatars/nelsonjoyce_128.jpg

0 commit comments

Comments
 (0)