Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -2609,6 +2609,11 @@ DataAccessObject.replaceById = function(id, data, options, cb) {
err.statusCode = 400;
process.nextTick(function() { cb(err); });
return cb.promise;
} else {
// Ensure any type conversion applied by the instance constructor
// on `data.id` is applied on the `id` value too.
// Typically, MongoDB converts PK value from a string to an ObjectID.
id = inst[pkName];
}

let context = {
Expand Down
27 changes: 27 additions & 0 deletions test/manipulation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,33 @@ describe('manipulation', function() {
done();
});
});

it('correctly coerces the PK value', async () => {
const created = await Post.create({
title: 'a title',
content: 'a content',
});

// Emulate what happens when model instance is received by REST API clients
const data = JSON.parse(JSON.stringify(created));

// Modify some of the data
data.title = 'Draft';

// Call replaceById to modify the database record
await Post.replaceById(data.id, data);

// Verify what has been stored
const found = await Post.findById(data.id);
found.toObject().should.eql({
id: created.id,
title: 'Draft',
content: 'a content',
});

// 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.

});
});

describe('findOrCreate', function() {
Expand Down