From 86216f3ee05dd11475c991ada3494cf40c89ce20 Mon Sep 17 00:00:00 2001 From: Shilpa Kundapur Date: Wed, 22 Jul 2020 19:19:26 +0530 Subject: [PATCH] Deserializer - Fix deserializer when data is null --- lib/deserializer.js | 2 ++ test/deserializer.js | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/deserializer.js b/lib/deserializer.js index 1cb7c1a..a117ff3 100644 --- a/lib/deserializer.js +++ b/lib/deserializer.js @@ -21,6 +21,8 @@ module.exports = function (opts) { } function resource() { + if (!jsonapi.data) { jsonapi.data = {}; } + return new DeserializerUtils(jsonapi, jsonapi.data, opts) .perform() .then(function (result) { diff --git a/test/deserializer.js b/test/deserializer.js index bd5ae42..0baab6c 100644 --- a/test/deserializer.js +++ b/test/deserializer.js @@ -1638,4 +1638,22 @@ describe('JSON API Deserializer', function () { }); }); + + describe('when data is null', function () { + it('should return an empty object', function (done) { + var dataSet = { + data: null, + jsonapi: { + version: '1.0' + } + }; + + new JSONAPIDeserializer() + .deserialize(dataSet, function (err, json) { + expect(json).to.be.eql({}); + + done(null, json); + }); + }); + }); });