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

Fix JSON.stringify causing TypeErrors #2029

Merged
merged 4 commits into from Oct 5, 2019
Merged

Fix JSON.stringify causing TypeErrors #2029

merged 4 commits into from Oct 5, 2019

Conversation

@anajavi
Copy link
Contributor

@anajavi anajavi commented Oct 4, 2019

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

@ricardograca ricardograca self-requested a review Oct 5, 2019
Copy link
Member

@ricardograca ricardograca left a comment

Looks great, but the simpler object check would be preferable.

lib/base/model.js Outdated Show resolved Hide resolved
@anajavi
Copy link
Contributor Author

@anajavi anajavi commented Oct 5, 2019

I hope you can release this fix soon, as it is blocking upgrade to 1.0 for me.

Copy link
Member

@ricardograca ricardograca left a comment

Looks good.

@ricardograca ricardograca merged commit e87b9e4 into bookshelf:master Oct 5, 2019
1 check passed
@ricardograca
Copy link
Member

@ricardograca ricardograca commented Oct 5, 2019

This will be released very soon as there's no reason not to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants