-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(populate): make getSchemaTypes() handle embedded discriminators w…
…ithout a ref Re: #5970
- Loading branch information
Showing
2 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
|
||
var Schema = require('../../lib/schema'); | ||
var assert = require('assert'); | ||
var getSchemaTypes = require('../../lib/services/populate/getSchemaTypes'); | ||
|
||
describe('getSchemaTypes', function() { | ||
it('handles embedded discriminators (gh-5970)', function(done) { | ||
var ItemSchema = new Schema({ | ||
title: { | ||
type: String, | ||
required: true | ||
} | ||
}, {discriminatorKey: 'type'}); | ||
|
||
var ItemBookSchema = new Schema({ | ||
author: { | ||
type: String, | ||
ref: 'Ref1' | ||
} | ||
}); | ||
|
||
var ItemEBookSchema = new Schema({ | ||
author: { | ||
type: String, | ||
ref: 'Ref2' | ||
} | ||
}); | ||
|
||
var OtherItem = new Schema({ name: String }); | ||
|
||
var BundleSchema = new Schema({ | ||
items: [{ | ||
type: ItemSchema, | ||
required: false | ||
}] | ||
}); | ||
|
||
BundleSchema.path('items').discriminator('Book', ItemBookSchema); | ||
BundleSchema.path('items').discriminator('EBook', ItemEBookSchema); | ||
BundleSchema.path('items').discriminator('Other', OtherItem); | ||
|
||
var doc = { | ||
items: [ | ||
{ type: 'Book', author: 'test 1' }, | ||
{ type: 'EBook', author: 'test 2' }, | ||
{ type: 'Other' }, | ||
] | ||
} | ||
var schemaTypes = getSchemaTypes(BundleSchema, doc, 'items.author'); | ||
|
||
assert.ok(Array.isArray(schemaTypes)); | ||
// Make sure we only got the schema paths for Book and EBook, and none | ||
// for the 'Other' | ||
assert.equal(schemaTypes.length, 2); | ||
assert.equal(schemaTypes[0].options.ref, 'Ref1'); | ||
assert.equal(schemaTypes[1].options.ref, 'Ref2'); | ||
|
||
done(); | ||
}); | ||
}); |