diff --git a/lib/adapters/mongodb.js b/lib/adapters/mongodb.js index ef72a2fdb..961a256ba 100644 --- a/lib/adapters/mongodb.js +++ b/lib/adapters/mongodb.js @@ -416,8 +416,11 @@ adapter._serialize = function (model, resource) { } } - //TODO: This may cause WEB-2618 issue. Test for /[0-9a-f]{24}/ before casting - resource[pk] = pkType(resource[pk]); + resource[pk] = pkType(resource[pk] || resource.id); + if (!resource[pk]){ + //If failed to cast - generate ObjectId from provided .id + resource._id = mongoose.Types.ObjectId(resource.id.toString()); + } delete resource.id; } diff --git a/test/fortune-mongodb/mongodb.spec.js b/test/fortune-mongodb/mongodb.spec.js index f5b902130..2b70d4894 100644 --- a/test/fortune-mongodb/mongodb.spec.js +++ b/test/fortune-mongodb/mongodb.spec.js @@ -18,6 +18,34 @@ module.exports = function(options){ ids = options.ids; }); + describe('Creation', function(){ + it('should be able to create document with provided id', function(done){ + var doc = { + id: '123456789012345678901234' + }; + adapter.create('pet', doc).then(function(){ + var model = adapter.model('pet'); + model.findOne({_id: '123456789012345678901234'}).exec(function(err, doc){ + should.not.exist(err); + should.exist(doc); + done(); + }); + }); + }); + it('should be able to cast provided id to proper type', function(done){ + var doc = { + id: '123456789012345678901234' + }; + adapter.create('person', doc).then(function(){ + var model = adapter.model('person'); + model.findOne({email: '123456789012345678901234'}).exec(function(err, doc){ + should.not.exist(err); + should.exist(doc); + done(); + }); + }); + }); + }); describe('Relationships', function(){ describe('synchronizing many-to-many', function(){ it('should keep in sync many-to-many relationship', function(done){