fix(whatsapp): resolve this.isZero error in list messages by removing destructive JSON cloning#2461
Open
MohaDevPro wants to merge 2 commits intoEvolutionAPI:mainfrom
Open
fix(whatsapp): resolve this.isZero error in list messages by removing destructive JSON cloning#2461MohaDevPro wants to merge 2 commits intoEvolutionAPI:mainfrom
MohaDevPro wants to merge 2 commits intoEvolutionAPI:mainfrom
Conversation
… destructive JSON cloning
Contributor
Reviewer's guide (collapsed on small PRs)Reviewer's GuideRemoves destructive JSON cloning in the WhatsApp Baileys Sequence diagram for WhatsApp list message sending after removing JSON cloningsequenceDiagram
actor Client
participant ApiServer
participant BaileysStartupService
participant BaileysEncoder
Client->>ApiServer: POST /message/sendList
ApiServer->>BaileysStartupService: patchMessageBeforeSending(message)
BaileysStartupService->>BaileysStartupService: check message.deviceSentMessage.message.listMessage.listType
alt deviceSentMessage listType is PRODUCT_LIST
BaileysStartupService->>BaileysStartupService: mutate listType to SINGLE_SELECT (in place)
end
BaileysStartupService->>BaileysStartupService: check message.listMessage.listType
alt message listType is PRODUCT_LIST
BaileysStartupService->>BaileysStartupService: mutate listType to SINGLE_SELECT (in place)
end
BaileysStartupService-->>ApiServer: patched message (Long instances preserved)
ApiServer->>BaileysEncoder: encode(message)
BaileysEncoder->>BaileysEncoder: call Long.isZero() on timestamps and ids
BaileysEncoder-->>ApiServer: encoded payload
ApiServer-->>Client: success response
Class diagram for BaileysStartupService patchMessageBeforeSending changeclassDiagram
class BaileysStartupService {
+patchMessageBeforeSending(message)
}
class Message {
+DeviceSentMessage deviceSentMessage
+ListMessage listMessage
}
class DeviceSentMessage {
+InnerMessage message
}
class InnerMessage {
+ListMessage listMessage
}
class ListMessage {
+ListType listType
}
class ListType {
<<enumeration>>
+PRODUCT_LIST
+SINGLE_SELECT
}
BaileysStartupService ..> Message : patches
Message o-- DeviceSentMessage
Message o-- ListMessage
DeviceSentMessage o-- InnerMessage
InnerMessage o-- ListMessage
ListMessage --> ListType
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- Since this change relies on mutating the original
messageobject instead of cloning, double-check that no callers assumepatchMessageBeforeSendingis side-effect free; if they do, you may want to document or refactor those call sites. - Consider adding a brief inline comment near the removed
JSON.parse(JSON.stringify(message))explaining that cloning breaks BaileysLonginstances, so future refactors don’t reintroduce a similar pattern.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Since this change relies on mutating the original `message` object instead of cloning, double-check that no callers assume `patchMessageBeforeSending` is side-effect free; if they do, you may want to document or refactor those call sites.
- Consider adding a brief inline comment near the removed `JSON.parse(JSON.stringify(message))` explaining that cloning breaks Baileys `Long` instances, so future refactors don’t reintroduce a similar pattern.
## Individual Comments
### Comment 1
<location path="src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts" line_range="684" />
<code_context>
- if (message.listMessage?.listType == proto.Message.ListMessage.ListType.PRODUCT_LIST) {
- message = JSON.parse(JSON.stringify(message));
-
+ if (message.listMessage?.listType === proto.Message.ListMessage.ListType.PRODUCT_LIST) {
message.listMessage.listType = proto.Message.ListMessage.ListType.SINGLE_SELECT;
}
</code_context>
<issue_to_address>
**suggestion:** Consider consolidating the two listType checks into a small helper to avoid divergence.
There are two separate places converting `PRODUCT_LIST` to `SINGLE_SELECT` (for `deviceSentMessage.message.listMessage` and for `message.listMessage`). Extracting a helper like `normalizeListType(listMessage)` and using it in both cases would reduce duplication and keep behavior consistent if we need to update related properties or adapt to future proto/WhatsApp changes.
Suggested implementation:
```typescript
message.deviceSentMessage?.message?.listMessage?.listType === proto.Message.ListMessage.ListType.PRODUCT_LIST
) {
normalizeListType(message.deviceSentMessage.message.listMessage);
}
normalizeListType(message.listMessage);
```
To fully implement this change, you also need to:
1. Define the `normalizeListType` helper in this file (either at module scope or as a private method on the relevant class, depending on existing patterns). For example, at module scope:
```ts
function normalizeListType(listMessage?: proto.Message.IListMessage | null): void {
if (listMessage?.listType === proto.Message.ListMessage.ListType.PRODUCT_LIST) {
listMessage.listType = proto.Message.ListMessage.ListType.SINGLE_SELECT;
}
}
```
or as a method:
```ts
private normalizeListType(listMessage?: proto.Message.IListMessage | null): void {
if (listMessage?.listType === proto.Message.ListMessage.ListType.PRODUCT_LIST) {
listMessage.listType = proto.Message.ListMessage.ListType.SINGLE_SELECT;
}
}
```
2. If you choose the method form, update the usages in the replaced block to `this.normalizeListType(...)` instead of `normalizeListType(...)`.
Place the helper near related WhatsApp/Baileys message-handling utilities to match existing code organization.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Outdated
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📋 Description
This PR resolves the
TypeError: this.isZero is not a functioncrash that occurs when attempting to send list messages via the/message/sendList/endpoint.Root Cause:
Inside
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts, thepatchMessageBeforeSendinghook was usingJSON.parse(JSON.stringify(message))to clone the message payload before modifying thelistType. This destructive cloning operation stripped the prototype methods from underlying ProtobufLongobjects (which Baileys uses for timestamps and message IDs). When Baileys later attempted to encode the message, it called.isZero()on what it expected to be aLonginstance, resulting in a crash.Fix:
Removed the
JSON.parse(JSON.stringify())calls. The code now directly mutates thelistTypeproperty on the existing message object, preserving the prototype chain of all nested Protobuf instances and allowing Baileys to encode the message successfully.🔗 Related Issue
Closes #(issue_number) (Note: Replace with the actual issue number if you opened one, e.g., #2188 or #2351)
🧪 Type of Change
🧪 Testing
📸 Screenshots (if applicable)
N/A
✅ Checklist
📝 Additional Notes
This specifically addresses the remaining
/message/sendList/failures reported in v2.3.6 and v2.3.7. Note that PR #2105 previously attempted to fixthis.isZeroby simplifying the logger, but it did not resolve this specific issue because the crash here occurs during thepatchMessageBeforeSendingProtobuf encoding phase, well before the logger is invoked.Summary by Sourcery
Bug Fixes:
this.isZero is not a functioncrashes when sending WhatsApp list messages by mutating listType in place instead of JSON-cloning the message payload.