diff --git a/server/api/public/comments-create-body.test.ts b/server/api/public/comments-create-body.test.ts index 1051403..8119aeb 100644 --- a/server/api/public/comments-create-body.test.ts +++ b/server/api/public/comments-create-body.test.ts @@ -25,6 +25,21 @@ describe("parsePublicPostCreateCommentBody", () => { body: "hello", }); }); + + test("keeps guestIsAnonymous as undefined when field is missing", () => { + const parsed = parsePublicPostCreateCommentBody({ + guestDisplayName: "访客C", + guestEmail: undefined, + body: "legacy", + }); + expect(parsed).toEqual({ + parentId: null, + guestDisplayName: "访客C", + guestEmail: undefined, + guestIsAnonymous: undefined, + body: "legacy", + }); + }); }); describe("parseUnlistedCreateCommentBody", () => { diff --git a/server/api/public/comments-create-body.ts b/server/api/public/comments-create-body.ts index 1b467b8..824eec4 100644 --- a/server/api/public/comments-create-body.ts +++ b/server/api/public/comments-create-body.ts @@ -10,7 +10,7 @@ export type ParsedCreateCommentInput = { parentId: number | null; guestDisplayName?: string; guestEmail?: string; - guestIsAnonymous: boolean; + guestIsAnonymous?: boolean; body: string; }; @@ -22,7 +22,7 @@ export function parsePublicPostCreateCommentBody(body: CreateCommentRequestBody) parentId: parseParentId(body.parentId), guestDisplayName: typeof body.guestDisplayName === "string" ? body.guestDisplayName : undefined, guestEmail: typeof body.guestEmail === "string" ? body.guestEmail : undefined, - guestIsAnonymous: body.guestIsAnonymous === true, + guestIsAnonymous: typeof body.guestIsAnonymous === "boolean" ? body.guestIsAnonymous : undefined, body: body.body, }; } @@ -35,7 +35,7 @@ export function parseUnlistedCreateCommentBody(body: CreateCommentRequestBody): parentId: parseParentId(body.parentId), guestDisplayName: typeof body.guestDisplayName === "string" ? body.guestDisplayName : undefined, guestEmail: typeof body.guestEmail === "string" ? body.guestEmail : undefined, - guestIsAnonymous: body.guestIsAnonymous === true, + guestIsAnonymous: typeof body.guestIsAnonymous === "boolean" ? body.guestIsAnonymous : undefined, body: body.body, }; }