Skip to content

Commit

Permalink
Fix few adapter aliases bug (#3055)
Browse files Browse the repository at this point in the history
  • Loading branch information
gargcreation1992 authored Sep 5, 2023
1 parent 47f840d commit 55fcb1c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 26 deletions.
28 changes: 18 additions & 10 deletions config/bidderinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,11 @@ func processBidderInfos(reader InfoReader, normalizeBidderName func(string) (ope
for fileName, data := range bidderConfigs {
bidderName := strings.Split(fileName, ".")
if len(bidderName) == 2 && bidderName[1] == "yaml" {
normalizedBidderName, bidderNameExists := normalizeBidderName(bidderName[0])
if !bidderNameExists {
return nil, fmt.Errorf("error parsing config for bidder %s: unknown bidder", fileName)
}
info := BidderInfo{}
if err := yaml.Unmarshal(data, &info); err != nil {
return nil, fmt.Errorf("error parsing config for bidder %s: %v", fileName, err)
}

bidderInfos[string(normalizedBidderName)] = info

//need to maintain nullable fields from BidderInfo struct into bidderInfoNullableFields
//to handle the default values in aliases yaml
if len(info.AliasOf) > 0 {
Expand All @@ -260,7 +254,25 @@ func processBidderInfos(reader InfoReader, normalizeBidderName func(string) (ope
return nil, fmt.Errorf("error parsing config for aliased bidder %s: %v", fileName, err)
}

//required for CoreBidderNames function to also return aliasBiddernames
if err := openrtb_ext.SetAliasBidderName(bidderName[0], openrtb_ext.BidderName(info.AliasOf)); err != nil {
return nil, err
}

normalizedBidderName, bidderNameExists := normalizeBidderName(bidderName[0])
if !bidderNameExists {
return nil, fmt.Errorf("error parsing config for an alias %s: unknown bidder", fileName)
}

aliasNillableFieldsByBidder[string(normalizedBidderName)] = aliasFields
bidderInfos[string(normalizedBidderName)] = info
} else {
normalizedBidderName, bidderNameExists := normalizeBidderName(bidderName[0])
if !bidderNameExists {
return nil, fmt.Errorf("error parsing config for bidder %s: unknown bidder", fileName)
}

bidderInfos[string(normalizedBidderName)] = info
}
}
}
Expand All @@ -277,10 +289,6 @@ func processBidderAliases(aliasNillableFieldsByBidder map[string]aliasNillableFi
return nil, err
}

//required for CoreBidderNames function to also return aliasBiddernames
if err := openrtb_ext.SetAliasBidderName(bidderName, openrtb_ext.BidderName(aliasBidderInfo.AliasOf)); err != nil {
return nil, err
}
parentBidderInfo := bidderInfos[aliasBidderInfo.AliasOf]
if aliasBidderInfo.AppSecret == "" {
aliasBidderInfo.AppSecret = parentBidderInfo.AppSecret
Expand Down
20 changes: 8 additions & 12 deletions config/bidderinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ func TestProcessBidderInfo(t *testing.T) {
expectedBidderInfos: nil,
expectError: "error parsing config for bidder bidderA.yaml",
},
{
description: "Invalid alias name",
bidderInfos: map[string][]byte{
"all.yaml": []byte(testSimpleAliasYAML),
},
expectedBidderInfos: nil,
expectError: "alias all is a reserved bidder name and cannot be used",
},
{
description: "Valid aliases",
bidderInfos: map[string][]byte{
Expand Down Expand Up @@ -434,17 +442,6 @@ func TestProcessAliasBidderInfo(t *testing.T) {
},
expectedErr: errors.New("bidder info not found for an alias: bidderB"),
},
{
description: "unable to set an alias",
aliasInfos: map[string]aliasNillableFields{
"all": {},
},
bidderInfos: BidderInfos{
"bidderA": parentBidderInfo,
"all": aliasBidderInfo,
},
expectedErr: errors.New("alias all is a reserved bidder name and cannot be used"),
},
}

for _, test := range testCases {
Expand All @@ -453,7 +450,6 @@ func TestProcessAliasBidderInfo(t *testing.T) {
assert.Equal(t, test.expectedErr, err)
} else {
assert.Equal(t, test.expectedBidderInfos, bidderInfos)
assert.Contains(t, openrtb_ext.CoreBidderNames(), openrtb_ext.BidderName("bidderB"))
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions openrtb_ext/bidders.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ var coreBidderNames []BidderName = []BidderName{
BidderZetaGlobalSsp,
}

func GetAliasBidderToParent() map[BidderName]BidderName {
return aliasBidderToParent
}

func SetAliasBidderName(aliasBidderName string, parentBidderName BidderName) error {
if IsBidderNameReserved(aliasBidderName) {
return fmt.Errorf("alias %s is a reserved bidder name and cannot be used", aliasBidderName)
Expand Down Expand Up @@ -552,11 +556,11 @@ var bidderNameLookup = func() map[string]BidderName {
lookup[bidderNameLower] = name
}
return lookup
}()
}

func NormalizeBidderName(name string) (BidderName, bool) {
nameLower := strings.ToLower(name)
bidderName, exists := bidderNameLookup[nameLower]
bidderName, exists := bidderNameLookup()[nameLower]
return bidderName, exists
}

Expand Down
9 changes: 9 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ import (
// This function stores the file contents in memory, and should not be used on large directories.
// If the root directory, or any of the files in it, cannot be read, then the program will exit.
func NewJsonDirectoryServer(schemaDirectory string, validator openrtb_ext.BidderParamValidator, aliases map[string]string) httprouter.Handle {
return newJsonDirectoryServer(schemaDirectory, validator, aliases, openrtb_ext.GetAliasBidderToParent())
}

func newJsonDirectoryServer(schemaDirectory string, validator openrtb_ext.BidderParamValidator, aliases map[string]string, yamlAliases map[openrtb_ext.BidderName]openrtb_ext.BidderName) httprouter.Handle {
// Slurp the files into memory first, since they're small and it minimizes request latency.
files, err := os.ReadDir(schemaDirectory)
if err != nil {
Expand All @@ -73,6 +77,11 @@ func NewJsonDirectoryServer(schemaDirectory string, validator openrtb_ext.Bidder
data[bidder] = json.RawMessage(validator.Schema(bidderName))
}

// Add in any aliases
for aliasName, parentBidder := range yamlAliases {
data[string(aliasName)] = json.RawMessage(validator.Schema(parentBidder))
}

// Add in any default aliases
for aliasName, bidderName := range aliases {
bidderData, ok := data[bidderName]
Expand Down
6 changes: 4 additions & 2 deletions router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func ensureHasKey(t *testing.T, data map[string]json.RawMessage, key string) {
}

func TestNewJsonDirectoryServer(t *testing.T) {
alias := map[string]string{"aliastest": "appnexus"}
handler := NewJsonDirectoryServer("../static/bidder-params", &testValidator{}, alias)
defaultAlias := map[string]string{"aliastest": "appnexus"}
yamlAlias := map[openrtb_ext.BidderName]openrtb_ext.BidderName{openrtb_ext.BidderName("alias"): openrtb_ext.BidderName("parentAlias")}
handler := newJsonDirectoryServer("../static/bidder-params", &testValidator{}, defaultAlias, yamlAlias)
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/whatever", nil)
handler(recorder, request, nil)
Expand All @@ -59,6 +60,7 @@ func TestNewJsonDirectoryServer(t *testing.T) {
}

ensureHasKey(t, data, "aliastest")
ensureHasKey(t, data, "alias")
}

func TestCheckSupportedUserSyncEndpoints(t *testing.T) {
Expand Down

0 comments on commit 55fcb1c

Please sign in to comment.