Merge pull request #44718 from neersighted/backport/44668/20.10

[20.10 backport] api: can marshal and unmarshal when args.fields is empty
This commit is contained in:
Sebastiaan van Stijn 2023-01-02 18:29:02 +01:00 committed by GitHub
commit 6ddb3ba414
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View file

@ -48,7 +48,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)
}
@ -106,9 +106,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)
}

View file

@ -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},