fix(onebot): accept empty text placeholders in arrays

Fixes #273.
This commit is contained in:
DrSmoothl
2026-07-23 12:08:17 +08:00
parent 120dda1d9d
commit a7b9ba14fb
2 changed files with 68 additions and 0 deletions
+4
View File
@@ -408,6 +408,10 @@ export async function parseMessage(message: JsonValue, autoEscape: boolean, opti
const elements: MessageElement[] = [];
for (const seg of message) {
const data = segmentPayload(seg);
// Some OneBot clients insert empty text between media as a separator.
// Only the exact empty string is a no-op; malformed and whitespace text
// must still pass through strict element validation.
if (seg.type.trim().toLowerCase() === 'text' && data.text === '') continue;
const elem = await segmentToElement(seg.type, data, options);
if (elem) elements.push(elem);
}
@@ -91,6 +91,70 @@ describe('parseMessage', () => {
expect(result).toEqual([{ type: 'text', text: 'hello' }]);
});
it('ignores explicit empty text placeholders between sendable segments (issue #273)', async () => {
const result = await parseMessage(
[
{ type: 'image', data: { file: 'https://example.com/first.png' } },
{ type: 'text', data: { text: '' } },
{ type: 'image', data: { file: 'https://example.com/second.png' } },
] as any,
false,
);
expect(result).toHaveLength(2);
expect(result).toMatchObject([
{ type: 'image', url: 'https://example.com/first.png' },
{ type: 'image', url: 'https://example.com/second.png' },
]);
});
it('rejects a segment array containing only empty text placeholders', async () => {
await expect(parseMessage(
[
{ type: 'text', data: { text: '' } },
{ type: 'text', data: { text: '' } },
] as any,
false,
)).rejects.toMatchObject({
code: 'MISSING_FIELD',
field: 'message',
});
});
it('keeps a single empty text segment invalid', async () => {
await expect(parseMessage(
{ type: 'text', data: { text: '' } } as any,
false,
)).rejects.toMatchObject({
code: 'MISSING_FIELD',
elementType: 'text',
});
});
it('does not treat missing or null text fields as empty placeholders', async () => {
await expect(parseMessage(
[{ type: 'text', data: {} }] as any,
false,
)).rejects.toMatchObject({
code: 'MISSING_FIELD',
elementType: 'text',
});
await expect(parseMessage(
[{ type: 'text', data: { text: null } }] as any,
false,
)).rejects.toMatchObject({
code: 'MISSING_FIELD',
elementType: 'text',
});
});
it('preserves whitespace-only text segments', async () => {
await expect(parseMessage(
[{ type: 'text', data: { text: ' ' } }] as any,
false,
)).resolves.toEqual([{ type: 'text', text: ' ' }]);
});
it('rejects object-valued fields instead of stringifying caller input', async () => {
await expect(parseMessage(
[{ type: 'text', data: { text: { bad: true } } }] as any,