From 408ad0773f471b34d40e76af14ed0d53dc0565ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20St=C3=A4bler?= Date: Wed, 25 Jan 2023 09:36:39 +0100 Subject: [PATCH] Make SetDefaults of Destination duck type nil safer (#2670) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christoph Stäbler Signed-off-by: Christoph Stäbler --- apis/duck/v1/destination.go | 4 ++++ apis/duck/v1/destination_test.go | 17 +++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/apis/duck/v1/destination.go b/apis/duck/v1/destination.go index 87f0983fd..c895e6d29 100644 --- a/apis/duck/v1/destination.go +++ b/apis/duck/v1/destination.go @@ -73,6 +73,10 @@ func (d *Destination) GetRef() *KReference { } func (d *Destination) SetDefaults(ctx context.Context) { + if d == nil { + return + } + if d.Ref != nil && d.Ref.Namespace == "" { d.Ref.Namespace = apis.ParentMeta(ctx).Namespace } diff --git a/apis/duck/v1/destination_test.go b/apis/duck/v1/destination_test.go index 6472c6162..a514e2207 100644 --- a/apis/duck/v1/destination_test.go +++ b/apis/duck/v1/destination_test.go @@ -179,7 +179,10 @@ func TestDestinationSetDefaults(t *testing.T) { d *Destination ctx context.Context want string - }{"uri set, nothing in ref, not modified ": { + }{"destination nil ": { + d: nil, + ctx: ctx, + }, "uri set, nothing in ref, not modified ": { d: &Destination{URI: apis.HTTP("example.com")}, ctx: ctx, }, "namespace set, nothing in context, not modified ": { @@ -206,11 +209,13 @@ func TestDestinationSetDefaults(t *testing.T) { for name, tc := range tests { t.Run(name, func(t *testing.T) { tc.d.SetDefaults(tc.ctx) - if tc.d.Ref != nil && tc.d.Ref.Namespace != tc.want { - t.Errorf("Got: %s wanted %s", tc.d.Ref.Namespace, tc.want) - } - if tc.d.Ref == nil && tc.want != "" { - t.Error("Got: nil Ref wanted", tc.want) + if tc.d != nil { + if tc.d.Ref != nil && tc.d.Ref.Namespace != tc.want { + t.Errorf("Got: %s wanted %s", tc.d.Ref.Namespace, tc.want) + } + if tc.d.Ref == nil && tc.want != "" { + t.Error("Got: nil Ref wanted", tc.want) + } } }) }