From c482a75dbf1c49920ec9ab0d1f54e82c2e05a12f Mon Sep 17 00:00:00 2001 From: hhhjort <31041505+hhhjort@users.noreply.github.com> Date: Fri, 25 Oct 2024 16:03:22 -0400 Subject: [PATCH] ORTB 2.6: Adds a Regs clone (#4012) --- exchange/utils.go | 1 + ortb/clone.go | 16 ++++++++++++++++ ortb/clone_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/exchange/utils.go b/exchange/utils.go index 4f4e2a53f41..5e3feab15f5 100644 --- a/exchange/utils.go +++ b/exchange/utils.go @@ -220,6 +220,7 @@ func (rs *requestSplitter) cleanOpenRTBRequests(ctx context.Context, // down convert info, ok := rs.bidderInfo[bidder] if !ok || info.OpenRTB == nil || info.OpenRTB.Version != "2.6" { + reqWrapperCopy.Regs = ortb.CloneRegs(reqWrapperCopy.Regs) if err := openrtb_ext.ConvertDownTo25(reqWrapperCopy); err != nil { errs = append(errs, err) continue diff --git a/ortb/clone.go b/ortb/clone.go index 1e34795e05a..ff2d16560b8 100644 --- a/ortb/clone.go +++ b/ortb/clone.go @@ -277,3 +277,19 @@ func CloneBidRequestPartial(s *openrtb2.BidRequest) *openrtb2.BidRequest { return &c } + +func CloneRegs(s *openrtb2.Regs) *openrtb2.Regs { + if s == nil { + return nil + } + + // Shallow Copy (Value Fields) + c := *s + + // Deep Copy (Pointers) + c.GDPR = ptrutil.Clone(s.GDPR) + c.GPPSID = slices.Clone(s.GPPSID) + c.Ext = slices.Clone(s.Ext) + + return &c +} diff --git a/ortb/clone_test.go b/ortb/clone_test.go index 21c19f170c6..096f78d51b2 100644 --- a/ortb/clone_test.go +++ b/ortb/clone_test.go @@ -759,3 +759,44 @@ func discoverPointerFields(t reflect.Type) []string { } return fields } + +func TestCloneRegs(t *testing.T) { + t.Run("nil", func(t *testing.T) { + result := CloneRegs(nil) + assert.Nil(t, result) + }) + + t.Run("empty", func(t *testing.T) { + given := &openrtb2.Regs{} + result := CloneRegs(given) + assert.Empty(t, result) + assert.NotSame(t, given, result) + }) + + t.Run("populated", func(t *testing.T) { + given := &openrtb2.Regs{ + COPPA: 1, + GDPR: ptrutil.ToPtr(int8(0)), + USPrivacy: "1YNN", + GPP: "SomeGPPStrig", + GPPSID: []int8{1, 2, 3}, + Ext: json.RawMessage(`{"anyField":1}`), + } + result := CloneRegs(given) + assert.Equal(t, given, result, "equality") + assert.NotSame(t, given, result, "pointer") + assert.NotSame(t, given.GDPR, result.GDPR, "gdpr") + assert.NotSame(t, given.GPPSID, result.GPPSID, "gppsid[]") + assert.NotSame(t, given.GPPSID[0], result.GPPSID[0], "gppsid[0]") + assert.NotSame(t, given.Ext, result.Ext, "ext") + }) + + t.Run("assumptions", func(t *testing.T) { + assert.ElementsMatch(t, discoverPointerFields(reflect.TypeOf(openrtb2.Regs{})), + []string{ + "GDPR", + "GPPSID", + "Ext", + }) + }) +}