From fd64f8d7efea05db30e1a011c0dffc52c37101ed Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Tue, 14 Sep 2021 21:48:28 -0400 Subject: [PATCH 1/3] Add missing json tag on internal unstructured list --- .../apimachinery/pkg/apis/meta/v1/unstructured/helpers.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers.go index 7b101ea5124..d26c6cff4e6 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/helpers.go @@ -382,7 +382,7 @@ func (unstructuredJSONScheme) Identifier() runtime.Identifier { func (s unstructuredJSONScheme) decode(data []byte) (runtime.Object, error) { type detector struct { - Items gojson.RawMessage + Items gojson.RawMessage `json:"items"` } var det detector if err := json.Unmarshal(data, &det); err != nil { @@ -425,7 +425,7 @@ func (unstructuredJSONScheme) decodeToUnstructured(data []byte, unstruct *Unstru func (s unstructuredJSONScheme) decodeToList(data []byte, list *UnstructuredList) error { type decodeList struct { - Items []gojson.RawMessage + Items []gojson.RawMessage `json:"items"` } var dList decodeList From b4632c38f06583aa9b5d3bf6f47e7c781c3b6e60 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Fri, 15 Oct 2021 10:47:14 -0400 Subject: [PATCH 2/3] Fix strict json decoder test --- .../pkg/runtime/serializer/json/json_test.go | 73 +++++++------------ 1 file changed, 25 insertions(+), 48 deletions(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json_test.go b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json_test.go index ee869206279..22efe4c8ee3 100644 --- a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json_test.go @@ -22,6 +22,7 @@ import ( "strings" "testing" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/serializer/json" @@ -30,11 +31,12 @@ import ( ) type testDecodable struct { + metav1.TypeMeta `json:",inline"` + Other string Value int `json:"value"` Spec DecodableSpec `json:"spec"` Interface interface{} `json:"interface"` - gvk schema.GroupVersionKind } // DecodableSpec has 15 fields. json-iterator treats struct with more than 10 @@ -57,9 +59,6 @@ type DecodableSpec struct { O int `json:"o"` } -func (d *testDecodable) GetObjectKind() schema.ObjectKind { return d } -func (d *testDecodable) SetGroupVersionKind(gvk schema.GroupVersionKind) { d.gvk = gvk } -func (d *testDecodable) GroupVersionKind() schema.GroupVersionKind { return d.gvk } func (d *testDecodable) DeepCopyObject() runtime.Object { if d == nil { return nil @@ -74,7 +73,6 @@ func (d *testDecodable) DeepCopyInto(out *testDecodable) { out.Value = d.Value out.Spec = d.Spec out.Interface = d.Interface - out.gvk = d.gvk return } @@ -121,7 +119,7 @@ func TestDecode(t *testing.T) { data: []byte(`{"apiVersion":"blah"}`), defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, creater: &mockCreater{obj: &testDecodable{}}, - expectedObject: &testDecodable{}, + expectedObject: &testDecodable{TypeMeta: metav1.TypeMeta{APIVersion: "blah"}}, expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "", Version: "blah"}, }, // group without version is defaulted @@ -129,7 +127,7 @@ func TestDecode(t *testing.T) { data: []byte(`{"apiVersion":"other/"}`), defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, creater: &mockCreater{obj: &testDecodable{}}, - expectedObject: &testDecodable{}, + expectedObject: &testDecodable{TypeMeta: metav1.TypeMeta{APIVersion: "other/"}}, expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, }, // group version, kind is defaulted @@ -137,7 +135,7 @@ func TestDecode(t *testing.T) { data: []byte(`{"apiVersion":"other1/blah1"}`), defaultGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, creater: &mockCreater{obj: &testDecodable{}}, - expectedObject: &testDecodable{}, + expectedObject: &testDecodable{TypeMeta: metav1.TypeMeta{APIVersion: "other1/blah1"}}, expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other1", Version: "blah1"}, }, // gvk all provided then not defaulted at all @@ -145,17 +143,17 @@ func TestDecode(t *testing.T) { data: []byte(`{"kind":"Test","apiVersion":"other/blah"}`), defaultGVK: &schema.GroupVersionKind{Kind: "Test1", Group: "other1", Version: "blah1"}, creater: &mockCreater{obj: &testDecodable{}}, - expectedObject: &testDecodable{}, + expectedObject: &testDecodable{TypeMeta: metav1.TypeMeta{APIVersion: "other/blah", Kind: "Test"}}, expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, }, //gvk defaulting if kind not provided in data and defaultGVK use into's kind { data: []byte(`{"apiVersion":"b1/c1"}`), - into: &testDecodable{gvk: schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}}, + into: &testDecodable{TypeMeta: metav1.TypeMeta{Kind: "a3", APIVersion: "b1/c1"}}, typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}}, defaultGVK: nil, creater: &mockCreater{obj: &testDecodable{}}, - expectedObject: &testDecodable{gvk: schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}}, + expectedObject: &testDecodable{TypeMeta: metav1.TypeMeta{Kind: "a3", APIVersion: "b1/c1"}}, expectedGVK: &schema.GroupVersionKind{Kind: "a3", Group: "b1", Version: "c1"}, }, @@ -199,8 +197,9 @@ func TestDecode(t *testing.T) { typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})}, expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, expectedObject: &testDecodable{ - Other: "test", - Value: 1, + TypeMeta: metav1.TypeMeta{APIVersion: "other/blah", Kind: "Test"}, + Other: "test", + Value: 1, }, }, // registered types get defaulted by the into object kind @@ -243,7 +242,8 @@ func TestDecode(t *testing.T) { typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})}, expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, expectedObject: &testDecodable{ - Other: "test", + TypeMeta: metav1.TypeMeta{APIVersion: "other/blah", Kind: "Test"}, + Other: "test", }, }, // Unmarshalling is case-sensitive for big struct. @@ -254,7 +254,8 @@ func TestDecode(t *testing.T) { typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})}, expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, expectedObject: &testDecodable{ - Spec: DecodableSpec{A: 1, H: 3}, + TypeMeta: metav1.TypeMeta{APIVersion: "other/blah", Kind: "Test"}, + Spec: DecodableSpec{A: 1, H: 3}, }, }, // Unknown fields should return an error from the strict JSON deserializer. @@ -275,7 +276,7 @@ func TestDecode(t *testing.T) { typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, errFn: func(err error) bool { - return strings.Contains(err.Error(), "found unknown field") + return strings.Contains(err.Error(), "found unknown field: unknown") }, yaml: true, strict: true, @@ -287,7 +288,7 @@ func TestDecode(t *testing.T) { typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, errFn: func(err error) bool { - return strings.Contains(err.Error(), "already set in map") + return strings.Contains(err.Error(), `"value" already set in map`) }, strict: true, }, @@ -299,33 +300,7 @@ func TestDecode(t *testing.T) { typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, errFn: func(err error) bool { - return strings.Contains(err.Error(), "already set in map") - }, - yaml: true, - strict: true, - }, - // Strict JSON decode should fail for untagged fields. - { - data: []byte(`{"kind":"Test","apiVersion":"other/blah","value":1,"Other":"test"}`), - into: &testDecodable{}, - typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, - expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, - errFn: func(err error) bool { - return strings.Contains(err.Error(), "found unknown field") - }, - strict: true, - }, - // Strict YAML decode should fail for untagged fields. - { - data: []byte("kind: Test\n" + - "apiVersion: other/blah\n" + - "value: 1\n" + - "Other: test\n"), - into: &testDecodable{}, - typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, - expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, - errFn: func(err error) bool { - return strings.Contains(err.Error(), "found unknown field") + return strings.Contains(err.Error(), `"value" already set in map`) }, yaml: true, strict: true, @@ -337,8 +312,9 @@ func TestDecode(t *testing.T) { typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})}, expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, expectedObject: &testDecodable{ - Other: "test", - Value: 1, + TypeMeta: metav1.TypeMeta{APIVersion: "other/blah", Kind: "Test"}, + Other: "test", + Value: 1, }, strict: true, }, @@ -352,8 +328,9 @@ func TestDecode(t *testing.T) { typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind("mock", schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})}, expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, expectedObject: &testDecodable{ - Other: "test", - Value: 1, + TypeMeta: metav1.TypeMeta{APIVersion: "other/blah", Kind: "Test"}, + Other: "test", + Value: 1, }, yaml: true, strict: true, From ffb2d12633cdc9a908d965a54ab6157ab52d60e8 Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Fri, 15 Oct 2021 11:40:48 -0400 Subject: [PATCH 3/3] Test json/yaml decoding type coercion --- .../pkg/runtime/serializer/json/json_test.go | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) diff --git a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json_test.go b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json_test.go index 22efe4c8ee3..aac6cc3f7c2 100644 --- a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json_test.go @@ -76,6 +76,39 @@ func (d *testDecodable) DeepCopyInto(out *testDecodable) { return } +type testDecodeCoercion struct { + metav1.TypeMeta `json:",inline"` + + Bool bool `json:"bool"` + + Int int `json:"int"` + Int32 int `json:"int32"` + Int64 int `json:"int64"` + + Float32 float32 `json:"float32"` + Float64 float64 `json:"float64"` + + String string `json:"string"` + + Struct testDecodable `json:"struct"` + + Array []string `json:"array"` + Map map[string]string `json:"map"` +} + +func (d *testDecodeCoercion) DeepCopyObject() runtime.Object { + if d == nil { + return nil + } + out := new(testDecodeCoercion) + d.DeepCopyInto(out) + return out +} +func (d *testDecodeCoercion) DeepCopyInto(out *testDecodeCoercion) { + *out = *d + return +} + func TestDecode(t *testing.T) { testCases := []struct { creater runtime.ObjectCreater @@ -358,6 +391,115 @@ func TestDecode(t *testing.T) { yaml: true, strict: true, }, + + // coerce from null + { + data: []byte(`{"bool":null,"int":null,"int32":null,"int64":null,"float32":null,"float64":null,"string":null,"array":null,"map":null,"struct":null}`), + into: &testDecodeCoercion{}, + typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, + expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, + expectedObject: &testDecodeCoercion{}, + strict: true, + }, + { + data: []byte(`{"bool":null,"int":null,"int32":null,"int64":null,"float32":null,"float64":null,"string":null,"array":null,"map":null,"struct":null}`), + into: &testDecodeCoercion{}, + typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, + expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, + expectedObject: &testDecodeCoercion{}, + yaml: true, + strict: true, + }, + // coerce from string + { + data: []byte(`{"string":""}`), + into: &testDecodeCoercion{}, + typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, + expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, + expectedObject: &testDecodeCoercion{}, + strict: true, + }, + { + data: []byte(`{"string":""}`), + into: &testDecodeCoercion{}, + typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, + expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, + expectedObject: &testDecodeCoercion{}, + yaml: true, + strict: true, + }, + // coerce from array + { + data: []byte(`{"array":[]}`), + into: &testDecodeCoercion{}, + typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, + expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, + expectedObject: &testDecodeCoercion{Array: []string{}}, + strict: true, + }, + { + data: []byte(`{"array":[]}`), + into: &testDecodeCoercion{}, + typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, + expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, + expectedObject: &testDecodeCoercion{Array: []string{}}, + yaml: true, + strict: true, + }, + // coerce from map + { + data: []byte(`{"map":{},"struct":{}}`), + into: &testDecodeCoercion{}, + typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, + expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, + expectedObject: &testDecodeCoercion{Map: map[string]string{}}, + strict: true, + }, + { + data: []byte(`{"map":{},"struct":{}}`), + into: &testDecodeCoercion{}, + typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, + expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, + expectedObject: &testDecodeCoercion{Map: map[string]string{}}, + yaml: true, + strict: true, + }, + // coerce from int + { + data: []byte(`{"int":1,"int32":1,"int64":1,"float32":1,"float64":1}`), + into: &testDecodeCoercion{}, + typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, + expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, + expectedObject: &testDecodeCoercion{Int: 1, Int32: 1, Int64: 1, Float32: 1, Float64: 1}, + strict: true, + }, + { + data: []byte(`{"int":1,"int32":1,"int64":1,"float32":1,"float64":1}`), + into: &testDecodeCoercion{}, + typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, + expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, + expectedObject: &testDecodeCoercion{Int: 1, Int32: 1, Int64: 1, Float32: 1, Float64: 1}, + yaml: true, + strict: true, + }, + // coerce from float + { + data: []byte(`{"float32":1.0,"float64":1.0}`), + into: &testDecodeCoercion{}, + typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, + expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, + expectedObject: &testDecodeCoercion{Float32: 1, Float64: 1}, + strict: true, + }, + { + data: []byte(`{"int":1.0,"int32":1.0,"int64":1.0,"float32":1.0,"float64":1.0}`), // floating point gets dropped in yaml -> json step + into: &testDecodeCoercion{}, + typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}}, + expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, + expectedObject: &testDecodeCoercion{Int: 1, Int32: 1, Int64: 1, Float32: 1, Float64: 1}, + yaml: true, + strict: true, + }, } for i, test := range testCases {