Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk): encode empty Go webhook filters as an array

Yudhi Armyndharis05115735399c
diff --git a/sdk/go/types_webhook.go b/sdk/go/types_webhook.go
index ced4070ad..5bf19c288 100644
--- a/sdk/go/types_webhook.go
+++ b/sdk/go/types_webhook.go
@@ -86,6 +86,19 @@ type WebhookFilters struct {
Conditions []WebhookFilterCondition `json:"conditions"`
}
+// MarshalJSON encodes a nil Conditions as `[]` rather than `null`.
+//
+// The gateway requires conditions to be an array and answers 400 for `null`, so the zero value
+// &WebhookFilters{} could not express the documented empty filter `{ "conditions": [] }`.
+func (f WebhookFilters) MarshalJSON() ([]byte, error) {
+ type alias WebhookFilters
+ out := alias(f)
+ if out.Conditions == nil {
+ out.Conditions = []WebhookFilterCondition{}
+ }
+ return json.Marshal(out)
+}
+
// CreateWebhookRequest registers a webhook. RetryCount is 0–5 (default 3).
//
// Secret is optional and signs every delivery as X-OpenWA-Signature: sha256=<hex>. The gateway
diff --git a/sdk/go/webhook_clear_test.go b/sdk/go/webhook_clear_test.go
index 246742a16..1c337a2d9 100644
--- a/sdk/go/webhook_clear_test.go
+++ b/sdk/go/webhook_clear_test.go
@@ -67,3 +67,25 @@ func TestUpdateTemplateRequestTransmitsEmptyHeaderAndFooter(t *testing.T) {
}
}
}
+
+// A nil Conditions slice encoded as `"conditions": null`, which the gateway rejects with a 400
+// ("filters.conditions must be an array"), so the zero value &WebhookFilters{} could not express the
+// documented empty filter `{ "conditions": [] }` on either create or update.
+func TestWebhookFiltersZeroValueEncodesEmptyConditions(t *testing.T) {
+ for name, req := range map[string]any{
+ "create": CreateWebhookRequest{URL: "https://example.com/hook", Events: []string{"message.received"}, Filters: &WebhookFilters{}},
+ "update": UpdateWebhookRequest{Filters: &WebhookFilters{}},
+ } {
+ body, err := json.Marshal(req)
+ if err != nil {
+ t.Fatalf("%s: marshal: %v", name, err)
+ }
+ var got map[string]json.RawMessage
+ if err := json.Unmarshal(body, &got); err != nil {
+ t.Fatalf("%s: unmarshal: %v", name, err)
+ }
+ if v := string(got["filters"]); v != `{"conditions":[]}` {
+ t.Errorf("%s: filters should marshal to {\"conditions\":[]}, got %s", name, v)
+ }
+ }
+}