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 parsing empty strings #50

Merged
merged 7 commits into from
Jul 18, 2017
Merged

Fix parsing empty strings #50

merged 7 commits into from
Jul 18, 2017

Conversation

zhongzhi107
Copy link
Contributor

@zhongzhi107 zhongzhi107 commented May 24, 2017

This PR fixes a bug when using empty strings on update with the patch option. Since the values aren't being stringified, an error was raised when parsing them.

@ricardogama
Copy link
Collaborator

Thanks @zhongzhi107 for the contribution.

However, this doesn't seem to fix #49, as @pandapaul explained the error is thrown when a value was already stringified on save, or is malformed in the database and it fails when parsing on fetch.

Also, you seem to be fixing null or undefined values and I think we already cover that on the test suite. If I'm wrong can you please add tests to this use case?

@zhongzhi107
Copy link
Contributor Author

Hi @ricardogama , I add test case.
An error occurs when updating an field with an empty string.

SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) at Object.keys.forEach.attribute (/home/q/www/qrmaps_api.qunar.com/node_modules/bookshelf-json-columns/dist/index.js:114:48) at Array.forEach (native) at Draft.Model.save.call.then.model (/home/q/www/qrmaps_api.qunar.com/node_modules/bookshelf-json-columns/dist/index.js:112:33) at Draft.tryCatcher (/home/q/www/qrmaps_api.qunar.com/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/home/q/www/qrmaps_api.qunar.com/node_modules/bluebird/js/release/promise.js:512:31) at Promise._settlePromise (/home/q/www/qrmaps_api.qunar.com/node_modules/bluebird/js/release/promise.js:569:18) at Promise._settlePromise0 (/home/q/www/qrmaps_api.qunar.com/node_modules/bluebird/js/release/promise.js:614:10) at Promise._settlePromises (/home/q/www/qrmaps_api.qunar.com/node_modules/bluebird/js/release/promise.js:693:18) at Async._drainQueue (/home/q/www/qrmaps_api.qunar.com/node_modules/bluebird/js/release/async.js:133:16) at Async._drainQueues (/home/q/www/qrmaps_api.qunar.com/node_modules/bluebird/js/release/async.js:143:10) at Immediate.Async.drainQueues (/home/q/www/qrmaps_api.qunar.com/node_modules/bluebird/js/release/async.js:17:14)

Copy link
Collaborator

@ricardogama ricardogama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @zhongzhi107 for the late review, this seems good to merge once you review my comments.

If you don't mind I'll change the PR name and description to something more meaningful to keep the history clean.

Cheers!

src/index.js Outdated
@@ -103,7 +103,9 @@ export default Bookshelf => {
// Parse JSON columns.
Object.keys(attributes).forEach(attribute => {
if (this.constructor.jsonColumns.includes(attribute)) {
model.attributes[attribute] = JSON.parse(model.attributes[attribute]);
if (model.attributes[attribute]) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update to the following to keep consistency with other conditionals like this:

if (this.constructor.jsonColumns.includes(attribute) && attributes[attribute]) {
  model.attributes[attribute] = JSON.parse(model.attributes[attribute]);
}

@@ -140,6 +140,19 @@ describe('with MySQL client', () => {
sinon.restore(ModelPrototype);
});

it('should not stringify \'\' values on update with `patch` option', async () => {
Copy link
Collaborator

@ricardogama ricardogama Jul 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test does not assert that the value has not been parsed, use something like this instead:

it('should keep an empty string on update with `patch` option', async () => {
  const model = await Model.forge({ foo: 'bar' }).save();

  await model.save({ foo: '' }, { patch: true });

  model.get('foo').should.equal('');
});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please add the same test to the SQLite3 suite.

@ricardogama ricardogama changed the title fix: Handling of parse errors #49 Fix parsing empty strings Jul 13, 2017
@zhongzhi107
Copy link
Contributor Author

Thanks @ricardogama for the advices.
The code have been modified and please review it again.

dist/index.js Outdated
@@ -111,7 +111,9 @@ exports.default = Bookshelf => {
// Parse JSON columns.
Object.keys(attributes).forEach(attribute => {
if (this.constructor.jsonColumns.indexOf(attribute) !== -1) {
model.attributes[attribute] = JSON.parse(model.attributes[attribute]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good, but please remove changes on this file as the dist files are only added on the release commit.

@zhongzhi107
Copy link
Contributor Author

I'm sorry for my carelessness. The changes have been discarded.

@ricardogama ricardogama merged commit a6ebb51 into seegno:master Jul 18, 2017
@ricardogama
Copy link
Collaborator

Thanks @zhongzhi107, released as 2.1.1.

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

Successfully merging this pull request may close these issues.

2 participants