Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OTT-1956: Introduce transparency object in response.bid.ext.dsa #871

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions adapters/pubmatic/pubmatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ func (a *PubmaticAdapter) MakeBids(internalRequest *openrtb2.BidRequest, externa
for i := 0; i < len(sb.Bid); i++ {
bid := sb.Bid[i]

bid.Ext = renameTransparencyParamsKey(bid.Ext)
ashishshinde-pubm marked this conversation as resolved.
Show resolved Hide resolved
// Copy SeatBid Ext to Bid.Ext
bid.Ext = copySBExtToBidExt(sb.Ext, bid.Ext)

Expand Down
28 changes: 28 additions & 0 deletions adapters/pubmatic/pubmatic_ow.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package pubmatic

import (
"bytes"
"encoding/json"
"fmt"
"strconv"

"github.com/buger/jsonparser"
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/openrtb_ext"
)

const (
dsaKey = "dsa"
transparencyKey = "transparency"
)

var (
paramKey = []byte(`"params"`)
dsaParamKey = []byte(`"dsaparams"`)
)

func getTargetingKeys(bidExt json.RawMessage, bidderName string) map[string]string {
targets := map[string]string{}
if bidExt != nil {
Expand Down Expand Up @@ -74,3 +87,18 @@ func prepareMetaObject(bid openrtb2.Bid, bidExt *pubmaticBidExt, seat string) *o

return meta
}

// renameTransparencyParamsKey renames the bid.ext.dsa.transparency.params key to bid.ext.dsa.transparency.dsaparams
func renameTransparencyParamsKey(bidExt []byte) []byte {
transparencyObjectCnt := 0
jsonparser.ArrayEach(bidExt, func(transparencyObject []byte, dataType jsonparser.ValueType, offset int, err error) {
transparencyObject = bytes.Replace(transparencyObject, paramKey, dsaParamKey, 1)
ashishshinde-pubm marked this conversation as resolved.
Show resolved Hide resolved
bidExt, err = jsonparser.Set(bidExt, transparencyObject, dsaKey, transparencyKey, fmt.Sprintf("[%d]", transparencyObjectCnt))
if err != nil {
return
}
transparencyObjectCnt++
}, dsaKey, transparencyKey)

return bidExt
}
239 changes: 239 additions & 0 deletions adapters/pubmatic/pubmatic_ow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package pubmatic

import (
"encoding/json"
"net/http"
"reflect"
"testing"

"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/openrtb_ext"
"github.com/stretchr/testify/assert"
)

func TestGetAdServerTargetingForEmptyExt(t *testing.T) {
Expand Down Expand Up @@ -247,3 +250,239 @@ func TestPrepareMetaObject(t *testing.T) {
})
}
}

func TestRenameTransparencyParamsKey(t *testing.T) {
type args struct {
bidExt []byte
}
tests := []struct {
name string
args args
want []byte
}{
{
name: "Empty bidExt",
args: args{
bidExt: []byte(``),
},
want: []byte(``),
},
{
name: "Valid bidExt with params key",
args: args{
bidExt: []byte(`{"dsa":{"transparency":[{"domain":"params.com","params":[1,2]},{"domain":"dsaicon2.com","params":[2,3]}]}}`),
},
want: []byte(`{"dsa":{"transparency":[{"domain":"params.com","dsaparams":[1,2]},{"domain":"dsaicon2.com","dsaparams":[2,3]}]}}`),
},
{
name: "bidExt without params key",
args: args{
bidExt: []byte(`{"dsa":{"transparency":[{"domain":"dsaicon1.com","dsaparams":[1,2]},{"domain":"dsaicon2.com","dsaparams":[2,3]}]}}`),
},
want: []byte(`{"dsa":{"transparency":[{"domain":"dsaicon1.com","dsaparams":[1,2]},{"domain":"dsaicon2.com","dsaparams":[2,3]}]}}`),
},
{
name: "Empty transparency array",
args: args{
bidExt: []byte(`{"dsa":{"transparency":[]}}`),
},
want: []byte(`{"dsa":{"transparency":[]}}`),
},
{
name: "bidExt with invalid transparency key",
args: args{
bidExt: []byte(`{"dsa":{"transparency":{"domain":"dsaicon1.com","params":[1,2]}}}`),
},
want: []byte(`{"dsa":{"transparency":{"domain":"dsaicon1.com","params":[1,2]}}}`),
},
{
name: "Invalid bidExt structure",
args: args{
bidExt: []byte(`{"dsa":{"transparency":[{"domain":"dsaicon1.com","params":[1,2]},{"domain":"dsaicon2.com","params":[2,3]`), // Missing closing brackets
},
want: []byte(`{"dsa":{"transparency":[{"domain":"dsaicon1.com","dsaparams":[1,2]},{"domain":"dsaicon2.com","params":[2,3]`), // Missing closing brackets,
},
{
name: "Invalid params key",
args: args{
bidExt: []byte(`{"dsa":{"transparency":[{"domain":"dsaicon1.com"}]}}`),
},
want: []byte(`{"dsa":{"transparency":[{"domain":"dsaicon1.com"}]}}`),
},
{
name: "Missing transparency key",
args: args{
bidExt: []byte(`{"dsa":{}}`),
},
want: []byte(`{"dsa":{}}`),
},
{
name: "Missing dsa key",
args: args{
bidExt: []byte(`{"any":value"}`),
},
want: []byte(`{"any":value"}`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := renameTransparencyParamsKey(tt.args.bidExt)
assert.Equal(t, string(tt.want), string(got))
})
}
}

func TestPubmaticMakeBids(t *testing.T) {
type fields struct {
URI string
}
type args struct {
internalRequest *openrtb2.BidRequest
externalRequest *adapters.RequestData
response *adapters.ResponseData
}
tests := []struct {
name string
fields fields
args args
wantErr []error
wantResp *adapters.BidderResponse
}{
{
name: "rename bid.ext.dsa.transparency.params to bid.ext.dsa.transparency.dsaparams",
args: args{
response: &adapters.ResponseData{
StatusCode: http.StatusOK,
Body: []byte(`{"id":"test-request-id","seatbid":[{"seat":"958","bid":[{"id":"7706636740145184841","impid":"test-imp-id","price":0.5,"adid":"29681110","adm":"some-test-ad","adomain":["pubmatic.com"],"crid":"29681110","h":250,"w":300,"dealid":"testdeal","ext":{"dsa":{"transparency":[{"params":[1,2]}]},"dspid":6,"deal_channel":1,"prebiddealpriority":1}}],"ext":{"buyid":"testBuyId"}}],"bidid":"5778926625248726496","cur":"USD"}`),
},
externalRequest: &adapters.RequestData{BidderName: openrtb_ext.BidderPubmatic},
},
wantErr: nil,
wantResp: &adapters.BidderResponse{
Bids: []*adapters.TypedBid{
{
Bid: &openrtb2.Bid{
ID: "7706636740145184841",
ImpID: "test-imp-id",
Price: 0.500000,
AdID: "29681110",
AdM: "some-test-ad",
ADomain: []string{"pubmatic.com"},
CrID: "29681110",
H: 250,
W: 300,
DealID: "testdeal",
Ext: json.RawMessage(`{"buyid":"testBuyId","deal_channel":1,"dsa":{"transparency":[{"dsaparams":[1,2]}]},"dspid":6,"prebiddealpriority":1}`),
},
DealPriority: 1,
BidType: openrtb_ext.BidTypeBanner,
BidVideo: &openrtb_ext.ExtBidPrebidVideo{},
BidTargets: map[string]string{"hb_buyid_pubmatic": "testBuyId"},
BidMeta: &openrtb_ext.ExtBidPrebidMeta{
AdvertiserID: 958,
AgencyID: 958,
NetworkID: 6,
DemandSource: "6",
MediaType: "banner",
},
},
},
Currency: "USD",
},
},
{
name: "correct bid.ext.dsa.transparency.dsaparams present in response",
args: args{
response: &adapters.ResponseData{
StatusCode: http.StatusOK,
Body: []byte(`{"id":"test-request-id","seatbid":[{"seat":"958","bid":[{"id":"7706636740145184841","impid":"test-imp-id","price":0.5,"adid":"29681110","adm":"some-test-ad","adomain":["pubmatic.com"],"crid":"29681110","h":250,"w":300,"dealid":"testdeal","ext":{"dsa":{"transparency":[{"dsaparams":[1,2]}]},"dspid":6,"deal_channel":1,"prebiddealpriority":1}}],"ext":{"buyid":"testBuyId"}}],"bidid":"5778926625248726496","cur":"USD"}`),
},
externalRequest: &adapters.RequestData{BidderName: openrtb_ext.BidderPubmatic},
},
wantErr: nil,
wantResp: &adapters.BidderResponse{
Bids: []*adapters.TypedBid{
{
Bid: &openrtb2.Bid{
ID: "7706636740145184841",
ImpID: "test-imp-id",
Price: 0.500000,
AdID: "29681110",
AdM: "some-test-ad",
ADomain: []string{"pubmatic.com"},
CrID: "29681110",
H: 250,
W: 300,
DealID: "testdeal",
Ext: json.RawMessage(`{"buyid":"testBuyId","deal_channel":1,"dsa":{"transparency":[{"dsaparams":[1,2]}]},"dspid":6,"prebiddealpriority":1}`),
},
DealPriority: 1,
BidType: openrtb_ext.BidTypeBanner,
BidVideo: &openrtb_ext.ExtBidPrebidVideo{},
BidTargets: map[string]string{"hb_buyid_pubmatic": "testBuyId"},
BidMeta: &openrtb_ext.ExtBidPrebidMeta{
AdvertiserID: 958,
AgencyID: 958,
NetworkID: 6,
DemandSource: "6",
MediaType: "banner",
},
},
},
Currency: "USD",
},
},
{
name: "bidExt without dsa",
args: args{
response: &adapters.ResponseData{
StatusCode: http.StatusOK,
Body: []byte(`{"id":"test-request-id","seatbid":[{"seat":"958","bid":[{"id":"7706636740145184841","impid":"test-imp-id","price":0.5,"adid":"29681110","adm":"some-test-ad","adomain":["pubmatic.com"],"crid":"29681110","h":250,"w":300,"dealid":"testdeal","ext":{"dspid":6,"deal_channel":1,"prebiddealpriority":1}}],"ext":{"buyid":"testBuyId"}}],"bidid":"5778926625248726496","cur":"USD"}`),
},
externalRequest: &adapters.RequestData{BidderName: openrtb_ext.BidderPubmatic},
},
wantErr: nil,
wantResp: &adapters.BidderResponse{
Bids: []*adapters.TypedBid{
{
Bid: &openrtb2.Bid{
ID: "7706636740145184841",
ImpID: "test-imp-id",
Price: 0.500000,
AdID: "29681110",
AdM: "some-test-ad",
ADomain: []string{"pubmatic.com"},
CrID: "29681110",
H: 250,
W: 300,
DealID: "testdeal",
Ext: json.RawMessage(`{"buyid":"testBuyId","deal_channel":1,"dspid":6,"prebiddealpriority":1}`),
},
DealPriority: 1,
BidType: openrtb_ext.BidTypeBanner,
BidVideo: &openrtb_ext.ExtBidPrebidVideo{},
BidTargets: map[string]string{"hb_buyid_pubmatic": "testBuyId"},
BidMeta: &openrtb_ext.ExtBidPrebidMeta{
AdvertiserID: 958,
AgencyID: 958,
NetworkID: 6,
DemandSource: "6",
MediaType: "banner",
},
},
},
Currency: "USD",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &PubmaticAdapter{
URI: tt.fields.URI,
}
gotResp, gotErr := a.MakeBids(tt.args.internalRequest, tt.args.externalRequest, tt.args.response)
assert.Equal(t, tt.wantErr, gotErr, gotErr)
assert.Equal(t, tt.wantResp, gotResp)
})
}
}
5 changes: 5 additions & 0 deletions dsa/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dsa
import (
"errors"

"github.com/buger/jsonparser"
"github.com/prebid/prebid-server/v2/exchange/entities"
"github.com/prebid/prebid-server/v2/openrtb_ext"
"github.com/prebid/prebid-server/v2/util/jsonutil"
Expand Down Expand Up @@ -46,6 +47,10 @@ func Validate(req *openrtb_ext.RequestWrapper, bid *entities.PbsOrtbBid) error {
reqDSA := getReqDSA(req)
bidDSA := getBidDSA(bid)

if (reqDSA == nil || reqDSA.Required == nil) && bidDSA != nil {
pm-isha-bharti marked this conversation as resolved.
Show resolved Hide resolved
bid.Bid.Ext = jsonparser.Delete(bid.Bid.Ext, "dsa")
return nil
}
if dsaRequired(reqDSA) && bidDSA == nil {
return ErrDsaMissing
}
Expand Down
Loading
Loading