Json Patch inconsistent behavior
sasiharan opened this issue · 1 comments
Hi,
I'm trying to apply the below patch to the " old.json " [given at the end of the issue] and the resulting json was stored in " new.json "
Patch:
[{
"op": "add",
"path": "/fields/3",
"value": {
"name": "businessDay",
"type": ["null", "int"],
"default": null
}
}, {
"op": "add",
"path": "/fields/4/type/fields/0/type/1/items/fields/-",
"value": {
"name": "paymentDescription",
"type": ["null", "string"],
"default": null
}
}]
Now when I try to create a JsonPatch between " old.json " and " new.json ", I get a completely different jsonPatch given below. I was expecting the same JsonPatch which I applied initially.
JsonPatch:
[
{
"op" : "add",
"path" : "/fields/3/default",
"value" : null
},
{
"op" : "remove",
"path" : "/fields/3/doc",
"old" : "test"
},
{
"op" : "replace",
"path" : "/fields/3/name",
"value" : "businessDay",
"old" : "PaymentsList"
},
{
"op" : "replace",
"path" : "/fields/3/type",
"value" : [
"null",
"int"
],
"old" : {
"doc" : "test",
"type" : "record",
"name" : "PaymentsListInvoice",
"fields" : [
{
"name" : "Payments",
"doc" : "test",
"type" : [
"null",
{
"doc" : "test",
"type" : "array",
"items" : {
"doc" : "test",
"type" : "record",
"name" : "RPaymentsListInvoice",
"fields" : [
{
"name" : "SequenceNumber",
"doc" : "test",
"type" : "int"
},
{
"name" : "StoreID",
"doc" : "test",
"type" : "int"
}
]
}
}
],
"default" : null
}
]
}
},
{
"op" : "add",
"path" : "/fields/-",
"value" : {
"name" : "PaymentsList",
"doc" : "test",
"type" : {
"doc" : "test",
"type" : "record",
"name" : "PaymentsListInvoice",
"fields" : [
{
"name" : "Payments",
"doc" : "test",
"type" : [
"null",
{
"doc" : "test",
"type" : "array",
"items" : {
"doc" : "test",
"type" : "record",
"name" : "RPaymentsListInvoice",
"fields" : [
{
"name" : "SequenceNumber",
"doc" : "test",
"type" : "int"
},
{
"name" : "StoreID",
"doc" : "test",
"type" : "int"
},
{
"name" : "paymentDescription",
"type" : [
"null",
"string"
],
"default" : null
}
]
}
}
],
"default" : null
}
]
}
}
}
]
Could you let me know why there is inconsistency in creating the jsonDiff patch?
Thanks.
old.json:
{
"doc": "test",
"type": "record",
"name": "Invoice",
"fields": [
{
"name": "SequenceNumber",
"doc": "test",
"type": "int"
},
{
"name": "StoreID",
"doc": "test",
"type": "int"
},
{
"name": "ReferenceID",
"type": [
"null",
"string"
],
"default": null
},
{
"name": "PaymentsList",
"doc": "test",
"type": {
"doc": "test",
"type": "record",
"name": "PaymentsListInvoice",
"fields": [
{
"name": "Payments",
"doc": "test",
"type": [
"null",
{
"doc": "test",
"type": "array",
"items": {
"doc": "test",
"type": "record",
"name": "RPaymentsListInvoice",
"fields": [
{
"name": "SequenceNumber",
"doc": "test",
"type": "int"
},
{
"name": "StoreID",
"doc": "test",
"type": "int"
}
]
}
}
],
"default": null
}
]
}
}
]
}
new.json:
{
"doc": "test",
"type": "record",
"name": "Invoice",
"fields": [
{
"name": "SequenceNumber",
"doc": "test",
"type": "int"
},
{
"name": "StoreID",
"doc": "test",
"type": "int"
},
{
"name": "ReferenceID",
"type": [
"null",
"string"
],
"default": null
},
{
"name": "businessDay",
"type": [
"null",
"int"
],
"default": null
},
{
"name": "PaymentsList",
"doc": "test",
"type": {
"doc": "test",
"type": "record",
"name": "PaymentsListInvoice",
"fields": [
{
"name": "Payments",
"doc": "test",
"type": [
"null",
{
"doc": "test",
"type": "array",
"items": {
"doc": "test",
"type": "record",
"name": "RPaymentsListInvoice",
"fields": [
{
"name": "SequenceNumber",
"doc": "test",
"type": "int"
},
{
"name": "StoreID",
"doc": "test",
"type": "int"
},
{
"name": "paymentDescription",
"type": [
"null",
"string"
],
"default": null
}
]
}
}
],
"default": null
}
]
}
}
]
}
Sorry for replying so late, but for sake of completeness here is the answer. The behavior is not broken. If you apply the generated diff to old.json
, you get the same result as when applying your original patch. Patches are not unique, and diffson does not generate patches with any guarantee of being optimal (for any definition of optimal). The correctness of the patch is tested by checking that applying it gives the expected result. And it is the case here, you can try, the resulting JSON has the exact same structure and values as new.json
.