diff --git a/api/types/filters/parse.go b/api/types/filters/parse.go index 52c190ec79..f8fe794074 100644 --- a/api/types/filters/parse.go +++ b/api/types/filters/parse.go @@ -50,7 +50,7 @@ func (args Args) Keys() []string { // MarshalJSON returns a JSON byte representation of the Args func (args Args) MarshalJSON() ([]byte, error) { if len(args.fields) == 0 { - return []byte{}, nil + return []byte("{}"), nil } return json.Marshal(args.fields) } @@ -108,9 +108,6 @@ func FromJSON(p string) (Args, error) { // UnmarshalJSON populates the Args from JSON encode bytes func (args Args) UnmarshalJSON(raw []byte) error { - if len(raw) == 0 { - return nil - } return json.Unmarshal(raw, &args.fields) } diff --git a/api/types/filters/parse_test.go b/api/types/filters/parse_test.go index fe7958cf5b..cdbcdfc744 100644 --- a/api/types/filters/parse_test.go +++ b/api/types/filters/parse_test.go @@ -1,6 +1,7 @@ package filters // import "github.com/docker/docker/api/types/filters" import ( + "encoding/json" "errors" "testing" @@ -8,6 +9,26 @@ import ( is "gotest.tools/v3/assert/cmp" ) +func TestMarshalJSON(t *testing.T) { + fields := map[string]map[string]bool{ + "created": {"today": true}, + "image.name": {"ubuntu*": true, "*untu": true}, + } + a := Args{fields: fields} + + _, err := a.MarshalJSON() + if err != nil { + t.Errorf("failed to marshal the filters: %s", err) + } +} + +func TestMarshalJSONWithEmpty(t *testing.T) { + _, err := json.Marshal(NewArgs()) + if err != nil { + t.Errorf("failed to marshal the filters: %s", err) + } +} + func TestToJSON(t *testing.T) { fields := map[string]map[string]bool{ "created": {"today": true},