diff --git a/packages/api-explorer/__tests__/lib/remove-undefined-objects.test.js b/packages/api-explorer/__tests__/lib/remove-undefined-objects.test.js index 5c6229084..df3c48592 100644 --- a/packages/api-explorer/__tests__/lib/remove-undefined-objects.test.js +++ b/packages/api-explorer/__tests__/lib/remove-undefined-objects.test.js @@ -7,3 +7,7 @@ test('should remove empty objects with only undefined properties', () => { test('should not throw on arrays of primitives', () => { expect(removeUndefinedObjects([null])).toEqual([null]); }); + +test('should not throw for null', () => { + expect(removeUndefinedObjects({ a: null })).toEqual({ a: null }); +}); diff --git a/packages/api-explorer/src/lib/remove-undefined-objects.js b/packages/api-explorer/src/lib/remove-undefined-objects.js index bc05fbcd8..e32fc293a 100644 --- a/packages/api-explorer/src/lib/remove-undefined-objects.js +++ b/packages/api-explorer/src/lib/remove-undefined-objects.js @@ -7,7 +7,7 @@ function isEmptyObject(obj) { function stripEmptyObjects(obj) { Object.keys(obj).forEach(key => { const value = obj[key]; - if (typeof value === 'object' && !Array.isArray(obj)) { + if (typeof value === 'object' && !Array.isArray(obj) && value !== null) { // Recurse, strip out empty objects from children stripEmptyObjects(value); // Then remove all empty objects from the top level object