Skip to content

Commit

Permalink
types(document): make sure toObject() and toJSON() apply versionK…
Browse files Browse the repository at this point in the history
…ey __v

Fix #15077
  • Loading branch information
vkarpov15 committed Dec 15, 2024
1 parent 7ed441e commit 03407f6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
36 changes: 36 additions & 0 deletions test/types/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,39 @@ async function gh14876() {
expectType<UserObjectInterface>(populatedCar.owner);
expectType<Types.ObjectId>(depopulatedCar.owner);
}

async function gh15077() {
type Foo = {
state: 'on' | 'off';
};

const fooSchema = new Schema<Foo>(
{
state: {
type: String,
enum: ['on', 'off']
}
},
{ timestamps: true }
);

const fooModel = model('foo', fooSchema);

let foundFoo = await fooModel
.findOne({
state: 'on'
})
.lean()
.exec();

if (!foundFoo) {
const newFoo = {
state: 'on'
// extra props but irrelevant
};

const createdFoo = await fooModel.create(newFoo);

foundFoo = createdFoo.toObject(); // this errors on 8.8.3
}
}
26 changes: 13 additions & 13 deletions types/document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,21 @@ declare module 'mongoose' {
set(value: string | Record<string, any>): this;

/** The return value of this method is used in calls to JSON.stringify(doc). */
toJSON(options?: ToObjectOptions & { flattenMaps?: true, flattenObjectIds?: false }): FlattenMaps<Require_id<DocType>>;
toJSON(options: ToObjectOptions & { flattenObjectIds: false }): FlattenMaps<Require_id<DocType>>;
toJSON(options: ToObjectOptions & { flattenObjectIds: true }): ObjectIdToString<FlattenMaps<Require_id<DocType>>>;
toJSON(options: ToObjectOptions & { flattenMaps: false }): Require_id<DocType>;
toJSON(options: ToObjectOptions & { flattenMaps: false; flattenObjectIds: true }): ObjectIdToString<Require_id<DocType>>;

toJSON<T = Require_id<DocType>>(options?: ToObjectOptions & { flattenMaps?: true, flattenObjectIds?: false }): FlattenMaps<T>;
toJSON<T = Require_id<DocType>>(options: ToObjectOptions & { flattenObjectIds: false }): FlattenMaps<T>;
toJSON<T = Require_id<DocType>>(options: ToObjectOptions & { flattenObjectIds: true }): ObjectIdToString<FlattenMaps<T>>;
toJSON<T = Require_id<DocType>>(options: ToObjectOptions & { flattenMaps: false }): T;
toJSON<T = Require_id<DocType>>(options: ToObjectOptions & { flattenMaps: false; flattenObjectIds: true }): ObjectIdToString<T>;
toJSON(options?: ToObjectOptions & { flattenMaps?: true, flattenObjectIds?: false }): FlattenMaps<Default__v<Require_id<DocType>>>;
toJSON(options: ToObjectOptions & { flattenObjectIds: false }): FlattenMaps<Default__v<Require_id<DocType>>>;
toJSON(options: ToObjectOptions & { flattenObjectIds: true }): ObjectIdToString<FlattenMaps<Default__v<Require_id<DocType>>>>;
toJSON(options: ToObjectOptions & { flattenMaps: false }): Default__v<Require_id<DocType>>;
toJSON(options: ToObjectOptions & { flattenMaps: false; flattenObjectIds: true }): ObjectIdToString<Default__v<Require_id<DocType>>>;

toJSON<T = Default__v<Require_id<DocType>>>(options?: ToObjectOptions & { flattenMaps?: true, flattenObjectIds?: false }): FlattenMaps<T>;
toJSON<T = Default__v<Require_id<DocType>>>(options: ToObjectOptions & { flattenObjectIds: false }): FlattenMaps<T>;
toJSON<T = Default__v<Require_id<DocType>>>(options: ToObjectOptions & { flattenObjectIds: true }): ObjectIdToString<FlattenMaps<T>>;
toJSON<T = Default__v<Require_id<DocType>>>(options: ToObjectOptions & { flattenMaps: false }): T;
toJSON<T = Default__v<Require_id<DocType>>>(options: ToObjectOptions & { flattenMaps: false; flattenObjectIds: true }): ObjectIdToString<T>;

/** Converts this document into a plain-old JavaScript object ([POJO](https://masteringjs.io/tutorials/fundamentals/pojo)). */
toObject(options?: ToObjectOptions): Require_id<DocType>;
toObject<T>(options?: ToObjectOptions): Require_id<T>;
toObject(options?: ToObjectOptions): Default__v<Require_id<DocType>>;
toObject<T>(options?: ToObjectOptions): Default__v<Require_id<T>>;

/** Clears the modified state on the specified path. */
unmarkModified<T extends keyof DocType>(path: T): void;
Expand Down

0 comments on commit 03407f6

Please sign in to comment.