Skip to content

Commit

Permalink
fix(discriminator): support tiedValue parameter for embedded discri…
Browse files Browse the repository at this point in the history
…minators analagous to top-level discriminators

Fix #8164
  • Loading branch information
vkarpov15 committed Sep 23, 2019
1 parent d8cc819 commit e2d191a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/schema/SingleNestedPath.js
Expand Up @@ -283,8 +283,8 @@ SingleNestedPath.prototype.doValidateSync = function(value, scope, options) {
* @api public
*/

SingleNestedPath.prototype.discriminator = function(name, schema) {
discriminator(this.caster, name, schema);
SingleNestedPath.prototype.discriminator = function(name, schema, tiedValue) {
discriminator(this.caster, name, schema, tiedValue);

this.caster.discriminators[name] = _createConstructor(schema, this.caster);

Expand Down
4 changes: 2 additions & 2 deletions lib/schema/documentarray.js
Expand Up @@ -126,12 +126,12 @@ function _createConstructor(schema, options, baseClass) {
* Ignore
*/

DocumentArray.prototype.discriminator = function(name, schema) {
DocumentArray.prototype.discriminator = function(name, schema, tiedValue) {
if (typeof name === 'function') {
name = utils.getFunctionName(name);
}

schema = discriminator(this.casterConstructor, name, schema);
schema = discriminator(this.casterConstructor, name, schema, tiedValue);

const EmbeddedDocument = _createConstructor(schema, null, this.casterConstructor);
EmbeddedDocument.baseCasterConstructor = this.casterConstructor;
Expand Down
40 changes: 40 additions & 0 deletions test/model.discriminator.test.js
Expand Up @@ -1080,6 +1080,46 @@ describe('model', function() {
catch(done);
});

it('embedded with single nested subdocs and tied value (gh-8164)', function() {
const eventSchema = new Schema({ message: String },
{ discriminatorKey: 'kind', _id: false });

const trackSchema = new Schema({ event: eventSchema });
trackSchema.path('event').discriminator('Clicked', new Schema({
element: String
}, { _id: false }), 'click');
trackSchema.path('event').discriminator('Purchased', new Schema({
product: String
}, { _id: false }), 'purchase');

const MyModel = db.model('gh8164', trackSchema);
const doc1 = {
event: {
kind: 'click',
element: 'Amazon Link'
}
};
const doc2 = {
event: {
kind: 'purchase',
product: 'Professional AngularJS'
}
};
return MyModel.create([doc1, doc2]).
then(function(docs) {
const doc1 = docs[0];
const doc2 = docs[1];

assert.equal(doc1.event.kind, 'click');
assert.equal(doc1.event.element, 'Amazon Link');
assert.ok(!doc1.event.product);

assert.equal(doc2.event.kind, 'purchase');
assert.equal(doc2.event.product, 'Professional AngularJS');
assert.ok(!doc2.event.element);
});
});

it('Embedded discriminators in nested doc arrays (gh-6202)', function() {
const eventSchema = new Schema({ message: String }, {
discriminatorKey: 'kind',
Expand Down

0 comments on commit e2d191a

Please sign in to comment.