From 861134310500b81a3fd3ed6e31a0c515245af89b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lovro=20Ma=C5=BEgon?= Date: Mon, 27 May 2024 11:41:41 +0200 Subject: [PATCH] prevent panic if no decode function is registered --- pkg/sr/serde.go | 2 +- pkg/sr/serde_test.go | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/sr/serde.go b/pkg/sr/serde.go index d4cb1634..10ebad5d 100644 --- a/pkg/sr/serde.go +++ b/pkg/sr/serde.go @@ -387,7 +387,7 @@ func (s *Serde) decodeFind(b []byte) ([]byte, tserde, error) { t = t.subindex[idx] } } - if !t.exists { + if !t.exists || t.decode == nil { return nil, tserde{}, ErrNotRegistered } return b, t, nil diff --git a/pkg/sr/serde_test.go b/pkg/sr/serde_test.go index 486e76c6..92c6ef44 100644 --- a/pkg/sr/serde_test.go +++ b/pkg/sr/serde_test.go @@ -53,6 +53,7 @@ func TestSerde(t *testing.T) { serde.Register(3, idx4{}, Index(0, 0, 1)) serde.Register(3, idx3{}, Index(0, 0)) serde.Register(5, oneidx{}, Index(0), GenerateFn(func() any { return &oneidx{Foo: "defoo", Bar: "debar"} })) + serde.Register(101, struct{}{}, DecodeFn(nil)) for i, test := range []struct { enc any @@ -154,8 +155,8 @@ func TestSerde(t *testing.T) { if _, err := serde.DecodeNew([]byte{0, 0, 0, 0, 99}); err != ErrNotRegistered { t.Errorf("got %v != exp ErrNotRegistered", err) } - if _, err := serde.DecodeNew([]byte{0, 0, 0, 0, 100, 0}); err != ErrNotRegistered { - // schema is registered but type is unknown + if _, err := serde.DecodeNew([]byte{0, 0, 0, 0, 101, 0}); err != ErrNotRegistered { + // schema is registered but no decode function t.Errorf("got %v != exp ErrNotRegistered", err) } }