diff --git a/pkg/conversion/converter.go b/pkg/conversion/converter.go index a77f321551f13..61025b617253a 100644 --- a/pkg/conversion/converter.go +++ b/pkg/conversion/converter.go @@ -583,7 +583,12 @@ func (c *Converter) defaultConvert(sv, dv reflect.Value, scope *scope) error { return nil } dv.Set(reflect.New(dt.Elem())) - return c.convert(sv.Elem(), dv.Elem(), scope) + switch st.Kind() { + case reflect.Ptr, reflect.Interface: + return c.convert(sv.Elem(), dv.Elem(), scope) + default: + return c.convert(sv, dv.Elem(), scope) + } case reflect.Map: if sv.IsNil() { // Don't copy a nil ptr! diff --git a/pkg/conversion/converter_test.go b/pkg/conversion/converter_test.go index 6ba1bac9cc80a..2a110546bbd79 100644 --- a/pkg/conversion/converter_test.go +++ b/pkg/conversion/converter_test.go @@ -39,6 +39,34 @@ func TestConverter_byteSlice(t *testing.T) { } } +func TestConverter_MismatchedTypes(t *testing.T) { + c := NewConverter() + + err := c.RegisterConversionFunc( + func(in *[]string, out *int, s Scope) error { + if str, err := strconv.Atoi((*in)[0]); err != nil { + return err + } else { + *out = str + return nil + } + }, + ) + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + + src := []string{"5"} + var dest *int + err = c.Convert(&src, &dest, 0, nil) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if e, a := 5, *dest; e != a { + t.Errorf("expected %#v, got %#v", e, a) + } +} + func TestConverter_DefaultConvert(t *testing.T) { type A struct { Foo string