-
Notifications
You must be signed in to change notification settings - Fork 217
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
Undefined properties cause an exception to be thrown #32
Comments
We are using 1c5ef73 internally. This commit fixes the bug trivially, by simply not reporting on properties that are undefined. This may or may not be the appropriate long-term fix, though it does work for our needs. If it is desirable to track the addition of undefined properties in some context, a more thorough solution should be sought. I am thus not adding a pull request for this fix. |
Something like this should do the trick: var clone = obj[key];
mirror[key] = clone ? JSON.parse(JSON.stringify(obj[key])) : clone; We only need to create the clone if it's an object; otherwise it's passed by value and we're fine. And objects are always truthy. |
We could handle it somehow in |
@sonnyp has a convincing argument that we should not handle invalid JSON values. I've been persuaded. What do you think, @warpech? |
Ah, okay. Sounds good 👍 |
I've just added a description at README.md, and bunch of tests. Basically:
|
Is it too late to reconsider?
Users can Detail: Like @wolfgang42 in #60, I believe
RFC6902 says, over and over:
If Users can Consider setting a value to var myobj = { };
var observer = jsonpatch.observe( myobj );
myobj.firstName = undefined;
var patch = jsonpatch.generate( observer ); A patch of [ { op: 'add', path: '/firstName', value: undefined } ] [ { "op": "add", "path": "/firstName" } ] In short, for any mutation:
Edits:
|
There are many other edge cases. JSON.stringify discard undefined values in objects, not in arrays where they are turned into null.
Same thing for ES6 symbols And there is the special case where the value is an object and replaced with the result of its toJSON method if defined. also and pretty much the same hings if an object key isn't JSON valid. I think we should provide a sanitize function that runs the patch and/or the target document through JSON.stringify/JSON.parse and recommend using it before evaluation. |
Fixed in 1.0.0 |
Consider the following code:
JSON-Patch throws a SyntaxError exception when trying to generate the patch at line 421 of json-patch-duplex.js (in the function
_generate
):obj[key]
isundefined
, causingJSON.stringify()
to returnundefined
, whichJSON.parse()
cannot handle ("Unexpected token u").The expected behavior is that no patches should be generated, as nothing has changed.
The text was updated successfully, but these errors were encountered: