Skip to content

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented Jul 25, 2019

Before this change, when both the PK value (id) and the data object were provided as plain-data values (e.g. as received in a JSON request), and the connector was using a complex PK type (e.g. ObjectID in MongoDB), then replaceById operation was printing confusing warnings:

WARNING: id property cannot be changed from 5d39775a59f5f541513c5e05
    to 5d39775a59f5f541513c5e05 for model:Post
    in 'before save' operation hook

WARNING: id property cannot be changed from 5d39775a59f5f541513c5e05
    to 5d39775a59f5f541513c5e05 for model:Post
    in 'loaded' operation hook

This commit fixes the problem by applying the same type coercion on the PK value (id) as has been applied by the model constructor on the PK property (data.id).

Related issues

I discovered this problem while working on the spike for Inclusion of related models (see loopbackio/loopback-next#3387), but the relation is not really relevant.

  • connect to <link_to_referenced_issue>

Checklist

  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style
    guide

});

// Verify that no warnings were triggered
Object.keys(Post._warned).should.be.empty();
Copy link
Member Author

Choose a reason for hiding this comment

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

Without my fix in place, this checks passes when we run the tests in juggler, but fails against the MongoDB connector. I have verified that myself by running the MongoDB test suite against npm link-ed version of juggler.

@bajtos bajtos force-pushed the fix/PK-coercion-in-replaceById branch from c1fb686 to bb1cc70 Compare July 26, 2019 07:00
Before this change, when both the PK value (`id`) and the `data` object
were provided as plain-data values (e.g. as received in a JSON request),
and the connector was using a complex PK type (e.g. `ObjectID`
in MongoDB), then `replaceById` operation was printing confusing
warnings:

    WARNING: id property cannot be changed from 5d39775a59f5f541513c5e05
        to 5d39775a59f5f541513c5e05 for model:Post
        in 'before save' operation hook

    WARNING: id property cannot be changed from 5d39775a59f5f541513c5e05
        to 5d39775a59f5f541513c5e05 for model:Post
        in 'loaded' operation hook

This commit fixes the problem by applying the same type coercion on the
PK value (`id`) as has been applied by the model constructor on the PK
property (`data.id`).

Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
@bajtos bajtos force-pushed the fix/PK-coercion-in-replaceById branch from bb1cc70 to f1fa976 Compare July 26, 2019 07:02
@bajtos
Copy link
Member Author

bajtos commented Jul 26, 2019

[cis-jenkins] downstream: loopback-connector-mysql@master — Failed! (f1fa976)
[cis-jenkins] downstream: loopback-connector-oracle@master — Failed

I checked the console logs and these downstream failures do not seem to be caused by my changes.

@bajtos bajtos merged commit 74c43ca into master Jul 26, 2019
@delete-merged-branch delete-merged-branch bot deleted the fix/PK-coercion-in-replaceById branch July 26, 2019 07:22
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.

5 participants