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

Issue with applying diff with CompatibilityFlags.ALLOW_MISSING_TARGET_OBJECT_ON_REPLACE #163

Open tanmayvijay opened 1 year ago

tanmayvijay commented 1 year ago

When we have a diff specifying a replace operation on a path and one of the tokens in the path is not present in the target json, we get an error: [REPLACE operation] Missing field "path_token" at /path_token_x/path_token_y.

This case is handled in replace operation with CompatibilityFlags.ALLOW_MISSING_TARGET_OBJECT_ON_REPLACE. Problem is, every operation runs an evaluate() call on target json, which checks if all the tokens from root to the parent of token to be replaced are present or not, and if any of them is missing we get this error. So, this compatibility check effectively works only if the target field is missing and not if some ancestor token is missing.

This applies to all operations.

Is this done on purpose? If yes, why can't we have a compatibility check for all tokens on the path?

Can we have a fix for this? I can create a PR if this is not being already worked on.

Expected Behaviour

Have a compatibility flag to allow for missing tokens on entire path.

Actual Behaviour

Currently, we only allow target values to be missing and if any token on the path is missing, we fail with above error. Not just in replace operation, but in all operations.

Steps to Reproduce the Problem

Try applying given diff on the given target Json:

diff: [{"op":"replace","path":"/t1/t2/t3/t4","value":"new_value"}]

target: { "t1": { "t2": { } } }

Error expected: [REPLACE Operation] Missing field "t3" at /t1/t2