Skip to content

Commit 81f6b5d

Browse files
author
Dhwaneet Bhatt
authored
Merge pull request #704 from postmanlabs/feature/fix-typerror-null-params
Fixed issue where TypeErrors were happening for null/undefined params.
2 parents 6ebfe98 + 0d9af9f commit 81f6b5d

File tree

5 files changed

+156
-5
lines changed

5 files changed

+156
-5
lines changed

lib/schemaUtils.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ module.exports = {
696696
operationItem.parameters = this.getRequestParams(operationItem.parameters, commonParams,
697697
specComponentsAndUtils, options);
698698
summary = operationItem.summary || operationItem.description;
699-
currentNode.addMethod({
699+
_.isFunction(currentNode.addMethod) && currentNode.addMethod({
700700
name: summary,
701701
method: method,
702702
path: path,
@@ -1480,6 +1480,10 @@ module.exports = {
14801480
includeDeprecated = options.includeDeprecated !== false;
14811481

14821482
_.forEach(localParams, (param) => {
1483+
if (!_.isObject(param)) {
1484+
return;
1485+
}
1486+
14831487
tempParam = param;
14841488
let verifyAddDeprecated = (includeDeprecated ||
14851489
includeDeprecated === false && tempParam.deprecated !== true);
@@ -2182,6 +2186,10 @@ module.exports = {
21822186
return null;
21832187
}
21842188
_.forOwn(response.headers, (value, key) => {
2189+
if (!_.isObject(value)) {
2190+
return;
2191+
}
2192+
21852193
if (_.toLower(key) !== 'content-type') {
21862194
if (_.get(value, '$ref')) {
21872195
// the convert to PmHeader function handles the

libV2/schemaUtils.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ let QUERYPARAM = 'query',
227227
_.forOwn(serverObj.variables, (value, key) => {
228228
serverVariables.push({
229229
key,
230-
value: value.default || ''
230+
value: _.get(value, 'default') || ''
231231
});
232232
});
233233

@@ -1150,6 +1150,10 @@ let QUERYPARAM = 'query',
11501150
// TODO: This could have properties inside properties which needs to be handled
11511151
// That's why for some properties we are not deleting the format
11521152
_.forOwn(requestBodySchema.properties, (schema, prop) => {
1153+
if (!_.isObject(requestBodySchema.properties[prop])) {
1154+
return;
1155+
}
1156+
11531157
if (
11541158
requestBodySchema.properties[prop].format === 'binary' ||
11551159
requestBodySchema.properties[prop].format === 'byte' ||
@@ -1466,6 +1470,10 @@ let QUERYPARAM = 'query',
14661470
{ includeDeprecated } = context.computedOptions;
14671471

14681472
_.forEach(params, (param) => {
1473+
if (!_.isObject(param)) {
1474+
return;
1475+
}
1476+
14691477
if (_.has(param, '$ref')) {
14701478
param = resolveSchema(context, param);
14711479
}
@@ -1497,6 +1505,10 @@ let QUERYPARAM = 'query',
14971505
pmParams = [];
14981506

14991507
_.forEach(params, (param) => {
1508+
if (!_.isObject(param)) {
1509+
return;
1510+
}
1511+
15001512
if (_.has(param, '$ref')) {
15011513
param = resolveSchema(context, param);
15021514
}
@@ -1557,6 +1569,10 @@ let QUERYPARAM = 'query',
15571569
{ keepImplicitHeaders, includeDeprecated } = context.computedOptions;
15581570

15591571
_.forEach(params, (param) => {
1572+
if (!_.isObject(param)) {
1573+
return;
1574+
}
1575+
15601576
if (_.has(param, '$ref')) {
15611577
param = resolveSchema(context, param);
15621578
}
@@ -1646,6 +1662,10 @@ let QUERYPARAM = 'query',
16461662
}
16471663

16481664
_.forOwn(responseHeaders, (value, headerName) => {
1665+
if (!_.isObject(value)) {
1666+
return;
1667+
}
1668+
16491669
if (!includeDeprecated && value.deprecated) {
16501670
return;
16511671
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
paths:
10+
/pets/{petid}:
11+
get:
12+
summary: List all pets
13+
operationId: listPets
14+
tags:
15+
- pets
16+
parameters:
17+
- name: petid
18+
in: path
19+
required: true
20+
schema:
21+
type: string
22+
- name: limit
23+
in: query
24+
description: How many items to return at one time (max 100)
25+
required: false
26+
schema:
27+
type: integer
28+
format: int32
29+
-
30+
responses:
31+
'200':
32+
description: An paged array of pets
33+
headers:
34+
x-next: null
35+
content:
36+
application/json:
37+
schema:
38+
$ref: "#/components/schemas/Pets"
39+
default:
40+
description: unexpected error
41+
content:
42+
application/json:
43+
schema:
44+
$ref: "#/components/schemas/Error"
45+
components:
46+
schemas:
47+
Pet:
48+
required:
49+
- id
50+
- name
51+
properties:
52+
id:
53+
type: integer
54+
format: int64
55+
name:
56+
type: string
57+
tag:
58+
type: string
59+
Pets:
60+
type: array
61+
items:
62+
$ref: "#/components/schemas/Pet"
63+
Error:
64+
required:
65+
- code
66+
- message
67+
properties:
68+
code:
69+
type: integer
70+
format: int32
71+
message:
72+
type: string

test/unit/base.test.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ describe('CONVERT FUNCTION TESTS ', function() {
8888
xmlRequestAndResponseBodyArrayTypeWrapped =
8989
path.join(__dirname, VALID_OPENAPI_PATH, '/xmlRequestAndResponseBodyArrayTypeWrapped.json'),
9090
schemaWithAdditionalProperties =
91-
path.join(__dirname, VALID_OPENAPI_PATH, '/schemaWithAdditionalProperties.yaml');
91+
path.join(__dirname, VALID_OPENAPI_PATH, '/schemaWithAdditionalProperties.yaml'),
92+
specWithNullParams =
93+
path.join(__dirname, VALID_OPENAPI_PATH, '/specWithNullParams.yaml');
9294

9395

9496
it('Should add collection level auth with type as `bearer`' +
@@ -1822,6 +1824,30 @@ describe('CONVERT FUNCTION TESTS ', function() {
18221824
done();
18231825
});
18241826
});
1827+
1828+
it('The converter should not throw error for definition with undefined/null params and response headers',
1829+
function(done) {
1830+
var openapi = fs.readFileSync(specWithNullParams, 'utf8');
1831+
Converter.convert({ type: 'string', data: openapi }, {},
1832+
(err, conversionResult) => {
1833+
expect(err).to.be.null;
1834+
expect(conversionResult.result).to.equal(true);
1835+
expect(conversionResult.output.length).to.equal(1);
1836+
expect(conversionResult.output[0].type).to.equal('collection');
1837+
expect(conversionResult.output[0].data).to.have.property('info');
1838+
expect(conversionResult.output[0].data).to.have.property('item');
1839+
expect(conversionResult.output[0].data.item.length).to.equal(1);
1840+
1841+
const item = conversionResult.output[0].data.item[0];
1842+
1843+
expect(item.request.url.query.length).to.eql(1);
1844+
expect(item.request.url.variable.length).to.eql(1);
1845+
expect(item.request.header.length).to.eql(1);
1846+
1847+
expect(item.response[0].header.length).to.eql(1);
1848+
done();
1849+
});
1850+
});
18251851
});
18261852

18271853
describe('Converting swagger 2.0 files', function() {

test/unit/convertV2.test.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ const expect = require('chai').expect,
8383
schemaWithAdditionalProperties =
8484
path.join(__dirname, VALID_OPENAPI_PATH, '/schemaWithAdditionalProperties.yaml'),
8585
specWithResponseRef =
86-
path.join(__dirname, VALID_OPENAPI_PATH, '/specWithResponseRef.yaml');
86+
path.join(__dirname, VALID_OPENAPI_PATH, '/specWithResponseRef.yaml'),
87+
specWithNullParams =
88+
path.join(__dirname, VALID_OPENAPI_PATH, '/specWithNullParams.yaml');
8789

8890

8991
describe('The convert v2 Function', function() {
@@ -733,7 +735,7 @@ describe('The convert v2 Function', function() {
733735
});
734736
});
735737

736-
it('[Github #137]- Should add `requried` keyword in parameters where ' +
738+
it('[Github #137]- Should add `required` keyword in parameters where ' +
737739
'required field is set to true', function(done) {
738740
Converter.convertV2({ type: 'file', data: requiredInParams }, { schemaFaker: true }, (err, conversionResult) => {
739741
expect(err).to.be.null;
@@ -2106,4 +2108,27 @@ describe('The convert v2 Function', function() {
21062108
done();
21072109
});
21082110
});
2111+
2112+
it('Should convert a collection with undefined/null params and response headers without error', function(done) {
2113+
var openapi = fs.readFileSync(specWithNullParams, 'utf8');
2114+
Converter.convertV2({ type: 'string', data: openapi }, {},
2115+
(err, conversionResult) => {
2116+
expect(err).to.be.null;
2117+
expect(conversionResult.result).to.equal(true);
2118+
expect(conversionResult.output.length).to.equal(1);
2119+
expect(conversionResult.output[0].type).to.equal('collection');
2120+
expect(conversionResult.output[0].data).to.have.property('info');
2121+
expect(conversionResult.output[0].data).to.have.property('item');
2122+
expect(conversionResult.output[0].data.item.length).to.equal(1);
2123+
2124+
const item = conversionResult.output[0].data.item[0].item[0].item[0];
2125+
2126+
expect(item.request.url.query.length).to.eql(1);
2127+
expect(item.request.url.variable.length).to.eql(1);
2128+
expect(item.request.header.length).to.eql(1);
2129+
2130+
expect(item.response[0].header.length).to.eql(1);
2131+
done();
2132+
});
2133+
});
21092134
});

0 commit comments

Comments
 (0)