Skip to content

Commit

Permalink
Merge branch 'master' into vkarpov15/gh-13799
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 authored Sep 25, 2023
2 parents 1bed0c7 + f4fc8a7 commit f9e5043
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cause any problems for your application. Please [report any issues on GitHub](ht

To fix all deprecation warnings, follow the below steps:

* Replace `rawResult: true` with `includeResultMetadata: false` in `findOneAndUpdate()`, `findOneAndReplace()`, `findOneAndDelete()` calls.
* Replace `rawResult: true` with `includeResultMetadata: true` in `findOneAndUpdate()`, `findOneAndReplace()`, `findOneAndDelete()` calls.

Read below for more a more detailed description of each deprecation warning.

Expand Down
17 changes: 10 additions & 7 deletions lib/helpers/discriminator/getConstructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ const getDiscriminatorByValue = require('./getDiscriminatorByValue');
* @api private
*/

module.exports = function getConstructor(Constructor, value) {
module.exports = function getConstructor(Constructor, value, defaultDiscriminatorValue) {
const discriminatorKey = Constructor.schema.options.discriminatorKey;
if (value != null &&
Constructor.discriminators &&
value[discriminatorKey] != null) {
if (Constructor.discriminators[value[discriminatorKey]]) {
Constructor = Constructor.discriminators[value[discriminatorKey]];
let discriminatorValue = (value != null && value[discriminatorKey]);
if (discriminatorValue == null) {
discriminatorValue = defaultDiscriminatorValue;
}
if (Constructor.discriminators &&
discriminatorValue != null) {
if (Constructor.discriminators[discriminatorValue]) {
Constructor = Constructor.discriminators[discriminatorValue];
} else {
const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, value[discriminatorKey]);
const constructorByValue = getDiscriminatorByValue(Constructor.discriminators, discriminatorValue);
if (constructorByValue) {
Constructor = constructorByValue;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/schema/SubdocumentPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ SubdocumentPath.prototype.cast = function(val, doc, init, priorVal, options) {
throw new ObjectExpectedError(this.path, val);
}

const Constructor = getConstructor(this.caster, val);
const discriminatorKeyPath = this.schema.path(this.schema.options.discriminatorKey);
const defaultDiscriminatorValue = discriminatorKeyPath == null ? null : discriminatorKeyPath.getDefault(doc);
const Constructor = getConstructor(this.caster, val, defaultDiscriminatorValue);

let subdoc;

Expand Down
27 changes: 27 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12500,6 +12500,33 @@ describe('document', function() {

assert.equal(updatedPerson?._age, 61);
});

it('handles default embedded discriminator values (gh-13835)', async function() {
const childAbstractSchema = new Schema(
{ kind: { type: Schema.Types.String, enum: ['concreteKind'], required: true, default: 'concreteKind' } },
{ discriminatorKey: 'kind', _id: false }
);
const childConcreteSchema = new Schema({ concreteProp: { type: Number, required: true } });

const parentSchema = new Schema(
{
child: {
type: childAbstractSchema,
required: true
}
},
{ _id: false }
);

parentSchema.path('child').discriminator('concreteKind', childConcreteSchema);

const ParentModel = db.model('Test', parentSchema);

const parent = new ParentModel({ child: { concreteProp: 123 } });
assert.strictEqual(parent.child.concreteProp, 123);
assert.strictEqual(parent.get('child.concreteProp'), 123);
assert.strictEqual(parent.toObject().child.concreteProp, 123);
});
});

describe('Check if instance function that is supplied in schema option is availabe', function() {
Expand Down

0 comments on commit f9e5043

Please sign in to comment.