Skip to content
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

require the caller to define native assets[...].ID #1123

Merged
merged 2 commits into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,23 +491,37 @@ func fillAndValidateNativeAssets(assets []nativeRequests.Asset, impIndex int) er
return fmt.Errorf("request.imp[%d].native.request.assets must be an array containing at least one object", impIndex)
}

assetIDs := make(map[int64]struct{}, len(assets))

// If none of the asset IDs are defined by the caller, then prebid server should assign its own unique IDs. But
// if the caller did assign its own asset IDs, then prebid server will respect those IDs
assignAssetIDs := true
for i := 0; i < len(assets); i++ {
assignAssetIDs = assignAssetIDs && (assets[i].ID == 0)
}

for i := 0; i < len(assets); i++ {
// Per the OpenRTB spec docs, this is a "unique asset ID, assigned by exchange. Typically a counter for the array"
// To avoid conflict with the Request, we'll return a 400 if the Request _did_ define this ID,
// and then populate it as the spec suggests.
if err := validateNativeAsset(assets[i], impIndex, i); err != nil {
return err
}
assets[i].ID = int64(i)

if assignAssetIDs {
assets[i].ID = int64(i)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the code it seems like we expect that asset IDs are either specified or not specified for all assets i.e it can't be that they are specified for some and not for others, right?

I am not a 100% sure if we can make that assumption or not so just wanted to call it out and make sure that indeed is the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats indeed the case

continue
}

// Each asset should have a unique ID thats assigned by the caller
if _, ok := assetIDs[assets[i].ID]; ok {
return fmt.Errorf("request.imp[%d].native.request.assets[%d].id is already being used by another asset. Each asset ID must be unique.", impIndex, i)
}

assetIDs[assets[i].ID] = struct{}{}
}

return nil
}

func validateNativeAsset(asset nativeRequests.Asset, impIndex int, assetIndex int) error {
if asset.ID != 0 {
return fmt.Errorf(`request.imp[%d].native.request.assets[%d].id must not be defined. Prebid Server will set this automatically, using the index of the asset in the array as the ID`, impIndex, assetIndex)
}

assetErr := "request.imp[%d].native.request.assets[%d] must define exactly one of {title, img, video, data}"
foundType := false

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"context": 1,
"plcmttype": 1,
"assets": [
{
"id": 1,
"img": {
"wmin": 30
}
},
{
"id": 1,
"title": {
"len": 20
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"context": 1,
"plcmttype": 1,
"assets": [
{
"id": 1,
"img": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering.. is this test case actually for an invalid native request? Based on our assumption that there's gonna be asset IDs either specified or not specified for all assets and that we don't expect partial IDs to be specified, this would actually be a valid-native test case, no?

I guess it gets a little tricky when we consider assets with partial IDs set. We will treat them as if all asset IDs are set and so the assets that don't have asset IDs set will have zero values and we will therefore consider them not being unique asset IDs and return an error which is a fair behavior. But, if only one of the assets doesn't have an ID set, like in this case, then we won't return an error because one asset with a 0 asset ID is still valid. Therefore, this example shouldn't really return an error, correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And looks like that is actually one of the tests that's failing:

--- FAIL: TestBadNativeRequests (0.38s)
    auction_test.go:175: Expected a 400 response from sample-requests/invalid-native/assets-with-partial-ids.json. Got 200: {"id":"req-id","bidid":"test bid id","nbr":0}

"wmin": 30
}
},
{
"title": {
"len": 20
}
},
{
"img": {
"wmin": 50
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
"plcmttype": 1,
"assets": [
{
"id": 2,
"id": 1,
"img": {
"hmin": 30,
"wmin": 20
"wmin": 30
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please add some more test cases like for example, one with multiple assets with unique asset IDs, one with multiple assets but with non-unique asset IDs, one with multiple assets with unique asset IDs but one of the assets having asset ID 0.

"context": 1,
"plcmttype": 1,
"assets": [
{
"img": {
"wmin": 30
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"context": 1,
"plcmttype": 1,
"assets": [
{
"id": 1,
"img": {
"wmin": 30
}
},
{
"id": 2,
"title": {
"len": 20
}
}
]
}