Fix JSON.stringify causing TypeErrors#2029
Merged
ricardograca merged 4 commits intobookshelf:masterfrom Oct 5, 2019
anajavi:fix-2028
Merged
Fix JSON.stringify causing TypeErrors#2029ricardograca merged 4 commits intobookshelf:masterfrom anajavi:fix-2028
ricardograca merged 4 commits intobookshelf:masterfrom
anajavi:fix-2028
Conversation
ricardograca
requested changes
Oct 5, 2019
Member
ricardograca
left a comment
There was a problem hiding this comment.
Looks great, but the simpler object check would be preferable.
Contributor
Author
|
I hope you can release this fix soon, as it is blocking upgrade to 1.0 for me. |
Member
|
This will be released very soon as there's no reason not to. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduction
Fix JSON.stringify throwing TypeError if Model object is placed as object property or in an array.
Motivation
JSON.stringify calls .toJSON on object to be serialized. When called in array, it passes index as parameter to .toJSON. When called in object it passes the property name as parameter:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON_behavior
Bookshelf model accepts options object as parameter for toJSON, it however does not check if passed parameter is object before trying to put properties in it. This results in
TypeError: Cannot create property 'visibility' on string 'propertyname'Proposed solution
The proposed solution is to check if options is really an object by checking it with lodash.isPlainObject.
Current PR Issues
Not benchmarked this, so can't know the performance implications.
Alternatives considered
Simpler Object check might be more performant. One from redux:
https://github.com/reduxjs/redux/blob/master/src/utils/isPlainObject.ts