Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions internal/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down