flipkart-incubator / zjsonpatch

This is an implementation of RFC 6902 JSON Patch written in Java
Apache License 2.0
523 stars 148 forks source link

JsonPatch.apply replacing array type field value when operation is ADD instead of APPEND #181

Open vikaskr0704 opened 10 months ago

vikaskr0704 commented 10 months ago

Expected Behavior

Op="ADD" should append an entry to array type field "attributes".

Original Json: { "id": "4c35b75c-6e1a-4bd2-8050-1137d485c76f", "name": "PatientPanelsType", "description": "This object contains the definition of patient panels", "attributes": [ { "name": "about", "type": "String", "editable": false, "maxLength": 40, "isRequired": true }, { "name": "contact", "type": "Object", "editable": false, "attributes": [ { "name": "phone", "type": "String", "editable": true, "maxLength": 20, "isRequired": true }, { "name": "email", "type": "String", "editable": true, "maxLength": 256 } ], "isRequired": true } ] }

Patch Json: { "op": "add", "path": "/attributes/1/attributes", "value": { "name": "objIdentifier", "type": "Object", "attributes": [ { "name": "identifierName", "type": "String", "maxLength": 256 }, { "name": "identifierValue", "type": "String", "maxLength": 256 } ] } }

Expected Patched Json: { "id": "4c35b75c-6e1a-4bd2-8050-1137d485c76f", "name": "PatientPanelsType", "description": "This object contains the definition of patient panels", "attributes": [ { "name": "about", "type": "String", "editable": false, "maxLength": 40, "isRequired": true }, { "name": "contact", "type": "Object", "editable": false, "attributes": [ { "name": "phone", "type": "String", "editable": true, "maxLength": 20, "isRequired": true }, { "name": "email", "type": "String", "editable": true, "maxLength": 256 }, { "name": "objIdentifier", "type": "Object", "attributes": [ { "name": "identifierName", "type": "String", "maxLength": 256 }, { "name": "identifierValue", "type": "String", "maxLength": 256 } ] } ], "isRequired": true } ] }

Actual Behavior

Op="ADD" is replacing array type field "attributes" value with op ADD value. Output of JsonPatch.apply(patch, originalJson) is below: { "id": "4c35b75c-6e1a-4bd2-8050-1137d485c76f", "name": "PatientPanelsType", "description": "This object contains the definition of patient panels", "attributes": [ { "name": "about", "type": "String", "editable": false, "maxLength": 40, "isRequired": true }, { "name": "contact", "type": "Object", "editable": false, "attributes": { "name": "objIdentifier", "type": "Object", "attributes": [ { "name": "identifierName", "type": "String", "maxLength": 256 }, { "name": "identifierValue", "type": "String", "maxLength": 256 } ] }, "isRequired": true } ] }

Steps to Reproduce the Problem

origialJsonNode = objectMapper.valueToTree( /Original Json from Expected behavior section/) patchJsonNode = objectMapper.valueToTree(/Patch Json from Expected behavior section/)

JsonNode applyPatch = JsonPatch.apply(patchJsonNode, origialJsonNode);

Specifications

Library Version: 0.4.12 and 0.4.14

Language: Java 17.1

vikaskr0704 commented 10 months ago

Upon debugging, I found that issue is in InPlaceApplyProcessor.addToObject() API where it tries to set attributes field in parent contact Json instead of adding to existing array.

private void addToObject(JsonPointer path, JsonNode node, JsonNode value) { ObjectNode target = (ObjectNode)node; String key = path.last().getField(); target.set(key, value); }