Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inconsistent requirement for kind in strict json decoding #109019

Merged
merged 2 commits into from Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -340,6 +340,7 @@ func (s unstructuredJSONScheme) Decode(data []byte, _ *schema.GroupVersionKind,
if len(gvk.Kind) == 0 {
return nil, &gvk, runtime.NewMissingKindErr(string(data))
}
// TODO(109023): require apiVersion here as well

return obj, &gvk, nil
}
Expand Down
Expand Up @@ -166,7 +166,20 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
strictErrs, err := s.unmarshal(into, data, originalData)
if err != nil {
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, nil
Expand Down Expand Up @@ -261,9 +274,9 @@ func (s *Serializer) unmarshal(into runtime.Object, data, originalData []byte) (
var strictJSONErrs []error
if u, isUnstructured := into.(runtime.Unstructured); isUnstructured {
// 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.
m := u.UnstructuredContent()
m := map[string]interface{}{}
strictJSONErrs, err = kjson.UnmarshalStrict(data, &m)
u.SetUnstructuredContent(m)
} else {
Expand Down