Skip to content

Commit

Permalink
add support of conditional mapping for appsite object
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishshinde-pubm committed May 9, 2024
1 parent 5dc3e9d commit 390f384
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 21 deletions.
21 changes: 20 additions & 1 deletion adapters/ortbbidder/bidderParamMapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
115 changes: 115 additions & 0 deletions adapters/ortbbidder/bidderParamMapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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")
})
}
}
19 changes: 0 additions & 19 deletions adapters/ortbbidder/ortbbidder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 390f384

Please sign in to comment.