java-json-tools / json-patch

An RFC 6902 (JSON Patch) and reverse, plus RFC 7386 (JSON Merge Patch), implementation in Java using Jackson (2.2.x)
Other
645 stars 186 forks source link

Json Patch apply method is throwing JsonPatchException even if one property name is different #111

Open anandhkishan opened 3 years ago

anandhkishan commented 3 years ago

First of all great library support for JSON transformation 😃 👏 Was easy and intuitive on usage of it. One thing that I noticed is when applying patch to the actual JSON -> if some of the attributes in actual JSON is not present in the operations, we are getting JsonPatchException ref: com.github.fge.jsonpatch.JsonPatchOperation#apply com.github.fge.jsonpatch.JsonPatch#apply

For instance, I tried the following code snippet

JsonNode payloadNode = JsonLoader.fromString(payload);
JsonPatch jsonPatch = JsonPatch.fromJson(JsonLoader.fromString(specJson));
JsonNode transformedNode = jsonPatch.apply(payloadNode);

Here the specJson was:

{ "op": "move", "from": "/account_id", "path": "/app_id" }

And the payload was: { "tenant_id":2637,"account_id": 1231232123423}

Since payload has unknown property (tenant_id), this was throwing JsonPatchException exception. The ask is what if there are some properties which needs no JsonPatch and can stay as it is?

Can we have another implementation which would suppress these exceptions that could happen when there are unknown attributes present in the actual JSON?

anandhkishan commented 3 years ago

@fge If you could confirm that this new implementation can exist, I could start working on this and contribute to this change. Thanks!