Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(model): handle discriminators in castObject() #15096

Merged
merged 1 commit into from
Dec 16, 2024
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
6 changes: 5 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3643,7 +3643,11 @@ Model.castObject = function castObject(obj, options) {
options = options || {};
const ret = {};

const schema = this.schema;
let schema = this.schema;
const discriminatorKey = schema.options.discriminatorKey;
if (schema.discriminators != null && obj != null && obj[discriminatorKey] != null) {
schema = getSchemaDiscriminatorByValue(schema, obj[discriminatorKey]) || schema;
}
const paths = Object.keys(schema.paths);

for (const path of paths) {
Expand Down
61 changes: 61 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7717,6 +7717,67 @@ describe('Model', function() {
const ret = Test.castObject(obj, { ignoreCastErrors: true });
assert.deepStrictEqual(ret, { nested: { num: 2 }, docArr: [{ num: 4 }] });
});
it('handles discriminators (gh-15075)', async function() {
// Create the base shape schema
const shapeSchema = new mongoose.Schema({ name: String }, {
discriminatorKey: 'kind',
_id: false
});

// Main schema with shape array
const schema = new mongoose.Schema({
shape: [shapeSchema]
});

// Circle discriminator
schema
.path('shape')
.discriminator('Circle', new mongoose.Schema({
radius: {
type: mongoose.Schema.Types.Number,
required: true
}
}, { _id: false }));

// PropertyPath schema for Square
const propertyPathSchema = new mongoose.Schema({
property: {
type: mongoose.Schema.Types.String,
required: true
},
path: {
type: mongoose.Schema.Types.String,
required: true
}
}, { _id: false });

// Square discriminator
schema
.path('shape')
.discriminator(
'Square',
new mongoose.Schema({
propertyPaths: {
type: [propertyPathSchema],
required: true
}
}, { _id: false })
);

const TestModel = db.model('Test', schema);

const circle = { shape: [{ kind: 'Circle', radius: '5' }] };
const square = { shape: [{ kind: 'Square', propertyPaths: [{ property: 42 }] }] };

assert.deepStrictEqual(
TestModel.castObject(circle).shape[0],
{ kind: 'Circle', radius: 5 }
);
assert.deepStrictEqual(
TestModel.castObject(square).shape[0],
{ kind: 'Square', propertyPaths: [{ property: '42' }] }
);
});
});

it('works if passing class that extends Document to `loadClass()` (gh-12254)', async function() {
Expand Down
Loading