From 95727cc669044448f3df17bef129e1ea1c0cd0eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albin=20M=C3=B6rner?= Date: Fri, 23 Nov 2018 11:28:44 +0100 Subject: [PATCH] Return false for non-empty objects (#143) * Return false for non-empty objects * Check that properties are empty. Rather than the object itself. * Refactor hasEmptyProperty. Add test * Fix minor typo --- lib/helpers/getSpecificationObject.js | 16 ++++------------ lib/helpers/hasEmptyProperty.js | 14 ++++++++++++++ test/helpers/specification-helpers.js | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 lib/helpers/hasEmptyProperty.js diff --git a/lib/helpers/getSpecificationObject.js b/lib/helpers/getSpecificationObject.js index aa5906b1..75c8bb96 100644 --- a/lib/helpers/getSpecificationObject.js +++ b/lib/helpers/getSpecificationObject.js @@ -4,15 +4,7 @@ const specHelper = require('./specification'); const parseApiFile = require('./parseApiFile'); const filterJsDocComments = require('./filterJsDocComments'); const convertGlobPaths = require('./convertGlobPaths'); - -function isEmptyObject(obj) { - // eslint-disable-next-line - Object.keys(obj).forEach(key => { - if (key in obj) return false; - }); - - return true; -} +const hasEmptyProperty = require('./hasEmptyProperty'); /** * OpenAPI specification validator does not accept empty values for a few properties. @@ -30,9 +22,9 @@ function cleanUselessProperties(inputSpec) { 'securityDefinitions', ]; - toClean.forEach(unncessaryProp => { - if (isEmptyObject(improvedSpec[unncessaryProp])) { - delete improvedSpec[unncessaryProp]; + toClean.forEach(unnecessaryProp => { + if (hasEmptyProperty(improvedSpec[unnecessaryProp])) { + delete improvedSpec[unnecessaryProp]; } }); diff --git a/lib/helpers/hasEmptyProperty.js b/lib/helpers/hasEmptyProperty.js new file mode 100644 index 00000000..a826d5b3 --- /dev/null +++ b/lib/helpers/hasEmptyProperty.js @@ -0,0 +1,14 @@ +/** + * Checks if there is any properties of @obj that is a empty object + * @function + * @param {object} obj - the object to check + */ +function hasEmptyProperty(obj) { + return Object.values(obj).every( + keyObject => + typeof keyObject === 'object' && + Object.keys(keyObject).every(key => !(key in keyObject)) + ); +} + +module.exports = hasEmptyProperty; diff --git a/test/helpers/specification-helpers.js b/test/helpers/specification-helpers.js index e4d132d3..1d5412aa 100644 --- a/test/helpers/specification-helpers.js +++ b/test/helpers/specification-helpers.js @@ -4,6 +4,7 @@ // Dependencies. const chai = require('chai'); const specHelper = require('../../lib/helpers/specification'); +const hasEmptyProperty = require('../../lib/helpers/hasEmptyProperty'); const { expect } = chai; const swaggerObject = require('../fixtures/v2/swaggerObject.json'); @@ -77,4 +78,19 @@ describe('swagger-helpers submodule', () => { expect(testObject.responses.api).to.include.keys(['foo', 'bar']); done(); }); + + it('hasEmptyProperty() identifies object with an empty object or array as property', done => { + const invalidA = { foo: {} }; + const invalidB = { foo: [] }; + const validA = { foo: { bar: 'baz' } }; + const validB = { foo: ['¯_(ツ)_/¯'] }; + const validC = { foo: '¯_(ツ)_/¯' }; + + expect(hasEmptyProperty(invalidA)).equal(true); + expect(hasEmptyProperty(invalidB)).equal(true); + expect(hasEmptyProperty(validA)).equal(false); + expect(hasEmptyProperty(validB)).equal(false); + expect(hasEmptyProperty(validC)).equal(false); + done(); + }); });