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 <github@almeidx.dev>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Egologics
2025-12-08 17:08:36 -06:00
committed by GitHub
parent d0745afbea
commit fa43c40d34
2 changed files with 12 additions and 1 deletions

View File

@@ -24,6 +24,17 @@ test('AttachmentBuilder stores and exposes file data', () => {
expect(attachment.getRawFile()).toBe(undefined); 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', () => { test('MessageBuilder.toFileBody returns JSON body and files', () => {
const msg = new MessageBuilder().setContent('here is a file').addAttachments( const msg = new MessageBuilder().setContent('here is a file').addAttachments(
new AttachmentBuilder() new AttachmentBuilder()

View File

@@ -153,7 +153,7 @@ export class AttachmentBuilder implements JSONEncodable<RESTAPIAttachment> {
return { return {
...this.fileData, ...this.fileData,
name: this.data.filename, name: this.data.filename,
key: this.data.id ? `files[${this.data.id}]` : undefined, key: this.data.id === undefined ? undefined : `files[${this.data.id}]`,
}; };
} }