diff --git a/model/criteria/criteria_test.go b/model/criteria/criteria_test.go index de5124568..5a8ebaa5c 100644 --- a/model/criteria/criteria_test.go +++ b/model/criteria/criteria_test.go @@ -423,4 +423,16 @@ var _ = Describe("Criteria", func() { gomega.Expect(ids).To(gomega.Equal([]string{sharedID})) }) }) + + DescribeTable("empty nested groups", + func(input, expected string) { + var c Criteria + gomega.Expect(json.Unmarshal([]byte(input), &c)).To(gomega.Succeed()) + out, err := json.Marshal(c) + gomega.Expect(err).ToNot(gomega.HaveOccurred()) + gomega.Expect(string(out)).To(gomega.Equal(expected)) + }, + Entry("keeps a nested empty group", `{"all":[{"all":[]}]}`, `{"all":[{"all":[]}]}`), + Entry("keeps a nested empty any group", `{"all":[{"any":[]}]}`, `{"all":[{"any":[]}]}`), + ) }) diff --git a/model/criteria/json.go b/model/criteria/json.go index d0f453524..87f768298 100644 --- a/model/criteria/json.go +++ b/model/criteria/json.go @@ -174,14 +174,8 @@ func marshalExpression(name string, value map[string]any) ([]byte, error) { } func marshalConjunction(name string, conj []Expression) ([]byte, error) { - aux := struct { - All []Expression `json:"all,omitempty"` - Any []Expression `json:"any,omitempty"` - }{} - if name == "any" { - aux.Any = conj - } else { - aux.All = conj + if conj == nil { + conj = []Expression{} } - return json.Marshal(aux) + return json.Marshal(map[string][]Expression{name: conj}) }