caohanyang / json_diff_rfc6902

This is the framework to handle JSON diff and patch based on RFC6902
GNU Lesser General Public License v3.0
20 stars 1 forks source link

crash when property is undefined #2

Closed Bonuspunkt closed 6 years ago

Bonuspunkt commented 7 years ago
const { diff } = require('json-diff-rfc6902');

const before = {
    a: 'text',
}
const after = {
    a: 'text',
    b: undefined
};

diff(before, after);

crashes with

.\node_modules\json-diff-rfc6902\lib\unchangedArea.js:71
    if (newValue.toString() === value) {
                ^

TypeError: Cannot read property 'toString' of undefined
    at Object.findValueInUnchanged (.\node_modules\json-diff-rfc6902\lib\unchangedArea.js:71:17)
    at generateObjectDiff (.\node_modules\json-diff-rfc6902\JSON-Diff.js:127:35)
    at generateDiff (.\node_modules\json-diff-rfc6902\JSON-Diff.js:51:5)
    at diff (.\node_modules\json-diff-rfc6902\JSON-Diff.js:33:3)
caohanyang commented 7 years ago

Thanks! I will fix it later.

nlac commented 7 years ago

Not sure it is really an issue. Keep in mind that the value undefined is an illegal JSON node, see here and here so don't expect any diff for that input - dropping an exception is reasonable, however the library may drop some more friendly one saying "invalid input" or stg like that.

caohanyang commented 6 years ago

Undefined is not a valid json value, even though it is valid in javascript. From the official json standard (ECMA-404, Section 5): A JSON value can be an object, array, number, string, true, false, or null.

I will give some friendly output in the next version.

Thanks!