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")