diff --git a/pkg/conversion/converter_test.go b/pkg/conversion/converter_test.go index ca2b2c657d2..96c569cd6cd 100644 --- a/pkg/conversion/converter_test.go +++ b/pkg/conversion/converter_test.go @@ -93,7 +93,7 @@ func TestConverter_CallsRegisteredFunctions(t *testing.T) { func TestConverter_fuzz(t *testing.T) { newAnonType := func() interface{} { - return reflect.New(reflect.TypeOf(externalTypeReturn())).Interface() + return reflect.New(reflect.TypeOf(externalTypeReturn()).Elem()).Interface() } // Use the same types from the scheme test. table := []struct { diff --git a/pkg/conversion/scheme.go b/pkg/conversion/scheme.go index 1c0a9836c2d..af88ba4e63e 100644 --- a/pkg/conversion/scheme.go +++ b/pkg/conversion/scheme.go @@ -85,8 +85,8 @@ func NewScheme() *Scheme { // AddKnownTypes registers all types passed in 'types' as being members of version 'version. // Encode() will refuse objects unless their type has been registered with AddKnownTypes. -// All objects passed to types should be structs, not pointers to structs. The name that go -// reports for the struct becomes the "kind" field when encoding. +// All objects passed to types should be pointers to structs. The name that go reports for +// the struct becomes the "kind" field when encoding. func (s *Scheme) AddKnownTypes(version string, types ...interface{}) { knownTypes, found := s.versionMap[version] if !found { @@ -95,8 +95,12 @@ func (s *Scheme) AddKnownTypes(version string, types ...interface{}) { } for _, obj := range types { t := reflect.TypeOf(obj) + if t.Kind() != reflect.Ptr { + panic("All types must be pointers to structs.") + } + t = t.Elem() if t.Kind() != reflect.Struct { - panic("All types must be structs.") + panic("All types must be pointers to structs.") } knownTypes[t.Name()] = t s.typeToVersion[t] = version diff --git a/pkg/conversion/scheme_test.go b/pkg/conversion/scheme_test.go index 0f4d7f2a4e1..c8232ff4ea6 100644 --- a/pkg/conversion/scheme_test.go +++ b/pkg/conversion/scheme_test.go @@ -90,7 +90,7 @@ func externalTypeReturn() interface{} { O *TestType2 `yaml:"O,omitempty" json:"O,omitempty"` P []TestType2 `yaml:"Q,omitempty" json:"Q,omitempty"` } - return TestType1{} + return &TestType1{} } type ExternalInternalSame struct { @@ -124,8 +124,8 @@ var TestObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 100).Funcs( // Returns a new Scheme set up with the test objects. func GetTestScheme() *Scheme { s := NewScheme() - s.AddKnownTypes("", TestType1{}, ExternalInternalSame{}) - s.AddKnownTypes("v1", externalTypeReturn(), ExternalInternalSame{}) + s.AddKnownTypes("", &TestType1{}, &ExternalInternalSame{}) + s.AddKnownTypes("v1", externalTypeReturn(), &ExternalInternalSame{}) s.ExternalVersion = "v1" s.InternalVersion = "" s.MetaInsertionFactory = testMetaInsertionFactory{}