Skip to content

Commit

Permalink
OTT-1349 : Pass schain object to VAST bidder via schain macro (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankit-Pinge authored Nov 7, 2023
1 parent 2a2d2db commit 9540de8
Show file tree
Hide file tree
Showing 7 changed files with 440 additions and 0 deletions.
27 changes: 27 additions & 0 deletions adapters/vastbidder/bidder_macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,33 @@ func (tag *BidderMacro) MacroPaymentIDChain(key string) string {
return ""
}

// MacroSchain contains definition for Schain Parameter
func (tag *BidderMacro) MacroSchain(key string) string {
if tag.Request.Source == nil {
return ""
}

if tag.Request.Source.SChain != nil {
return openrtb_ext.SerializeSupplyChain(tag.Request.Source.SChain)
}

if tag.Request.Source.Ext != nil {
schain, _, _, err := jsonparser.Get(tag.Request.Source.Ext, MacroSchain)

if err != nil {
return ""
}
var schainObj openrtb2.SupplyChain
err = json.Unmarshal(schain, &schainObj)

if err != nil {
return ""
}
return openrtb_ext.SerializeSupplyChain(&schainObj)
}
return ""
}

/********************* Regs *********************/

// MacroCoppa contains definition for Coppa Parameter
Expand Down
142 changes: 142 additions & 0 deletions adapters/vastbidder/bidder_macro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1619,3 +1619,145 @@ func TestBidderMacroKVM(t *testing.T) {
})
}
}

func TestMacroSchain(t *testing.T) {

type fields struct {
Request *openrtb2.BidRequest
}
type args struct {
key string
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "source_object_with_both_source.schain_and_source.ext.schain",
fields: fields{&openrtb2.BidRequest{Source: &openrtb2.Source{
SChain: &openrtb2.SupplyChain{},
Ext: []byte(`{
"schain":{
"complete":1,
"nodes":[
{
"asi":"exchange1.com",
"sid":"1234&abcd",
"hp":1,
"name":"publisher name"
}
],
"ver":"1.0"
}
}`),
}}},
args: args{key: "schain"},
want: "", // here we have given priority to source.schain object hence source.schain is not nil it return empty string
},
{
name: "nil_source.schain_object",
fields: fields{&openrtb2.BidRequest{
Source: &openrtb2.Source{
SChain: nil,
Ext: []byte(`{
"schain":{
"complete":0,
"nodes":[
{
"asi":"exchange2.com",
"sid":"abcd",
"hp":1
}
],
"ver":"1.0"
}
}`),
},
}},
args: args{key: "schain"},
want: "1.0,0!exchange2.com,abcd,1,,,",
},
{
name: "missing_schain_object",
fields: fields{&openrtb2.BidRequest{Source: &openrtb2.Source{
Ext: []byte(`{
"somechain":{
"complete":1,
"nodes":[
{
"asi":"exchange1.com",
"sid":"1234&abcd",
"hp":1,
"ext":{"k1":"v1"}
}
],
"ver":"1.0"
}
}`),
}}},
args: args{key: "schain"},
want: "",
},
{
name: "missing_both_source.schain_and_source.ext",
fields: fields{&openrtb2.BidRequest{Source: nil}},
args: args{key: "schain"},
want: "",
},
{
name: "source.schain_is_present",
fields: fields{&openrtb2.BidRequest{Source: &openrtb2.Source{
SChain: &openrtb2.SupplyChain{
Complete: 1,
Ver: "1.0",
Nodes: []openrtb2.SupplyChainNode{
{
ASI: "asi",
SID: "sid",
RID: "rid",
Name: "name",
Domain: "domain",
HP: openrtb2.Int8Ptr(1),
},
}},
}}},
args: args{key: "schain"},
want: "1.0,1!asi,sid,1,rid,name,domain",
},
{
name: "unmarshaling_error",
fields: fields{&openrtb2.BidRequest{Source: &openrtb2.Source{
Ext: []byte(`{
"schain":{
"complete":"1",
"nodes":[
{
"asi":"exchange1.com",
"sid":"1234&abcd",
"rid":"bid-request-1",
"name":"publisher%20name",
"domain":"publisher.com",
"hp":1
}
],
"ver":"1.0"
}
}`),
}}},
args: args{key: "schain"},
want: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tag := &BidderMacro{
Request: tt.fields.Request,
}
got := tag.MacroSchain(tt.args.key)
assert.Equal(t, got, tt.want, tt.name)
})
}
}
1 change: 1 addition & 0 deletions adapters/vastbidder/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
MacroFD = `fd`
MacroTransactionID = `tid`
MacroPaymentIDChain = `pchain`
MacroSchain = `schain`

//Regs
MacroCoppa = `coppa`
Expand Down
1 change: 1 addition & 0 deletions adapters/vastbidder/ibidder_macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type IBidderMacro interface {
MacroFD(string) string
MacroTransactionID(string) string
MacroPaymentIDChain(string) string
MacroSchain(string) string

//Regs
MacroCoppa(string) string
Expand Down
1 change: 1 addition & 0 deletions adapters/vastbidder/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var _defaultMapper = Mapper{
MacroFD: &macroCallBack{cached: true, escape: true, callback: IBidderMacro.MacroFD},
MacroTransactionID: &macroCallBack{cached: true, escape: true, callback: IBidderMacro.MacroTransactionID},
MacroPaymentIDChain: &macroCallBack{cached: true, escape: true, callback: IBidderMacro.MacroPaymentIDChain},
MacroSchain: &macroCallBack{cached: true, escape: false, callback: IBidderMacro.MacroSchain},

//Regs
MacroCoppa: &macroCallBack{cached: true, callback: IBidderMacro.MacroCoppa},
Expand Down
57 changes: 57 additions & 0 deletions openrtb_ext/supplyChain.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package openrtb_ext

import (
"fmt"
"net/url"
"strings"

"github.com/prebid/openrtb/v19/openrtb2"
"github.com/prebid/prebid-server/util/ptrutil"
)
Expand All @@ -19,3 +23,56 @@ func cloneSupplyChain(schain *openrtb2.SupplyChain) *openrtb2.SupplyChain {
return &clone

}

// SerializeSupplyChain convert schain object to serialized string
func SerializeSupplyChain(schain *openrtb2.SupplyChain) string {

if len(schain.Nodes) < 1 {
return ""
}
var serializedSchain strings.Builder
serializedSchain.Grow(256)

serializedSchain.WriteString(schain.Ver)
serializedSchain.WriteByte(',')
fmt.Fprintf(&serializedSchain, "%d", schain.Complete)

for _, node := range schain.Nodes {
serializedSchain.WriteByte('!')

if node.ASI != "" {
serializedSchain.WriteString(url.QueryEscape(node.ASI))
}
serializedSchain.WriteByte(',')

if node.SID != "" {
serializedSchain.WriteString(url.QueryEscape(node.SID))
}
serializedSchain.WriteByte(',')

if node.HP != nil {
// node.HP is integer pointer so 1st dereference it then convert it to string and push to serializedSchain
fmt.Fprintf(&serializedSchain, "%d", *node.HP)
}
serializedSchain.WriteByte(',')

if node.RID != "" {
serializedSchain.WriteString(url.QueryEscape(node.RID))
}
serializedSchain.WriteByte(',')

if node.Name != "" {
serializedSchain.WriteString(url.QueryEscape(node.Name))
}
serializedSchain.WriteByte(',')

if node.Domain != "" {
serializedSchain.WriteString(url.QueryEscape(node.Domain))
}
if node.Ext != nil {
serializedSchain.WriteByte(',')
serializedSchain.WriteString(url.QueryEscape(string(node.Ext)))
}
}
return serializedSchain.String()
}
Loading

0 comments on commit 9540de8

Please sign in to comment.