Skip to content

Commit cf2f172

Browse files
committed
fix(gist): set gist subpaths individually to avoid CastError
Mongoose treats `gist` as a nested path, not a sub-schema, so set("gist", payload) mis-casts the inner subdoc arrays and fails validation with 'Cast to [string] failed' at gist.files.0. Set each subpath individually so the files/comments arrays cast correctly.
1 parent dcb524c commit cf2f172

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

src/core/Gist.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,6 @@ export default class Gist {
9999
author: comment.user?.login || "",
100100
}));
101101

102-
// Mongoose treats `gist` as a nested path; assigning a plain object that
103-
// contains nested arrays (files/comments) on an unsaved doc silently drops
104-
// those arrays. Cache the populated payload off-model so toJSON can read
105-
// it directly, and also set it on the model for the persisted path.
106102
const payload = {
107103
description: gistInfo.data.description || "",
108104
isPublic: gistInfo.data.public,
@@ -117,7 +113,16 @@ export default class Gist {
117113
comments: commentsMapped,
118114
};
119115
this._gistPayload = payload;
120-
this._model.set("gist", payload);
116+
// `gist` is a nested path (not a sub-schema), so assigning the whole
117+
// object via set("gist", payload) mis-casts the inner subdoc arrays.
118+
// Set each sub-path individually so Mongoose casts arrays correctly.
119+
this._model.set("gist.description", payload.description);
120+
this._model.set("gist.isPublic", payload.isPublic);
121+
this._model.set("gist.creationDate", payload.creationDate);
122+
this._model.set("gist.updatedDate", payload.updatedDate);
123+
this._model.set("gist.ownerLogin", payload.ownerLogin);
124+
this._model.set("gist.files", files);
125+
this._model.set("gist.comments", commentsMapped);
121126
this._model.markModified("gist");
122127
}
123128

0 commit comments

Comments
 (0)