-
Notifications
You must be signed in to change notification settings - Fork 14
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
Don't generate patch for a no-change set operation #45
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good change! However, for this PR to be accepted, there need to be tests for the change
Added braces for readability.
These operations should result in empty patches. Also updated some tests to also check proper handling of `null` values when replaced with or replacing `undefined` values.
Tests added. I have also worked through no-change operations on array elements and made sure |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could copy some tests for undefined
behavior from fast-json-patch
newValue = null; | ||
} | ||
return oldValue !== newValue; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this function (and tests) need to be extended cover scenario of adding a null
beyond the end of the array:
arr = Array(5);
JSON.stringify(arr); // "[null,null,null,null,null]"
arr[7]; // undefined
arr[7] = null; // null - Even though `typeof oldValue === 'undefined'`, this is a significant change, as
JSON.stringify(arr); // [null,null,null,null,null,null,null,null]"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tomalec Good catch! I noticed that these cases are were not dealt with properly and had intended to file a separate issue about it. The thing is that adding undefined
elements is broken on master
as well and therefore i figured it was a bit out of scope for this PR.
Check out failing test on 7cf32fd which is based on current master
. Here the undefined
element results in a replace
patch when it should be an add
. Based on this PR, the same test also fails, although differently: 5de895f. Here the undefined
element is simply ignored. Note that both fail on elements which are added implicitly without ever being set such as arr[5]
and arr[6]
in the example which are not present in the patch at all.
The test is based on your example with an addition:
arr = Array(5);
JSON.stringify(arr); // "[null,null,null,null,null]"
arr[7]; // undefined
arr[7] = null;
arr[8] = undefined;
// null - Even though `typeof oldValue === 'undefined'`, these are significant changes, as
JSON.stringify(arr); // [null,null,null,null,null,null,null,null,null]"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #52.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We found another issue with this function. Is it called when you pop
an array?
Then change from undefined
(mapped to null
) to not existing element.
I guess it should call deleteTrap
but it's worth to add a test for such case as well.
@warpech suggested to make |
I will make the required changes in a new PR. |
@tomalec I have made two alternative solutions:
The code is quite complex, and I am not very happy with the readability of either. Any suggestions which is better? (I can accept "Current" as an answer, too) |
I have a slight preference over 1. because of readable variable names |
As discussed today between @eriksunsol, @tomalec and me, I have pushed my solution that fixes the test created by @eriksunsol (0257a92). @tomalec, @eriksunsol could you pls check and give your blessing? I plan to add more tests from https://github.com/Starcounter-Jack/JSON-Patch/tree/master/test in a separate PR before next release. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than code comment and single test to checked and preferably added, LGTM
newValue = null; | ||
} | ||
return oldValue !== newValue; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We found another issue with this function. Is it called when you pop
an array?
Then change from undefined
(mapped to null
) to not existing element.
I guess it should call deleteTrap
but it's worth to add a test for such case as well.
Let's continue that in #52 |
Extending the example in README.md like this:
results in the following patch:
The second and third time
firstName
is set are effectively no-ops which have already been communicated. I think this is wrong. The proposed change recognizes this and renders the following result instead: