From 51f34a4cfe56b2efe248ad5afb3e6b4ddd2670da Mon Sep 17 00:00:00 2001 From: HirazawaUi <695097494plus@gmail.com> Date: Wed, 12 Jul 2023 00:02:13 +0800 Subject: [PATCH] Fix the converts an empty string to nil. Kubernetes-commit: 1484a5c32f079e0051964e6cf51ec1602c224978 --- pkg/runtime/converter.go | 4 ++-- pkg/runtime/converter_test.go | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pkg/runtime/converter.go b/pkg/runtime/converter.go index 90bf487e3..62eb27afc 100644 --- a/pkg/runtime/converter.go +++ b/pkg/runtime/converter.go @@ -231,7 +231,7 @@ func (c *fromUnstructuredContext) pushKey(key string) { } -// FromUnstructuredWIthValidation converts an object from map[string]interface{} representation into a concrete type. +// FromUnstructuredWithValidation converts an object from map[string]interface{} representation into a concrete type. // It uses encoding/json/Unmarshaler if object implements it or reflection if not. // It takes a validationDirective that indicates how to behave when it encounters unknown fields. func (c *unstructuredConverter) FromUnstructuredWithValidation(u map[string]interface{}, obj interface{}, returnUnknownFields bool) error { @@ -465,7 +465,7 @@ func sliceFromUnstructured(sv, dv reflect.Value, ctx *fromUnstructuredContext) e } dv.SetBytes(data) } else { - dv.Set(reflect.Zero(dt)) + dv.Set(reflect.MakeSlice(dt, 0, 0)) } return nil } diff --git a/pkg/runtime/converter_test.go b/pkg/runtime/converter_test.go index 620eeef20..848e261c6 100644 --- a/pkg/runtime/converter_test.go +++ b/pkg/runtime/converter_test.go @@ -91,6 +91,7 @@ type F struct { G []int `json:"fg"` H []bool `json:"fh"` I []float32 `json:"fi"` + J []byte `json:"fj"` } type G struct { @@ -751,6 +752,10 @@ func TestUnrecognized(t *testing.T) { data: "{\"ff\":[\"abc\"],\"fg\":[123],\"fh\":[true,false]}", obj: &F{}, }, + { + data: "{\"fj\":\"\"}", + obj: &F{}, + }, { // Invalid string data data: "{\"fa\":123}",