From fa43c40d34c779b5eca71573a257e2e346a6a071 Mon Sep 17 00:00:00 2001 From: Egologics Date: Mon, 8 Dec 2025 17:08:36 -0600 Subject: [PATCH] fix(builders): handle 0 as valid attachment ID (#11330) * fix(builders): handle 0 as valid attachment ID Fixes #11314 The AttachmentBuilder.getRawFile() method was using a falsy check that treated 0 as invalid. Changed to use the 'in' operator to properly check for the existence of the id property. * Update packages/builders/__tests__/messages/fileBody.test.ts Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> * Apply suggestion from @almeidx * fix: lint --------- Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> Co-authored-by: Almeida Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- packages/builders/__tests__/messages/fileBody.test.ts | 11 +++++++++++ packages/builders/src/messages/Attachment.ts | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/builders/__tests__/messages/fileBody.test.ts b/packages/builders/__tests__/messages/fileBody.test.ts index 4af96a382..3f46707e0 100644 --- a/packages/builders/__tests__/messages/fileBody.test.ts +++ b/packages/builders/__tests__/messages/fileBody.test.ts @@ -24,6 +24,17 @@ test('AttachmentBuilder stores and exposes file data', () => { expect(attachment.getRawFile()).toBe(undefined); }); +test('AttachmentBuilder handles 0 as a valid id', () => { + const data = Buffer.from('test data'); + const attachment = new AttachmentBuilder().setId(0).setFilename('test.txt').setFileData(data); + + expect(attachment.getRawFile()).toStrictEqual({ + data, + key: 'files[0]', + name: 'test.txt', + }); +}); + test('MessageBuilder.toFileBody returns JSON body and files', () => { const msg = new MessageBuilder().setContent('here is a file').addAttachments( new AttachmentBuilder() diff --git a/packages/builders/src/messages/Attachment.ts b/packages/builders/src/messages/Attachment.ts index 4e12e9d21..0c9b2494c 100644 --- a/packages/builders/src/messages/Attachment.ts +++ b/packages/builders/src/messages/Attachment.ts @@ -153,7 +153,7 @@ export class AttachmentBuilder implements JSONEncodable { return { ...this.fileData, name: this.data.filename, - key: this.data.id ? `files[${this.data.id}]` : undefined, + key: this.data.id === undefined ? undefined : `files[${this.data.id}]`, }; }