Skip to content

Commit

Permalink
Merge pull request #14541 from Automattic/vkarpov15/gh-14520
Browse files Browse the repository at this point in the history
types(document): make document _id type default to unknown instead of any
  • Loading branch information
vkarpov15 committed Apr 27, 2024
2 parents 6ca274d + 23869fb commit cb2e7c1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/schema/documentArray.js
Expand Up @@ -195,7 +195,7 @@ SchemaDocumentArray.prototype.discriminator = function(name, schema, options) {
schema = schema.clone();
}

schema = discriminator(this.casterConstructor, name, schema, tiedValue);
schema = discriminator(this.casterConstructor, name, schema, tiedValue, null, null, options?.overwriteExisting);

const EmbeddedDocument = _createConstructor(schema, null, this.casterConstructor);
EmbeddedDocument.baseCasterConstructor = this.casterConstructor;
Expand Down
17 changes: 17 additions & 0 deletions test/model.test.js
Expand Up @@ -7463,6 +7463,23 @@ describe('Model', function() {
assert.equal(instance.item.whoAmI(), 'I am Test2');
});

it('overwrites existing discriminators when calling recompileSchema (gh-14527) (gh-14444)', async function() {
const shopItemSchema = new mongoose.Schema({}, { discriminatorKey: 'type' });
const shopSchema = new mongoose.Schema({
items: { type: [shopItemSchema], required: true }
});

const shopItemSubType = new mongoose.Schema({ prop: Number });
shopItemSchema.discriminator(2, shopItemSubType);
const shopModel = db.model('shop', shopSchema);

shopModel.recompileSchema();
const doc = new shopModel({
items: [{ type: 2, prop: 42 }]
});
assert.equal(doc.items[0].prop, 42);
});

it('inserts versionKey even if schema has `toObject.versionKey` set to false (gh-14344)', async function() {
const schema = new mongoose.Schema(
{ name: String },
Expand Down
2 changes: 1 addition & 1 deletion types/document.d.ts
Expand Up @@ -16,7 +16,7 @@ declare module 'mongoose' {
* * TQueryHelpers - Object with any helpers that should be mixed into the Query type
* * DocType - the type of the actual Document created
*/
class Document<T = any, TQueryHelpers = any, DocType = any> {
class Document<T = unknown, TQueryHelpers = any, DocType = any> {
constructor(doc?: any);

/** This documents _id. */
Expand Down
2 changes: 1 addition & 1 deletion types/types.d.ts
Expand Up @@ -83,7 +83,7 @@ declare module 'mongoose' {
class ObjectId extends mongodb.ObjectId {
}

class Subdocument<IdType = any, TQueryHelpers = any, DocType = any> extends Document<IdType, TQueryHelpers, DocType> {
class Subdocument<IdType = unknown, TQueryHelpers = any, DocType = any> extends Document<IdType, TQueryHelpers, DocType> {
$isSingleNested: true;

/** Returns the top level document of this sub-document. */
Expand Down

0 comments on commit cb2e7c1

Please sign in to comment.