diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go index 6df9ca2575e..5ec008d4bc6 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go @@ -29,8 +29,6 @@ import ( "fmt" "strings" - "github.com/ugorji/go/codec" - "k8s.io/apimachinery/pkg/types" ) @@ -445,27 +443,6 @@ func (vs Verbs) String() string { return fmt.Sprintf("%v", []string(vs)) } -// CodecEncodeSelf is part of the codec.Selfer interface. -func (vs *Verbs) CodecEncodeSelf(encoder *codec.Encoder) { - encoder.Encode(vs) -} - -// CodecDecodeSelf is part of the codec.Selfer interface. It is overwritten here to make sure -// that an empty verbs list is not decoded as nil. On the other hand, an undefined verbs list -// will lead to nil because this decoding for Verbs is not invoked. -// -// TODO(sttts): this is due to a ugorji regression: https://github.com/ugorji/go/issues/119. Remove the -// workaround when the regression is fixed. -func (vs *Verbs) CodecDecodeSelf(decoder *codec.Decoder) { - m := []string{} - decoder.Decode(&m) - if len(m) == 0 { - *vs = []string{} - } else { - *vs = m - } -} - // APIResourceList is a list of APIResource, it is used to expose the name of the // resources supported in a specific group and version, and if the resource // is namespaced. diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types_test.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types_test.go index 540ba7131e5..cbd50009acf 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types_test.go @@ -24,7 +24,7 @@ import ( "github.com/ugorji/go/codec" ) -func TestVerbsMarshalJSON(t *testing.T) { +func TestVerbsUgorjiMarshalJSON(t *testing.T) { cases := []struct { input APIResource result string @@ -40,12 +40,12 @@ func TestVerbsMarshalJSON(t *testing.T) { t.Errorf("[%d] Failed to marshal input: '%v': %v", i, c.input, err) } if string(result) != c.result { - t.Errorf("[%d] Failed to marshal input: '%v': expected %+v, got %q", i, c.input, c.result, string(result)) + t.Errorf("[%d] Failed to marshal input: '%v': expected '%v', got '%v'", i, c.input, c.result, string(result)) } } } -func TestVerbsUnmarshalJSON(t *testing.T) { +func TestVerbsUgorjiUnmarshalJSON(t *testing.T) { cases := []struct { input string result APIResource @@ -67,7 +67,29 @@ func TestVerbsUnmarshalJSON(t *testing.T) { } } -func TestVerbsUgorjiUnmarshalJSON(t *testing.T) { +// TestUgorjiMarshalJSONWithOmit tests that we don't have regressions regarding nil and empty slices with "omit" +func TestUgorjiMarshalJSONWithOmit(t *testing.T) { + cases := []struct { + input LabelSelector + result string + }{ + {LabelSelector{}, `{}`}, + {LabelSelector{MatchExpressions: []LabelSelectorRequirement{}}, `{}`}, + {LabelSelector{MatchExpressions: []LabelSelectorRequirement{{}}}, `{"matchExpressions":[{"key":"","operator":""}]}`}, + } + + for i, c := range cases { + result, err := json.Marshal(&c.input) + if err != nil { + t.Errorf("[%d] Failed to marshal input: '%v': %v", i, c.input, err) + } + if string(result) != c.result { + t.Errorf("[%d] Failed to marshal input: '%v': expected '%v', got '%v'", i, c.input, c.result, string(result)) + } + } +} + +func TestVerbsUnmarshalJSON(t *testing.T) { cases := []struct { input string result APIResource