Skip to content

Commit a25b7a6

Browse files
committed
Fixed certain TypeErrors causing process to fail unexpectedly
1 parent 95cc99c commit a25b7a6

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

lib/31XUtils/schemaUtils31X.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ module.exports = {
125125
schema.items = this.fixExamplesByVersion(schema.items);
126126
}
127127
else if (hasProperties) {
128-
const schemaProperties = Object.keys(schema.properties);
128+
const schemaProperties = _.keys(schema.properties);
129129
schemaProperties.forEach((property) => {
130130
schema.properties[property] = this.fixExamplesByVersion(schema.properties[property]);
131131
});
@@ -141,7 +141,7 @@ module.exports = {
141141
* @returns {boolean} Returns true if content is a binary type
142142
*/
143143
isBinaryContentType (bodyType, contentObj) {
144-
return Object.keys(contentObj[bodyType]).length === 0 && fileUploadTypes.includes(bodyType);
144+
return _.keys(contentObj[bodyType]).length === 0 && fileUploadTypes.includes(bodyType);
145145
},
146146

147147
getOuterPropsIfIsSupported(schema) {
@@ -152,7 +152,7 @@ module.exports = {
152152

153153
addOuterPropsToRefSchemaIfIsSupported(refSchema, outerProps) {
154154
const resolvedSchema = _.cloneDeep(refSchema),
155-
outerKeys = Object.keys(outerProps);
155+
outerKeys = _.keys(outerProps);
156156

157157
if (_.isObject(resolvedSchema) && _.isObject(outerProps)) {
158158
outerKeys.forEach((key) => {

lib/schemaUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2797,7 +2797,7 @@ module.exports = {
27972797
// also, any endpoint-level params are merged into the returned pathItemObject
27982798
findMatchingRequestFromSchema: function (method, url, schema, options) {
27992799
// first step - get array of requests from schema
2800-
let parsedUrl = require('url').parse(url),
2800+
let parsedUrl = require('url').parse(_.isString(url) ? url : ''),
28012801
retVal = [],
28022802
pathToMatch,
28032803
matchedPath,

test/unit/31Xsupport/schemaUtils31X.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,26 @@ describe('fixExamplesByVersion method', function() {
624624
expect(JSON.stringify(fixedSchemaWithExample)).to.be.equal(JSON.stringify(expectedSchemaAfterFix));
625625
});
626626

627+
it('Should correctly handle schema with properties defined as null', function() {
628+
const providedSchema = {
629+
required: [
630+
'id',
631+
'name'
632+
],
633+
type: 'object',
634+
properties: null
635+
},
636+
expectedSchemaAfterFix = {
637+
required: [
638+
'id',
639+
'name'
640+
],
641+
type: 'object',
642+
properties: null
643+
},
644+
fixedSchemaWithExample = concreteUtils.fixExamplesByVersion(providedSchema);
645+
expect(JSON.stringify(fixedSchemaWithExample)).to.be.equal(JSON.stringify(expectedSchemaAfterFix));
646+
});
627647
});
628648

629649
describe('isBinaryContentType method', function() {
@@ -651,6 +671,15 @@ describe('isBinaryContentType method', function() {
651671
isBinary = concreteUtils.isBinaryContentType(bodyType, contentObject);
652672
expect(isBinary).to.be.false;
653673
});
674+
675+
it('Should correctly handle null value for corresponding body', function() {
676+
const bodyType = 'application/octet-stream',
677+
contentObject = {
678+
'application/octet-stream': null
679+
},
680+
isBinary = concreteUtils.isBinaryContentType(bodyType, contentObject);
681+
expect(isBinary).to.be.true;
682+
});
654683
});
655684

656685
describe('getOuterPropsIfIsSupported method', function() {
@@ -704,6 +733,22 @@ describe('getOuterPropsIfIsSupported method', function() {
704733
expect(resolvedSchema.job).to.be.equal(outerProperties.job);
705734
expect(JSON.stringify(resolvedSchema.required)).to.be.equal(JSON.stringify(expectedRequiredValue));
706735
});
736+
737+
it('Should correctly handle if outer properties are defined as null', function() {
738+
const referencedSchema = {
739+
name: 'Test name',
740+
age: '30',
741+
required: [
742+
'name'
743+
]
744+
},
745+
outerProperties = null,
746+
expectedRequiredValue = ['name'],
747+
resolvedSchema = concreteUtils.addOuterPropsToRefSchemaIfIsSupported(referencedSchema, outerProperties);
748+
expect(resolvedSchema).to.be.an('object')
749+
.nested.to.have.all.keys('name', 'age', 'required');
750+
expect(JSON.stringify(resolvedSchema.required)).to.be.equal(JSON.stringify(expectedRequiredValue));
751+
});
707752
});
708753

709754
describe('findTypeByExample method', function () {

test/unit/validator.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,23 @@ describe('VALIDATE FUNCTION TESTS ', function () {
14041404
expect(result[1].name).to.eql('GET /lookups');
14051405
done();
14061406
});
1407+
1408+
it('should correctly handle non string URLs', function (done) {
1409+
let schema = {
1410+
paths: {
1411+
'/lookups': {
1412+
'get': { 'summary': 'Lookup Job Values' }
1413+
}
1414+
}
1415+
},
1416+
schemaPath = null,
1417+
result;
1418+
1419+
result = schemaUtils.findMatchingRequestFromSchema('GET', schemaPath, schema, { strictRequestMatching: true });
1420+
1421+
expect(result).to.have.lengthOf(0);
1422+
done();
1423+
});
14071424
});
14081425
});
14091426

0 commit comments

Comments
 (0)