Skip to content

fix non-object attributes being passed to model.parse()#2056

Merged
ricardograca merged 4 commits into
bookshelf:masterfrom
brendantelz:master
Mar 22, 2020
Merged

fix non-object attributes being passed to model.parse()#2056
ricardograca merged 4 commits into
bookshelf:masterfrom
brendantelz:master

Conversation

@brendantelz

Copy link
Copy Markdown
Contributor

Introduction

When model.save() is executed with method: 'update', the resp argument is first the number of rows that were updated. This causes undefined to be passed into this.parse on line 1183 in model.save(). Any custom model.parse functions on a model are expecting an object of attributes to be passed.

Also, this branch can be reached if the save method is 'insert' and this.id is not null, which we have found to occur when query.options.returning was set to 'id' instead of the default '*' which bookshelf expects.

Motivation

Any custom model.parse functions are not expecting to be receiving any argument other than an object, potentially causing unexpected errors.

Proposed solution

Check that the response is an object before passing it to model.parse and merging with this.attributes.

@ricardograca

Copy link
Copy Markdown
Member

Won't this fix mean that we will still be left without the updated attributes?

@brendantelz

Copy link
Copy Markdown
Contributor Author

@ricardograca When the query returns the updated attributes in an object, yes it will add the updated attributes. Added a unit test for that behavior.

Perhaps you know more about why this is, but I found that this code gets called twice on a model.save() - the first time resp is number of rows updated, the second time it receives an array containing the object with all model attributes.

Thank you for the review!

@ricardograca
ricardograca merged commit 45c23b4 into bookshelf:master Mar 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.save({method: 'insert') is attempting to parse() undefined objects

2 participants