Skip to content

Commit

Permalink
require the caller to define native assets[...].ID (prebid#1123)
Browse files Browse the repository at this point in the history
* require the caller to define native assets[...].ID

* Update assets-with-partial-ids.json
  • Loading branch information
Aadeshp authored and mansinahar committed Apr 15, 2020
1 parent 7e8688e commit 0187da9
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 12 deletions.
30 changes: 22 additions & 8 deletions endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,23 +499,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)
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": {
"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 @@
{
"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
}
}
]
}

0 comments on commit 0187da9

Please sign in to comment.