Explorar o código

Merge pull request #44717 from neersighted/backport/44668/23.0

[23.0 backport] api: can marshal and unmarshal when args.fields is empty
Bjorn Neergaard %!s(int64=2) %!d(string=hai) anos
pai
achega
a166d959a0
Modificáronse 2 ficheiros con 22 adicións e 4 borrados
  1. 1 4
      api/types/filters/parse.go
  2. 21 0
      api/types/filters/parse_test.go

+ 1 - 4
api/types/filters/parse.go

@@ -50,7 +50,7 @@ func (args Args) Keys() []string {
 // MarshalJSON returns a JSON byte representation of the Args
 // MarshalJSON returns a JSON byte representation of the Args
 func (args Args) MarshalJSON() ([]byte, error) {
 func (args Args) MarshalJSON() ([]byte, error) {
 	if len(args.fields) == 0 {
 	if len(args.fields) == 0 {
-		return []byte{}, nil
+		return []byte("{}"), nil
 	}
 	}
 	return json.Marshal(args.fields)
 	return json.Marshal(args.fields)
 }
 }
@@ -108,9 +108,6 @@ func FromJSON(p string) (Args, error) {
 
 
 // UnmarshalJSON populates the Args from JSON encode bytes
 // UnmarshalJSON populates the Args from JSON encode bytes
 func (args Args) UnmarshalJSON(raw []byte) error {
 func (args Args) UnmarshalJSON(raw []byte) error {
-	if len(raw) == 0 {
-		return nil
-	}
 	return json.Unmarshal(raw, &args.fields)
 	return json.Unmarshal(raw, &args.fields)
 }
 }
 
 

+ 21 - 0
api/types/filters/parse_test.go

@@ -1,6 +1,7 @@
 package filters // import "github.com/docker/docker/api/types/filters"
 package filters // import "github.com/docker/docker/api/types/filters"
 
 
 import (
 import (
+	"encoding/json"
 	"errors"
 	"errors"
 	"testing"
 	"testing"
 
 
@@ -8,6 +9,26 @@ import (
 	is "gotest.tools/v3/assert/cmp"
 	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) {
 func TestToJSON(t *testing.T) {
 	fields := map[string]map[string]bool{
 	fields := map[string]map[string]bool{
 		"created":    {"today": true},
 		"created":    {"today": true},