Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vikaskr0704 opened this issue Oct 20, 2023 · 1 comment

Comments

@vikaskr0704
Copy link

vikaskr0704 commented Oct 20, 2023

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
Copy link
Author

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); }

@vikaskr0704 vikaskr0704 changed the title JsonPatch.apply replacing array type field value when operation is ADD JsonPatch.apply replacing array type field value when operation is ADD instead of APPEND Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant