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

Patch with context (created with ADD_ORIGINAL_VALUE_ON_REPLACE) must fail on value mismatch #91

Closed
Hubbitus opened this issue Feb 3, 2019 · 8 comments

Comments

@Hubbitus
Copy link

Hubbitus commented Feb 3, 2019

You have useful com.flipkart.zjsonpatch.DiffFlags.ADD_ORIGINAL_VALUE_ON_REPLACE flag on patch creation. But such context fully ignored on apply time!

Please look at test:

	def "Test create patch with context, and fail on apply when original object changed"(){
		when:
			JsonNode source = toJson '{"k1":"v1","k2":"v2"}'
			JsonNode target = toJson '{"k1":"v1","k2": {"inner": 1}}'

			JsonNode patch = asJson(source, target, EnumSet.of(ADD_ORIGINAL_VALUE_ON_REPLACE))
		then:
			patch as String == '[{"op":"replace","fromValue":"v2","path":"/k2","value":{"inner":1}}]'

		when:
			source = toJson '{"k1":"v1","k2":"v3 changed"}' // Our external changes in source object (configuration in production by customer)
			JsonNode targetRecreated = apply(patch, source)

		then:
//			JsonPatchApplicationException e = thrown(JsonPatchApplicationException) // I want exception there, but it succeed!
			targetRecreated as String == '{"k1":"v1","k2":{"inner":1}}'
	}

Now it succeed, but I want mode when it failed with JsonPatchApplicationException and message like "Can't apply patch on path [/k2] because it have unexpected value [v3 changed]. Expected value tracked in patch [v2]".

I think there should be added corresponding value for that like com.flipkart.zjsonpatch.CompatibilityFlags#CHECK_ORIGINAL_VALUE.

Are you willing I provide PR for such fix?

Hubbitus added a commit to Hubbitus/json-diff-test that referenced this issue Feb 3, 2019
@holograph
Copy link
Collaborator

com.flipkart.zjsonpatch.DiffFlags.ADD_ORIGINAL_VALUE_ON_REPLACE is there to aid in debugging, it's an extension to the RFC. I'm OK with adding a "check" flag (that should clearly indicate this is not standard), but IMO if what you need is data integrity, a much better approach would be to emit test nodes in the patch (instead of just the convenience value), which is part of the standard and would get you the behaviour your want.

@Hubbitus
Copy link
Author

Hubbitus commented Feb 3, 2019

a much better approach would be to emit test nodes in the patch (instead of just the convenience value)

Sorry I do not understand that. Could you please provide example?
And yes, I could archive context for data integrity to ensure apply is not destructive. Some sort of optimistic locking. What I suggest implement is behaviour similar to GNU diff.

@holograph
Copy link
Collaborator

Try:

[
  {"op": "test", "path": "/k2", "value": "v2"},
  {"op":"replace", "path":"/k2", "value": {"inner": 1}}
]

Should work as expected. If this is what you mean, DiffFlags could be modified to include flavours of EMIT_TEST_OPERATIONS, which is similar to ADD_ORIGINAL_VALUE_ON_REPLACE but doesn't require custom extensions to the RFC. @vishwakarma what do you think? If it seems reasonable I can pick this up.

@holograph
Copy link
Collaborator

@Hubbitus I implemented this in #92, have a look if that works for you :-)

@Hubbitus
Copy link
Author

Hubbitus commented Feb 4, 2019

It looks very well. Thank you very much! And if it is RFC then it indeed better approach then I suggest initially.

But I think it it have worth provide context on what path and value apply failed and extends tests on each type where such node may be present. Could you please look at comment https://github.com/flipkart-incubator/zjsonpatch/pull/92/files#r253562286

@holograph
Copy link
Collaborator

I'm working on that separately (see #94), but in the meantime I also realized we likely have defective generation of test ops for {"op":"test","path":"-",...} nodes. I'll add a test case and fix it soon.

vishwakarma pushed a commit that referenced this issue Feb 10, 2019
* Security upgrade (see FasterXML/jackson-databind#2186)

* Minor cleanup

* Further cleanup

* Added DiffFlags.EMIT_TEST_OPERATIONS, along with associated tests and functionality

* Further (minor) cleanup

* Corrected @SInCE version on EMIT_COPY_OPERATIONS
@holograph
Copy link
Collaborator

@Hubbitus Take a look at the proposed changes in #94, I believe they'll make you happy :-) In the meanwhile, I believe this issue in general has been addressed...

@Hubbitus
Copy link
Author

Thank you very much!

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

2 participants