Skip to content

Commit

Permalink
Merge branch '6.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Apr 3, 2023
2 parents ae931b7 + 8b661fd commit c98312e
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
1 change: 0 additions & 1 deletion docs/js/navbar-search.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';

(function() {
const versionFromUrl = window.location.pathname.match(/^\/docs\/(\d+\.x)/);
const version = versionFromUrl ? versionFromUrl[1] : null;
Expand Down
4 changes: 4 additions & 0 deletions docs/source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ const docs = {
};

docs['index.pug'] = require('./home');
<<<<<<< HEAD
docs['docs/api.md'] = {
docs: [],
title: 'Redirect to API',
markdown: true
};
=======
docs['docs/api.pug'] = require('./api');
>>>>>>> 6.x

docs['docs/advanced_schemas.md'] = { title: 'Advanced Schemas', acquit: true, markdown: true };
docs['docs/validation.md'] = { title: 'Validation', acquit: true, markdown: true };
Expand Down
4 changes: 2 additions & 2 deletions lib/schema/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ SchemaArray.prototype.$toObject = SchemaArray.prototype.toObject;
* ignore
*/

SchemaArray.prototype.discriminator = function(name, schema) {
SchemaArray.prototype.discriminator = function(...args) {
let arr = this;
while (arr.$isMongooseArray && !arr.$isMongooseDocumentArray) {
arr = arr.casterConstructor;
Expand All @@ -461,7 +461,7 @@ SchemaArray.prototype.discriminator = function(name, schema) {
'a document array, ' + this.path + ' is a plain array');
}
}
return arr.discriminator(name, schema);
return arr.discriminator(...args);
};

/*!
Expand Down
18 changes: 18 additions & 0 deletions test/docs/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,22 @@ describe('debug: shell', function() {
// Last log should not have been overwritten
assert.equal(storedLog, lastLog);
});

it('should avoid sending null session option with document ops (gh-13052)', async function() {
const args = [];
const m = new mongoose.Mongoose();
m.set('debug', function() {
args.push([...arguments]);
});
await m.connect(start.uri);
const schema = new Schema({ name: String });
const Test = m.model('gh_13052', schema);

await Test.create({ name: 'foo' });
assert.equal(args.length, 1);
assert.equal(args[0][1], 'insertOne');
assert.ok(!('session' in args[0][3]));

await m.disconnect();
});
});
68 changes: 68 additions & 0 deletions test/model.discriminator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2112,4 +2112,72 @@ describe('model', function() {
const Test = Base.discriminator('model-discriminator-custom1', childSchema);
assert.deepEqual(Test.schema.options.toJSON, { virtuals: true, getters: true });
});
it('uses "value" over "name" for multi-dimensonal arrays (gh-13201)', function() {
const buildingSchema = new mongoose.Schema(
{
width: {
type: Number,
default: 100
},
type: {
type: String,
enum: ['G', 'S']
}
},
{ discriminatorKey: 'type', _id: false }
);

const garageSchema = buildingSchema.clone();
garageSchema.add({
slotsForCars: {
type: Number,
default: 10
}
});

const summerSchema = buildingSchema.clone();
summerSchema.add({
distanceToLake: {
type: Number,
default: 100
}
});

const areaSchema = new mongoose.Schema({
buildings: {
type: [
[
{
type: buildingSchema
}
]
]
}
});

garageSchema.paths['type'].options.$skipDiscriminatorCheck = true;
summerSchema.paths['type'].options.$skipDiscriminatorCheck = true;
const path = areaSchema.path('buildings');

path.discriminator('Garage', garageSchema, 'G');
path.discriminator('Summer', summerSchema, 'S');

const AreaModel = mongoose.model('Area', areaSchema);

const area = new AreaModel({
buildings: [
[
{ type: 'S', distanceToLake: 100 },
{ type: 'G', slotsForCars: 20 }
]
]
});

assert.ok(area.buildings[0][0].distanceToLake);
assert.ok(area.buildings[0][1].slotsForCars);

const innerBuildingsPath = AreaModel.schema.path('buildings.$');
assert.ok(innerBuildingsPath.schemaOptions.type.discriminators.Garage);
assert.equal(innerBuildingsPath.schemaOptions.type.discriminators.Garage.discriminatorMapping.value, 'G');
});
});

0 comments on commit c98312e

Please sign in to comment.