Skip to content

fix(whatsapp): resolve this.isZero error in list messages by removing destructive JSON cloning#2461

Open
MohaDevPro wants to merge 2 commits intoEvolutionAPI:mainfrom
MohaDevPro:fix-sendlist-iszero
Open

fix(whatsapp): resolve this.isZero error in list messages by removing destructive JSON cloning#2461
MohaDevPro wants to merge 2 commits intoEvolutionAPI:mainfrom
MohaDevPro:fix-sendlist-iszero

Conversation

@MohaDevPro
Copy link

@MohaDevPro MohaDevPro commented Mar 5, 2026

📋 Description

This PR resolves the TypeError: this.isZero is not a function crash 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, the patchMessageBeforeSending hook was using JSON.parse(JSON.stringify(message)) to clone the message payload before modifying the listType. This destructive cloning operation stripped the prototype methods from underlying Protobuf Long objects (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 a Long instance, resulting in a crash.

Fix:
Removed the JSON.parse(JSON.stringify()) calls. The code now directly mutates the listType property 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

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🔧 Refactoring (no functional changes)
  • ⚡ Performance improvement
  • 🧹 Code cleanup
  • 🔒 Security fix

🧪 Testing

  • Manual testing completed
  • Functionality verified in development environment
  • No breaking changes introduced
  • Tested with different connection types (if applicable)

📸 Screenshots (if applicable)

N/A

✅ Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have manually tested my changes thoroughly
  • I have verified the changes work with different scenarios
  • Any dependent changes have been merged and published

📝 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 fix this.isZero by simplifying the logger, but it did not resolve this specific issue because the crash here occurs during the patchMessageBeforeSending Protobuf encoding phase, well before the logger is invoked.

Summary by Sourcery

Bug Fixes:

  • Prevent this.isZero is not a function crashes when sending WhatsApp list messages by mutating listType in place instead of JSON-cloning the message payload.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Mar 5, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Removes destructive JSON cloning in the WhatsApp Baileys patchMessageBeforeSending hook so that list messages adjust their listType in-place without breaking Baileys’ Protobuf Long instances, and tightens one equality check to use strict comparison.

Sequence diagram for WhatsApp list message sending after removing JSON cloning

sequenceDiagram
  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
Loading

Class diagram for BaileysStartupService patchMessageBeforeSending change

classDiagram
  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
Loading

File-Level Changes

Change Details Files
Update WhatsApp Baileys list message patching to mutate listType in-place instead of JSON-cloning the message.
  • Remove JSON.parse(JSON.stringify(message)) calls before modifying listMessage.listType in the patchMessageBeforeSending hook
  • Ensure both deviceSentMessage.message.listMessage and top-level message.listMessage update listType directly to SINGLE_SELECT when originally PRODUCT_LIST
  • Switch loose equality comparison for listMessage.listType to strict equality to avoid unintended type coercion
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Possibly linked issues

  • #(unknown): PR removes harmful JSON cloning that caused this.isZero errors, fixing failing WhatsApp list interactive messages in 2.3.7.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • 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.
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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant