Skip to content

Commit

Permalink
Update case insensitivity + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBVolcy committed Oct 12, 2023
1 parent 23bc394 commit 72b160b
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 11 deletions.
7 changes: 7 additions & 0 deletions endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,7 @@ func validateAndFillSourceTID(req *openrtb_ext.RequestWrapper, generateRequestID
}

func (deps *endpointDeps) validateBidAdjustmentFactors(adjustmentFactors map[string]float64, aliases map[string]string) error {
uniqueBidders := make(map[string]struct{})
for bidderToAdjust, adjustmentFactor := range adjustmentFactors {
if adjustmentFactor <= 0 {
return fmt.Errorf("request.ext.prebid.bidadjustmentfactors.%s must be a positive number. Got %f", bidderToAdjust, adjustmentFactor)
Expand All @@ -972,6 +973,12 @@ func (deps *endpointDeps) validateBidAdjustmentFactors(adjustmentFactors map[str
bidderName = normalizedCoreBidder.String()
}

if _, exists := uniqueBidders[bidderName]; exists {
return fmt.Errorf("cannot have multiple bidders that differ only in case style")
} else {
uniqueBidders[bidderName] = struct{}{}
}

if _, isBidder := deps.bidderMap[bidderName]; !isBidder {
if _, isAlias := aliases[bidderToAdjust]; !isAlias {
return fmt.Errorf("request.ext.prebid.bidadjustmentfactors.%s is not a known bidder or alias", bidderToAdjust)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"description": "This demonstrates a request with bidadjustmentfactors that have multiple of the same bidder is invalid request",
"config": {
"mockBidders": [
{
"bidderName": "appnexus",
"currency": "USD",
"price": 1.00
}
]
},
"mockBidRequest": {
"id": "some-request-id",
"site": {
"page": "prebid.org"
},
"user": {
"ext": {
"consent": "gdpr-consent-string"
}
},
"regs": {
"ext": {
"gdpr": 1,
"us_privacy": "1NYN"
}
},
"imp": [
{
"id": "some-impression-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"Appnexus": {
"placementId": 12883451
}
}
}
],
"tmax": 500,
"ext": {
"prebid": {
"bidadjustmentfactors": {
"APPNEXUS": 1.01,
"Appnexus": 2.00
},
"cache": {
"bids": {}
},
"channel": {
"name": "video",
"version": "1.0"
},
"targeting": {
"includewinners": false,
"pricegranularity": {
"precision": 2,
"ranges": [
{
"max": 20,
"increment": 0.10
}
]
}
}
}
}
},
"expectedReturnCode": 400,
"expectedErrorMessage": "Invalid request: cannot have multiple bidders that differ only in case style"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"description": "This demonstrates bid adjustment factors with bidder names that have different casing in imp vs. in bidadjustmentfactors",
"config": {
"mockBidders": [
{
"bidderName": "appnexus",
"currency": "USD",
"price": 1.00
}
]
},
"mockBidRequest": {
"id": "some-request-id",
"site": {
"page": "prebid.org"
},
"user": {
"ext": {
"consent": "gdpr-consent-string"
}
},
"regs": {
"ext": {
"gdpr": 1,
"us_privacy": "1NYN"
}
},
"imp": [
{
"id": "some-impression-id",
"banner": {
"format": [
{
"w": 300,
"h": 250
},
{
"w": 300,
"h": 600
}
]
},
"ext": {
"Appnexus": {
"placementId": 12883451
}
}
}
],
"tmax": 500,
"ext": {
"prebid": {
"bidadjustmentfactors": {
"APPNEXUS": 1.01
},
"cache": {
"bids": {}
},
"channel": {
"name": "video",
"version": "1.0"
},
"targeting": {
"includewinners": false,
"pricegranularity": {
"precision": 2,
"ranges": [
{
"max": 20,
"increment": 0.10
}
]
}
}
}
}
},
"expectedBidResponse": {
"id": "some-request-id",
"seatbid": [
{
"bid": [
{
"id": "appnexus-bid",
"impid": "some-impression-id",
"price": 1.01
}
],
"seat": "Appnexus"
}
],
"bidid": "test bid id",
"cur": "USD",
"nbr": 0
},
"expectedReturnCode": 200
}
4 changes: 2 additions & 2 deletions exchange/bidder.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ func (bidder *bidderAdapter) requestBid(ctx context.Context, bidderRequest Bidde
}

adjustmentFactor := 1.0
if givenAdjustment, ok := bidRequestOptions.bidAdjustments[bidderName.String()]; ok {
if givenAdjustment, ok := bidRequestOptions.bidAdjustments[(strings.ToLower(bidderName.String()))]; ok {
adjustmentFactor = givenAdjustment
} else if givenAdjustment, ok := bidRequestOptions.bidAdjustments[bidderRequest.BidderName.String()]; ok {
} else if givenAdjustment, ok := bidRequestOptions.bidAdjustments[(strings.ToLower(bidderRequest.BidderName.String()))]; ok {
adjustmentFactor = givenAdjustment
}

Expand Down
14 changes: 7 additions & 7 deletions exchange/bidder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2672,7 +2672,7 @@ func TestExtraBidWithBidAdjustments(t *testing.T) {
},
BidType: openrtb_ext.BidTypeBanner,
DealPriority: 4,
Seat: "pubmatic",
Seat: "PUBMATIC",
},
{
Bid: &openrtb2.Bid{
Expand All @@ -2699,7 +2699,7 @@ func TestExtraBidWithBidAdjustments(t *testing.T) {
BidType: openrtb_ext.BidTypeVideo,
OriginalBidCPM: 7,
OriginalBidCur: "USD",
BidMeta: &openrtb_ext.ExtBidPrebidMeta{AdapterCode: string(openrtb_ext.BidderPubmatic)},
BidMeta: &openrtb_ext.ExtBidPrebidMeta{AdapterCode: "PUBMATIC"},
}},
Seat: "groupm",
Currency: "USD",
Expand All @@ -2715,9 +2715,9 @@ func TestExtraBidWithBidAdjustments(t *testing.T) {
BidType: openrtb_ext.BidTypeBanner,
OriginalBidCur: "USD",
OriginalBidCPM: 3,
BidMeta: &openrtb_ext.ExtBidPrebidMeta{AdapterCode: string(openrtb_ext.BidderPubmatic)},
BidMeta: &openrtb_ext.ExtBidPrebidMeta{AdapterCode: "PUBMATIC"},
}},
Seat: string(openrtb_ext.BidderPubmatic),
Seat: "PUBMATIC",
Currency: "USD",
},
}
Expand All @@ -2727,10 +2727,10 @@ func TestExtraBidWithBidAdjustments(t *testing.T) {

bidderReq := BidderRequest{
BidRequest: &openrtb2.BidRequest{Imp: []openrtb2.Imp{{ID: "impId"}}},
BidderName: openrtb_ext.BidderPubmatic,
BidderName: "PUBMATIC",
}
bidAdjustments := map[string]float64{
string(openrtb_ext.BidderPubmatic): 2,
string(openrtb_ext.BidderPubmatic): 2, // All lowercase value in bid adjustments to simulate it being case insensitive
"groupm": 3,
}

Expand All @@ -2745,7 +2745,7 @@ func TestExtraBidWithBidAdjustments(t *testing.T) {
openrtb_ext.ExtAlternateBidderCodes{
Enabled: true,
Bidders: map[string]openrtb_ext.ExtAdapterAlternateBidderCodes{
string(openrtb_ext.BidderPubmatic): {
"PUBMATIC": {
Enabled: true,
AllowedBidderCodes: []string{"groupm"},
},
Expand Down
9 changes: 7 additions & 2 deletions exchange/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"math/rand"
"strings"

"github.com/buger/jsonparser"
"github.com/prebid/go-gdpr/vendorconsent"
Expand Down Expand Up @@ -892,8 +893,12 @@ func parseRequestDebugValues(test int8, requestExtPrebid *openrtb_ext.ExtRequest
}

func getExtBidAdjustmentFactors(requestExtPrebid *openrtb_ext.ExtRequestPrebid) map[string]float64 {
if requestExtPrebid != nil {
return requestExtPrebid.BidAdjustmentFactors
if requestExtPrebid != nil && requestExtPrebid.BidAdjustmentFactors != nil {
caseInsensitiveMap := make(map[string]float64, len(requestExtPrebid.BidAdjustmentFactors))
for bidder, bidAdjFactor := range requestExtPrebid.BidAdjustmentFactors {
caseInsensitiveMap[strings.ToLower(bidder)] = bidAdjFactor
}
return caseInsensitiveMap
}
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions exchange/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,11 @@ func TestGetExtBidAdjustmentFactors(t *testing.T) {
requestExtPrebid: &openrtb_ext.ExtRequestPrebid{BidAdjustmentFactors: map[string]float64{"bid-factor": 1.0}},
outBidAdjustmentFactors: map[string]float64{"bid-factor": 1.0},
},
{
desc: "BidAdjustmentFactors contains uppercase bidders, expect case insensitve map returned",
requestExtPrebid: &openrtb_ext.ExtRequestPrebid{BidAdjustmentFactors: map[string]float64{"Bidder": 1.0, "APPNEXUS": 2.0}},
outBidAdjustmentFactors: map[string]float64{"bidder": 1.0, "appnexus": 2.0},
},
}
for _, test := range testCases {
actualBidAdjustmentFactors := getExtBidAdjustmentFactors(test.requestExtPrebid)
Expand Down

0 comments on commit 72b160b

Please sign in to comment.