From 55fcb1c0824d8d0d64565655455cb255bf1b3685 Mon Sep 17 00:00:00 2001 From: Ashish Garg Date: Tue, 5 Sep 2023 11:20:32 +0530 Subject: [PATCH] Fix few adapter aliases bug (#3055) --- config/bidderinfo.go | 28 ++++++++++++++++++---------- config/bidderinfo_test.go | 20 ++++++++------------ openrtb_ext/bidders.go | 8 ++++++-- router/router.go | 9 +++++++++ router/router_test.go | 6 ++++-- 5 files changed, 45 insertions(+), 26 deletions(-) diff --git a/config/bidderinfo.go b/config/bidderinfo.go index f2bf9ea1a6e..96d6fd15bfc 100644 --- a/config/bidderinfo.go +++ b/config/bidderinfo.go @@ -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 { @@ -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 } } } @@ -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 diff --git a/config/bidderinfo_test.go b/config/bidderinfo_test.go index f79e4ec5bea..77625c0cdd0 100644 --- a/config/bidderinfo_test.go +++ b/config/bidderinfo_test.go @@ -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{ @@ -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 { @@ -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")) } } } diff --git a/openrtb_ext/bidders.go b/openrtb_ext/bidders.go index 4e4927ff3f5..c60058ebb38 100644 --- a/openrtb_ext/bidders.go +++ b/openrtb_ext/bidders.go @@ -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) @@ -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 } diff --git a/router/router.go b/router/router.go index fcf674b4143..f7270445ff4 100644 --- a/router/router.go +++ b/router/router.go @@ -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 { @@ -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] diff --git a/router/router_test.go b/router/router_test.go index 2b4ff7fd3e7..41c2a724c91 100644 --- a/router/router_test.go +++ b/router/router_test.go @@ -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) @@ -59,6 +60,7 @@ func TestNewJsonDirectoryServer(t *testing.T) { } ensureHasKey(t, data, "aliastest") + ensureHasKey(t, data, "alias") } func TestCheckSupportedUserSyncEndpoints(t *testing.T) {