mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 07:47:56 +00:00
Make strict json unstructured decoding consistent with non-strict decoding
This commit is contained in:
parent
48d3fde39f
commit
37952b7dbc
@ -166,7 +166,20 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
|
|||||||
strictErrs, err := s.unmarshal(into, data, originalData)
|
strictErrs, err := s.unmarshal(into, data, originalData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, actual, err
|
return nil, actual, err
|
||||||
} else if len(strictErrs) > 0 {
|
}
|
||||||
|
|
||||||
|
// when decoding directly into a provided unstructured object,
|
||||||
|
// extract the actual gvk decoded from the provided data,
|
||||||
|
// and ensure it is non-empty.
|
||||||
|
if isUnstructured {
|
||||||
|
*actual = into.GetObjectKind().GroupVersionKind()
|
||||||
|
if len(actual.Kind) == 0 {
|
||||||
|
return nil, actual, runtime.NewMissingKindErr(string(originalData))
|
||||||
|
}
|
||||||
|
// TODO(109023): require apiVersion here as well once unstructuredJSONScheme#Decode does
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(strictErrs) > 0 {
|
||||||
return into, actual, runtime.NewStrictDecodingError(strictErrs)
|
return into, actual, runtime.NewStrictDecodingError(strictErrs)
|
||||||
}
|
}
|
||||||
return into, actual, nil
|
return into, actual, nil
|
||||||
@ -261,9 +274,9 @@ func (s *Serializer) unmarshal(into runtime.Object, data, originalData []byte) (
|
|||||||
var strictJSONErrs []error
|
var strictJSONErrs []error
|
||||||
if u, isUnstructured := into.(runtime.Unstructured); isUnstructured {
|
if u, isUnstructured := into.(runtime.Unstructured); isUnstructured {
|
||||||
// Unstructured is a custom unmarshaler that gets delegated
|
// Unstructured is a custom unmarshaler that gets delegated
|
||||||
// to, so inorder to detect strict JSON errors we need
|
// to, so in order to detect strict JSON errors we need
|
||||||
// to unmarshal directly into the object.
|
// to unmarshal directly into the object.
|
||||||
m := u.UnstructuredContent()
|
m := map[string]interface{}{}
|
||||||
strictJSONErrs, err = kjson.UnmarshalStrict(data, &m)
|
strictJSONErrs, err = kjson.UnmarshalStrict(data, &m)
|
||||||
u.SetUnstructuredContent(m)
|
u.SetUnstructuredContent(m)
|
||||||
} else {
|
} else {
|
||||||
|
@ -638,10 +638,10 @@ func TestDecode(t *testing.T) {
|
|||||||
},
|
},
|
||||||
// Duplicate fields should return an error from the strict JSON deserializer for unstructured.
|
// Duplicate fields should return an error from the strict JSON deserializer for unstructured.
|
||||||
{
|
{
|
||||||
data: []byte(`{"value":1,"value":1}`),
|
data: []byte(`{"kind":"Custom","value":1,"value":1}`),
|
||||||
into: &unstructured.Unstructured{},
|
into: &unstructured.Unstructured{},
|
||||||
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
expectedGVK: &schema.GroupVersionKind{},
|
expectedGVK: &schema.GroupVersionKind{Kind: "Custom"},
|
||||||
errFn: func(err error) bool {
|
errFn: func(err error) bool {
|
||||||
return strings.Contains(err.Error(), `duplicate field "value"`)
|
return strings.Contains(err.Error(), `duplicate field "value"`)
|
||||||
},
|
},
|
||||||
@ -649,11 +649,12 @@ func TestDecode(t *testing.T) {
|
|||||||
},
|
},
|
||||||
// Duplicate fields should return an error from the strict YAML deserializer for unstructured.
|
// Duplicate fields should return an error from the strict YAML deserializer for unstructured.
|
||||||
{
|
{
|
||||||
data: []byte("value: 1\n" +
|
data: []byte("kind: Custom\n" +
|
||||||
|
"value: 1\n" +
|
||||||
"value: 1\n"),
|
"value: 1\n"),
|
||||||
into: &unstructured.Unstructured{},
|
into: &unstructured.Unstructured{},
|
||||||
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
typer: &mockTyper{gvk: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}},
|
||||||
expectedGVK: &schema.GroupVersionKind{},
|
expectedGVK: &schema.GroupVersionKind{Kind: "Custom"},
|
||||||
errFn: func(err error) bool {
|
errFn: func(err error) bool {
|
||||||
return strings.Contains(err.Error(), `"value" already set in map`)
|
return strings.Contains(err.Error(), `"value" already set in map`)
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user