Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(sdk-go): let the Go client send the values that clear a field
Yudhi Armyndharis6c5faaa82ec5
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e1ea573aa..4599b4ac2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- The Go SDK can now clear a webhook's `secret`, `headers` and `filters`, and a template's `header` and `footer`. Every field carried `omitempty` over a value type, so the values the server reads as "remove this" marshalled away to nothing: the request body came out `{}` and the stored values survived while the call reported success. Secret, headers, header and footer are now pointers, and a new `ClearFilters` flag emits the explicit `null` the server reads — which no struct tag can express for a nil pointer, and which dropping `omitempty` would have sent on every update instead.+- The addressbook write guard now checks the id, not just its domain. `parseWaId('NOT A [email protected]').kind` is `'user'`, so free text, letters and an empty user-part cleared the guard, reached the engine and were reported to the caller as a saved contact — for an entry keyed by something that is not a phone number. It now applies `isIndividualWid`, the same predicate the group-participant, channel-admin and message-mention surfaces already use on these very shapes.- `POST /api/infra/import-data` now answers 400 for a malformed archive instead of 500. A table value that is not an array — a hand-edited or truncated backup — was dereferenced with `.map()` before anything checked it, so the operator was told the server had broken when their file was simply wrong. Checked for every table, not only `sessions`: the rest are read inside the transaction, where the same mistake would fail mid-restore rather than before it opens.diff --git a/sdk/go/types_misc.go b/sdk/go/types_misc.goindex b23900a3b..bc2953422 100644--- a/sdk/go/types_misc.go+++ b/sdk/go/types_misc.go@@ -53,11 +53,15 @@ type CreateTemplateRequest struct {}// UpdateTemplateRequest updates a template; all fields optional.+//+// Header and Footer are pointers because an empty string is how the server is told to REMOVE them.+// As value types with `omitempty` they marshalled away, so a Go caller could never clear a header+// once set — every rendered message kept prepending the old text while the update reported success.type UpdateTemplateRequest struct {- Name string `json:"name,omitempty"`- Body string `json:"body,omitempty"`- Header string `json:"header,omitempty"`- Footer string `json:"footer,omitempty"`+ Name string `json:"name,omitempty"`+ Body string `json:"body,omitempty"`+ Header *string `json:"header,omitempty"`+ Footer *string `json:"footer,omitempty"`}// ── Label (WhatsApp Business) ────────────────────────────────diff --git a/sdk/go/types_webhook.go b/sdk/go/types_webhook.goindex 688cee787..991d4e429 100644--- a/sdk/go/types_webhook.go+++ b/sdk/go/types_webhook.go@@ -1,5 +1,7 @@package openwa+import "encoding/json"+// WebhookEvent names an event a webhook may subscribe to. It is an alias for// string so the Event* constants drop straight into the []string Events fields// of CreateWebhookRequest / UpdateWebhookRequest without a conversion. Use@@ -95,14 +97,46 @@ type CreateWebhookRequest struct {}// UpdateWebhookRequest updates a webhook; all fields optional.+//+// The server distinguishes an ABSENT field (leave it alone) from an explicit empty or null value+// (clear it). Secret and Headers are therefore pointers: with a value type, `omitempty` dropped an+// empty string and an empty map from the body, so the two documented "clear this" values could not+// be expressed at all and the stored values survived an update that meant to remove them.+//+// Filters cannot use the same trick — a nil pointer marshals away under omitempty, and dropping+// omitempty would send "filters": null on EVERY update, clearing filters nobody asked to touch. Set+// ClearFilters instead; MarshalJSON turns it into the explicit null the server reads.type UpdateWebhookRequest struct {- URL string `json:"url,omitempty"`- Events []string `json:"events,omitempty"`- Secret string `json:"secret,omitempty"`- Headers map[string]string `json:"headers,omitempty"`- Filters *WebhookFilters `json:"filters,omitempty"`- RetryCount *int `json:"retryCount,omitempty"`- Active *bool `json:"active,omitempty"`+ URL string `json:"url,omitempty"`+ Events []string `json:"events,omitempty"`+ Secret *string `json:"secret,omitempty"`+ Headers *map[string]string `json:"headers,omitempty"`+ Filters *WebhookFilters `json:"filters,omitempty"`+ RetryCount *int `json:"retryCount,omitempty"`+ Active *bool `json:"active,omitempty"`++ // ClearFilters removes any stored filters. Ignored when Filters is set — setting and clearing+ // the same field in one request is a caller error, and setting is the more specific intent.+ ClearFilters bool `json:"-"`+}++// MarshalJSON emits the explicit `"filters": null` the server reads as "remove the filters", which+// no combination of struct tags can express for a nil pointer.+func (r UpdateWebhookRequest) MarshalJSON() ([]byte, error) {+ type payload UpdateWebhookRequest // alias sheds this method, so json.Marshal does not recurse+ encoded, err := json.Marshal(payload(r))+ if err != nil {+ return nil, err+ }+ if !r.ClearFilters || r.Filters != nil {+ return encoded, nil+ }+ var fields map[string]json.RawMessage+ if err := json.Unmarshal(encoded, &fields); err != nil {+ return nil, err+ }+ fields["filters"] = json.RawMessage("null")+ return json.Marshal(fields)}// WebhookResponse is a stored webhook (secret/headers are omitted on reads).diff --git a/sdk/go/webhook_clear_test.go b/sdk/go/webhook_clear_test.gonew file mode 100644index 000000000..246742a16--- /dev/null+++ b/sdk/go/webhook_clear_test.go@@ -0,0 +1,69 @@+package openwa++import (+ "encoding/json"+ "testing"+)++// The server distinguishes ABSENT (leave the field alone) from an explicit empty/null value (clear+// it): webhook.service.ts applies `if (dto.filters !== undefined)`, and reads an empty secret and an+// empty headers map as "clear". Every field on UpdateWebhookRequest carried `omitempty` over a value+// type, so the three "clear this" values marshalled away to nothing and the stored values survived —+// while the JavaScript, Python, PHP and (for secret/headers) Java clients transmit them and the+// server acts on them.+func TestUpdateWebhookRequestTransmitsClearingValues(t *testing.T) {+ empty := ""+ noHeaders := map[string]string{}+ body, err := json.Marshal(UpdateWebhookRequest{+ Secret: &empty,+ Headers: &noHeaders,+ ClearFilters: true,+ })+ if err != nil {+ t.Fatalf("marshal: %v", err)+ }++ var got map[string]json.RawMessage+ if err := json.Unmarshal(body, &got); err != nil {+ t.Fatalf("unmarshal: %v", err)+ }+ if _, ok := got["secret"]; !ok {+ t.Errorf("secret was dropped from the body: %s", body)+ }+ if _, ok := got["headers"]; !ok {+ t.Errorf("headers was dropped from the body: %s", body)+ }+ if v, ok := got["filters"]; !ok || string(v) != "null" {+ t.Errorf("filters should marshal to an explicit null, got %q in %s", string(v), body)+ }+}++// Negative twin: a request that sets nothing must stay empty, or every update would clear the+// fields it did not mention.+func TestUpdateWebhookRequestOmitsUnsetFields(t *testing.T) {+ body, err := json.Marshal(UpdateWebhookRequest{})+ if err != nil {+ t.Fatalf("marshal: %v", err)+ }+ if string(body) != "{}" {+ t.Errorf("an empty update must marshal to {}, got %s", body)+ }+}++// The same shape for templates: header and footer are cleared with an empty string.+func TestUpdateTemplateRequestTransmitsEmptyHeaderAndFooter(t *testing.T) {+ empty := ""+ body, err := json.Marshal(UpdateTemplateRequest{Header: &empty, Footer: &empty})+ if err != nil {+ t.Fatalf("marshal: %v", err)+ }+ var got map[string]json.RawMessage+ if err := json.Unmarshal(body, &got); err != nil {+ t.Fatalf("unmarshal: %v", err)+ }+ for _, field := range []string{"header", "footer"} {+ if v, ok := got[field]; !ok || string(v) != `""` {+ t.Errorf("%s should marshal to an empty string, got %q in %s", field, string(v), body)+ }+ }+}