fix(onebot): clarify invalid JSON segment errors

Validate the public OneBot data.data field before converting JSON segments to the internal message element shape. Invalid payloads still return retcode 1400, but now identify the correct public field without reaching the QQ bridge.

Add Action-level regression coverage for issue #220.
This commit is contained in:
DrSmoothl committed 2026-07-12 16:43:05 +08:00
1 parent a824e0b46a
commit 2ae9a47249
2 files changed
+34 -1

No files matched your search

@@ -292,9 +292,18 @@ export const ELEMENT_CODECS = {
return { type: 'json', data: { data: element.text ?? '' } };
},
async fromSegment(data) {
const text = data.data;
if (typeof text !== 'string' || !text.trim()) {
throw new MessageElementValidationError(
'INVALID_FIELD',
'message segment "json" field "data" must be a non-empty JSON string',
'json',
'data',
);
}
return {
type: 'json',
text: String(data.data ?? ''),
text,
};
},
},
@@ -70,4 +70,28 @@ describe('outbound message validation at the Action boundary', () => {
});
expect(bridgeSend).not.toHaveBeenCalled();
});
it('reports the public data field when a JSON segment uses data.text', async () => {
const bridgeSend = vi.fn();
const ref = instanceContext(bridgeSend);
const api = new ApiHandler({
sendGroupMessage: (groupId, message, autoEscape) =>
sendGroupMessage(ref, groupId, message, autoEscape),
} as ApiActionContext);
const response = await api.handle('send_group_msg', {
group_id: 12345,
message: [{
type: 'json',
data: { text: '{"app":"com.tencent.contact.lua"}' },
}],
});
expect(response).toMatchObject({
status: 'failed',
retcode: 1400,
wording: 'message segment "json" field "data" must be a non-empty JSON string',
});
expect(bridgeSend).not.toHaveBeenCalled();
});
});