diff --git a/adapters/ortbbidder/bidderParamMapper.go b/adapters/ortbbidder/bidderParamMapper.go index f618d6544d9..d14762e1f65 100644 --- a/adapters/ortbbidder/bidderParamMapper.go +++ b/adapters/ortbbidder/bidderParamMapper.go @@ -16,6 +16,9 @@ const ( extKey = "ext" bidderKey = "bidder" reqExtPath = "req." + appsiteKey = "appsite" + siteKey = "site" + appKey = "app" ) // mapper struct holds mappings for bidder parameters and bid responses. @@ -196,7 +199,7 @@ func mapBidderParamsInRequest(requestBody []byte, bidderParamDetails map[string] if !ok { continue } - // TODO: handle app/site + details = applyConditionalMapping(requestBodyMap, details) // set the value in the requestBody according to the mapping details and remove the parameter if successful. if setValue(requestBodyMap, details.location, paramValue) { delete(bidderParams, paramName) @@ -217,3 +220,19 @@ func mapBidderParamsInRequest(requestBody []byte, bidderParamDetails map[string] } return requestBody, nil } + +// applyConditionalMapping applies the custom rules and updates the location of bidder-param +func applyConditionalMapping(requestBodyMap map[string]any, details paramDetails) paramDetails { + if len(details.location) == 0 || len(requestBodyMap) == 0 { + return details + } + // if location is "appsite" and if request contains "app" object then set location to "app" else set location to "site" + // example - if req.site is present and location is {"appsite","publisher","id"} then update location to {"site","publisher","id"} + if details.location[0] == appsiteKey { + details.location[0] = siteKey + if _, found := requestBodyMap[appKey]; found { + details.location[0] = appKey + } + } + return details +} diff --git a/adapters/ortbbidder/bidderParamMapper_test.go b/adapters/ortbbidder/bidderParamMapper_test.go index 7d504301a3e..434159e1a6b 100644 --- a/adapters/ortbbidder/bidderParamMapper_test.go +++ b/adapters/ortbbidder/bidderParamMapper_test.go @@ -656,6 +656,32 @@ func Test_mapBidderParamsInRequest(t *testing.T) { requestBody: json.RawMessage(`{"adunit":{"id":123},"app":{"ext":{"profile":1,"pubid":5890},"name":"sampleapp"},"imp":[{"ext":{"bidder":{"paramWithoutLocation":"value"}},"tagid":"test_slot"}]}`), }, }, + { + name: "conditional_mapping_set_app_object", + args: args{ + requestBody: json.RawMessage(`{"app":{"name":"sampleapp"},"imp":[{"tagid":"oldtagid","ext":{"bidder":{"paramWithoutLocation":"value","adunit":123,"slot":"test_slot","wrapper":{"pubid":5890,"profile":1}}}}]}`), + mapper: map[string]paramDetails{ + "wrapper": {location: []string{"appsite", "wrapper"}}, + }, + }, + want: want{ + err: "", + requestBody: json.RawMessage(`{"app":{"name":"sampleapp","wrapper":{"profile":1,"pubid":5890}},"imp":[{"ext":{"bidder":{"adunit":123,"paramWithoutLocation":"value","slot":"test_slot"}},"tagid":"oldtagid"}]}`), + }, + }, + { + name: "conditional_mapping_set_site_object", + args: args{ + requestBody: json.RawMessage(`{"site":{"name":"sampleapp"},"imp":[{"tagid":"oldtagid","ext":{"bidder":{"paramWithoutLocation":"value","adunit":123,"slot":"test_slot","wrapper":{"pubid":5890,"profile":1}}}}]}`), + mapper: map[string]paramDetails{ + "wrapper": {location: []string{"appsite", "wrapper"}}, + }, + }, + want: want{ + err: "", + requestBody: json.RawMessage(`{"imp":[{"ext":{"bidder":{"adunit":123,"paramWithoutLocation":"value","slot":"test_slot"}},"tagid":"oldtagid"}],"site":{"name":"sampleapp","wrapper":{"profile":1,"pubid":5890}}}`), + }, + }, { name: "multi_imps_bidder_params_mapping", args: args{ @@ -814,3 +840,92 @@ func Test_readFile(t *testing.T) { }) } } + +func Test_applyConditionalMapping(t *testing.T) { + type args struct { + requestBodyMap map[string]any + details paramDetails + } + tests := []struct { + name string + args args + want paramDetails + }{ + { + name: "empty_location_for_bidder_param", + args: args{ + requestBodyMap: map[string]any{ + "app": map[string]any{}, + }, + details: paramDetails{}, + }, + want: paramDetails{}, + }, + { + name: "empty_request_body", + args: args{ + requestBodyMap: map[string]any{}, + details: paramDetails{ + location: []string{"appsite", "publisher", "id"}, + }, + }, + want: paramDetails{ + location: []string{"appsite", "publisher", "id"}, + }, + }, + { + name: "app_object_present_in_request_body", + args: args{ + requestBodyMap: map[string]any{ + "app": map[string]any{ + "name": "sample_app", + }, + }, + details: paramDetails{ + location: []string{"appsite", "publisher", "id"}, + }, + }, + want: paramDetails{ + location: []string{"app", "publisher", "id"}, + }, + }, + { + name: "app_object_absent_in_request_body", + args: args{ + requestBodyMap: map[string]any{ + "device": map[string]any{ + "name": "sample_app", + }, + }, + details: paramDetails{ + location: []string{"appsite", "publisher", "id"}, + }, + }, + want: paramDetails{ + location: []string{"site", "publisher", "id"}, + }, + }, + { + name: "site_object_present_in_request_body", + args: args{ + requestBodyMap: map[string]any{ + "site": map[string]any{ + "name": "sample_app", + }, + }, + details: paramDetails{ + location: []string{"appsite", "publisher", "id"}, + }, + }, + want: paramDetails{ + location: []string{"site", "publisher", "id"}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := applyConditionalMapping(tt.args.requestBodyMap, tt.args.details) + assert.Equal(t, tt.want, got, "mismatched paramDetails") + }) + } +} diff --git a/adapters/ortbbidder/ortbbidder_test.go b/adapters/ortbbidder/ortbbidder_test.go index 4e2b899a2d6..987784a0085 100644 --- a/adapters/ortbbidder/ortbbidder_test.go +++ b/adapters/ortbbidder/ortbbidder_test.go @@ -420,25 +420,6 @@ func Test_prepareRequestData(t *testing.T) { err: fmt.Errorf("found nil request"), }, }, - // { - // name: "fail_to_map_bidder_param_in_request", - // fields: fields{ - // Adapter: config.Adapter{Endpoint: "https://example.com"}, - // }, - // args: args{ - // request: &openrtb2.BidRequest{ - // ID: "123", - // Imp: []openrtb2.Imp{{ID: "imp1", Ext: json.RawMessage(`{"bidder":{"adunit":{{}}}}}}}`)}}, - // }, - // mapper: map[string]paramDetails{ - // "adunit": {location: []string{"invalid-location"}}, - // }, - // }, - // want: want{ - // requestData: nil, - // err: fmt.Errorf("error:[invalid_bidder_param_location] param:[adunit] location:[invalid-location]"), - // }, - // }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/main.go b/main.go index 596c979de92..4ae35e57b33 100644 --- a/main.go +++ b/main.go @@ -24,7 +24,7 @@ func init() { } // TODO: revert this after PBS-OpenWrap module -func main() { +func Main() { flag.Parse() // required for glog flags and testing package flags bidderInfoPath, err := filepath.Abs(infoDirectory)