cyvive / agnostic

Software Agnostic Development Accelerator
Mozilla Public License 2.0
1 stars 2 forks source link

fast-json-patch fails in test when working replacing output with Array object #11

Closed bhatnagarm closed 5 years ago

bhatnagarm commented 5 years ago

When we put a array of json objects as the _out object the 'fast-json-patch' does not generate a valid Patch object. It just returns blank object. This issue has been patched in json-patch (reference #https://github.com/Starcounter-Jack/JSON-Patch/issues/147). But I think it has not been updated in the module fast-json-patch. Looking at the reason why this fails in fast-json-patch .

const R = require('ramda') _out = rfdc(R.unionWith(R.eqBy(R.prop('flightNumber') || R.prop('estimatedTimeDeparture')), JSON.parse(crewDB), JSON.parse(airCrew)))

await t.test('merge_data_from_systems', async t => { let data = {request : {} , crewDB: "[{\"flightCheckinId\": 1, \"timestamp\": \"2019-01-31T06:01:10.043Z\", \"airlineCode\": \"VA\", \"flightNumber\": \"007\", \"departurePort\": \"BNE\", \"arrivalPort\": \"LAX\", \"estimatedTimeDeparture\": \"2019-01-29T00:00:00.000Z\",\"estimatedTimeArrival\": \"2019-01-29T00:00:00.000Z\", \"outboundStatus\": \"SUCCESS\", \"inboundStatus\": \"SUCCESS\"}]" , airCrew: "[{\"airlineCode\": \"VA\",\"flightNumber\": \"0123\",\"departurePort\": \"BNE\", \"arrivalPort\": \"MEL\",\"estimatedTimeDeparture\": \"2019-02-04T05:03:37.000Z\",\"estimatedTimeArrival\": \"2019-02-04T05:03:37.000Z\"}]" , _cache : {}, _out : {}} // Fast JSon Patch do not support Javascript Array Objects. //https://github.com/Starcounter-Jack/JSON-Patch/issues/147 const dataWatcher = jsonPatch.observe(data) console.log(data._out); data = await o.__get__('merge_data_from_systems')(data) const delta = jsonPatch.generate(dataWatcher) console.log(delta); console.log(data._out); t.deepEqual( delta.shift(), {op: 'add', path: '/0/flightNumber', value:'007' }, 'Flights are merged as expected' ) t.equal(delta.length, 0, 'all delta records checked') })

Returns [] for delta

Issue is at this location https://github.com/martindale/fast-json-patch/blob/master/src/json-patch-duplex.js

function _generate(mirror, obj, patches, path) { var newKeys = _objectKeys(obj); var oldKeys = _objectKeys(mirror);

The code is looking for _objectKeys recursively. This causes a issue as the recursion misses a array of objects.

bhatnagarm commented 5 years ago

Correct code in Json-patch if (hasOwnProperty(obj, key) && !(obj[key] === undefined && oldVal !== undefined && Array.isArray(obj) === false)) { var newVal = obj[key]; if (typeof oldVal == "object" && oldVal != null && typeof newVal == "object" && newVal != null) { _generate(oldVal, newVal, patches, path + "/" + escapePathComponent(key)); }

keidrych commented 5 years ago

fast-json-patch replaced with rfc6902

Its a more stable library moving forward, and doesn't depend on depreciated Object.Observe. Also uses the 'test' part of the spec