From 030bbf03c7b27401443494161d036a0a546a97b4 Mon Sep 17 00:00:00 2001 From: Vikram Singh Date: Fri, 27 Nov 2026 13:13:14 +0530 Subject: [PATCH] fix: Send a null property so percentage-split segments can be created segment create/update dropped an empty "property" from conditions. The Admin API answers a PERCENTAGE_SPLIT condition without that key with a 500, so a percentage rollout segment could not be created from the CLI. Conditions now always send "property", as null when empty, which is how the API stores it. --- internal/api/client.go | 14 ++++++++++++++ internal/cmd/cmd_test.go | 17 +++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/internal/api/client.go b/internal/api/client.go index c815a74..65699e8 100644 --- a/internal/api/client.go +++ b/internal/api/client.go @@ -1256,6 +1256,20 @@ type SegmentCondition struct { Value any `json:"value"` } +// MarshalJSON always sends `property`, as null when empty: the Admin API rejects +// a PERCENTAGE_SPLIT condition that omits the key, and stores no property as null. +func (c SegmentCondition) MarshalJSON() ([]byte, error) { + var property any + if c.Property != "" { + property = c.Property + } + return json.Marshal(struct { + Property any `json:"property"` + Operator string `json:"operator"` + Value any `json:"value"` + }{property, c.Operator, c.Value}) +} + // SegmentRule is a node in a segment's rule tree (ALL/ANY/NONE over conditions // and sub-rules). type SegmentRule struct { diff --git a/internal/cmd/cmd_test.go b/internal/cmd/cmd_test.go index 8e6b338..26ff6b5 100644 --- a/internal/cmd/cmd_test.go +++ b/internal/cmd/cmd_test.go @@ -5976,6 +5976,23 @@ func TestSegmentCreate(t *testing.T) { } }) + t.Run("sends a null property for a percentage split", func(t *testing.T) { + f := flagUpdateEnv(t) + rule := `{"type":"ALL","rules":[{"type":"ANY","conditions":[{"operator":"PERCENTAGE_SPLIT","value":"25"}]}]}` + if out, err := run("", "segment", "create", "rollout", "--rules", rule); err != nil { + t.Fatalf("segment create: %v\noutput: %s", err, out) + } + top := f.lastSegmentBody["rules"].([]any)[0].(map[string]any) + cond := top["rules"].([]any)[0].(map[string]any)["conditions"].([]any)[0].(map[string]any) + property, present := cond["property"] + if !present || property != nil { + t.Errorf("property = %v (present %v), want null on the wire", property, present) + } + if cond["operator"] != "PERCENTAGE_SPLIT" || cond["value"] != "25" { + t.Errorf("condition = %v", cond) + } + }) + t.Run("requires --rules", func(t *testing.T) { flagUpdateEnv(t) _, err := run("", "segment", "create", "x")