-
Notifications
You must be signed in to change notification settings - Fork 805
Request Provided Currency Rates #1753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 14 commits
2989689
4d3196c
f50db9f
1e69876
40afa09
5605a98
57dde30
8b17152
86b4350
95c82d3
177e1e1
c1f136a
c0c81d7
5376694
348d83b
ed48b9c
dc36e68
8f7df0a
3731241
00dda98
72aa90c
a7bba04
9adf8b2
11fa5d2
6ff91ed
e454459
b60b68f
561dff8
b3db4a2
663dde0
32a3a22
c9ee8e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
"net/url" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/buger/jsonparser" | ||
|
@@ -37,6 +38,7 @@ import ( | |
"github.com/prebid/prebid-server/util/httputil" | ||
"github.com/prebid/prebid-server/util/iputil" | ||
"golang.org/x/net/publicsuffix" | ||
"golang.org/x/text/currency" | ||
SyntaxNode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
const storedRequestTimeoutMillis = 50 | ||
|
@@ -340,6 +342,10 @@ func (deps *endpointDeps) validateRequest(req *openrtb2.BidRequest) []error { | |
if err := deps.validateEidPermissions(bidExt, aliases); err != nil { | ||
return []error{err} | ||
} | ||
|
||
if err := validateCustomRates(bidExt.Prebid.BidReqConversions); err != nil { | ||
return []error{err} | ||
} | ||
} | ||
|
||
if (req.Site == nil && req.App == nil) || (req.Site != nil && req.App != nil) { | ||
|
@@ -434,6 +440,50 @@ func validateSChains(req *openrtb_ext.ExtRequest) error { | |
return err | ||
} | ||
|
||
// validateCustomRates discards invalid 3-digit currency codes found in the bidRequest.ext.prebid.currency | ||
// field. If all of them were discarded and bidRequest.ext.prebid.currency was set to false, it means | ||
// we have no currencies to work with and this function returns a fatal BadInput error | ||
func validateCustomRates(bidReqCurrencyRates *openrtb_ext.ExtRequestCurrency) error { | ||
if bidReqCurrencyRates == nil { | ||
SyntaxNode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil | ||
} | ||
|
||
var badCurrencyCodes []string | ||
|
||
for fromCurrency, rates := range bidReqCurrencyRates.ConversionRates { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: Extra line break. |
||
// Check if fromCurrency is a valid 3-letter currency code | ||
if _, err := currency.ParseISO(fromCurrency); err != nil { | ||
badCurrencyCodes = append(badCurrencyCodes, fromCurrency) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we throw errors on invalid currency codes instead of silently removing them? .. or perhaps throw a warning? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR is throwing a fatal error only if all of the custom currency codes were found to be wrong. The reason this approach made more sense to me is that we ignore malformed or inaccurate 3-digit currency codes inside
But, I'm open to ideas here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's seek out @hhhjort @mansinahar @bsardo for additional thoughts here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I agree with @SyntaxNode here that we should at least append an error/warning about the bad currency codes so that the user knows what exactly the problem was because right now we just remove them which might eventually lead to a currency not found error and that doesn't indicate the exact issue. Consider for example, someone wanted to provide and override for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modified this function to throw fatal bad input error at the first invalid or malformed currency code found in the map |
||
delete(bidReqCurrencyRates.ConversionRates, fromCurrency) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once we've recognized the current There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Added a |
||
} | ||
|
||
// Check if currencies mapped to fromCurrency are valid 3-letter currency codes | ||
for toCurrency := range rates { | ||
if _, err := currency.ParseISO(toCurrency); err != nil { | ||
badCurrencyCodes = append(badCurrencyCodes, toCurrency) | ||
delete(rates, toCurrency) | ||
} | ||
} | ||
|
||
// If all of fromCurrency's mapped currencies were invalid, remove fromCurrency map entry | ||
if len(rates) == 0 { | ||
delete(bidReqCurrencyRates.ConversionRates, fromCurrency) | ||
} | ||
} | ||
|
||
// If we are retricted to only use custom currency rates but the currency rate map is empty, return error | ||
customRatesOnly := bidReqCurrencyRates.UsePBSRates != nil && !(*bidReqCurrencyRates.UsePBSRates) | ||
if customRatesOnly && len(bidReqCurrencyRates.ConversionRates) == 0 { | ||
if len(badCurrencyCodes) > 0 { | ||
return &errortypes.BadInput{Message: fmt.Sprintf("Required custom currency rates are all invalid. %s", strings.Join(badCurrencyCodes, ","))} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might I suggest a different wording, to key on that this is a required field only because UsePBSRates is set to true in their request and that possibly not all currency names are invalid. Something along the lines of:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But to reach this branch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that's necessarily true. As the code is currently, a
If I'm reading the code correctly, this will cause the error message to be reached when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part of the code has been removed because we are now immediately returning a bad input error when the first non-valid code is found. |
||
} else { | ||
return &errortypes.BadInput{Message: "Required custom currency rates field is empty"} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (deps *endpointDeps) validateEidPermissions(req *openrtb_ext.ExtRequest, aliases map[string]string) error { | ||
if req == nil || req.Prebid.Data == nil { | ||
return nil | ||
|
Uh oh!
There was an error while loading. Please reload this page.