From a09612808878958b9350c04aafd57699c177aace Mon Sep 17 00:00:00 2001 From: Aadesh Patel Date: Mon, 25 Nov 2019 19:31:19 +0000 Subject: [PATCH 1/2] require the caller to define native assets[...].ID --- endpoints/openrtb2/auction.go | 30 ++++++++++++++----- .../invalid-native/assets-with-dup-ids.json | 18 +++++++++++ .../assets-with-partial-ids.json | 17 +++++++++++ .../asset-with-id.json | 7 ++--- .../valid-native/asset-with-no-id.json | 11 +++++++ .../valid-native/assets-with-unique-ids.json | 18 +++++++++++ 6 files changed, 89 insertions(+), 12 deletions(-) create mode 100644 endpoints/openrtb2/sample-requests/invalid-native/assets-with-dup-ids.json create mode 100644 endpoints/openrtb2/sample-requests/invalid-native/assets-with-partial-ids.json rename endpoints/openrtb2/sample-requests/{invalid-native => valid-native}/asset-with-id.json (60%) create mode 100644 endpoints/openrtb2/sample-requests/valid-native/asset-with-no-id.json create mode 100644 endpoints/openrtb2/sample-requests/valid-native/assets-with-unique-ids.json diff --git a/endpoints/openrtb2/auction.go b/endpoints/openrtb2/auction.go index fd1803597c3..38fd6fec50a 100644 --- a/endpoints/openrtb2/auction.go +++ b/endpoints/openrtb2/auction.go @@ -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) + 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 diff --git a/endpoints/openrtb2/sample-requests/invalid-native/assets-with-dup-ids.json b/endpoints/openrtb2/sample-requests/invalid-native/assets-with-dup-ids.json new file mode 100644 index 00000000000..dfece8cfb0e --- /dev/null +++ b/endpoints/openrtb2/sample-requests/invalid-native/assets-with-dup-ids.json @@ -0,0 +1,18 @@ +{ + "context": 1, + "plcmttype": 1, + "assets": [ + { + "id": 1, + "img": { + "wmin": 30 + } + }, + { + "id": 1, + "title": { + "len": 20 + } + } + ] +} diff --git a/endpoints/openrtb2/sample-requests/invalid-native/assets-with-partial-ids.json b/endpoints/openrtb2/sample-requests/invalid-native/assets-with-partial-ids.json new file mode 100644 index 00000000000..43e2369f6d4 --- /dev/null +++ b/endpoints/openrtb2/sample-requests/invalid-native/assets-with-partial-ids.json @@ -0,0 +1,17 @@ +{ + "context": 1, + "plcmttype": 1, + "assets": [ + { + "id": 1, + "img": { + "wmin": 30 + } + }, + { + "title": { + "len": 20 + } + } + ] +} diff --git a/endpoints/openrtb2/sample-requests/invalid-native/asset-with-id.json b/endpoints/openrtb2/sample-requests/valid-native/asset-with-id.json similarity index 60% rename from endpoints/openrtb2/sample-requests/invalid-native/asset-with-id.json rename to endpoints/openrtb2/sample-requests/valid-native/asset-with-id.json index 6f2eee53166..7c55888ba29 100644 --- a/endpoints/openrtb2/sample-requests/invalid-native/asset-with-id.json +++ b/endpoints/openrtb2/sample-requests/valid-native/asset-with-id.json @@ -3,11 +3,10 @@ "plcmttype": 1, "assets": [ { - "id": 2, + "id": 1, "img": { - "hmin": 30, - "wmin": 20 + "wmin": 30 } } ] -} \ No newline at end of file +} diff --git a/endpoints/openrtb2/sample-requests/valid-native/asset-with-no-id.json b/endpoints/openrtb2/sample-requests/valid-native/asset-with-no-id.json new file mode 100644 index 00000000000..a9bc57ea274 --- /dev/null +++ b/endpoints/openrtb2/sample-requests/valid-native/asset-with-no-id.json @@ -0,0 +1,11 @@ +{ + "context": 1, + "plcmttype": 1, + "assets": [ + { + "img": { + "wmin": 30 + } + } + ] +} diff --git a/endpoints/openrtb2/sample-requests/valid-native/assets-with-unique-ids.json b/endpoints/openrtb2/sample-requests/valid-native/assets-with-unique-ids.json new file mode 100644 index 00000000000..10692b9aaf2 --- /dev/null +++ b/endpoints/openrtb2/sample-requests/valid-native/assets-with-unique-ids.json @@ -0,0 +1,18 @@ +{ + "context": 1, + "plcmttype": 1, + "assets": [ + { + "id": 1, + "img": { + "wmin": 30 + } + }, + { + "id": 2, + "title": { + "len": 20 + } + } + ] +} From 5b6670e7532208347f2ed67e65fb952d17999108 Mon Sep 17 00:00:00 2001 From: hhhjort <31041505+hhhjort@users.noreply.github.com> Date: Fri, 6 Dec 2019 09:08:52 -0500 Subject: [PATCH 2/2] Update assets-with-partial-ids.json --- .../invalid-native/assets-with-partial-ids.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/endpoints/openrtb2/sample-requests/invalid-native/assets-with-partial-ids.json b/endpoints/openrtb2/sample-requests/invalid-native/assets-with-partial-ids.json index 43e2369f6d4..291ae8d77b1 100644 --- a/endpoints/openrtb2/sample-requests/invalid-native/assets-with-partial-ids.json +++ b/endpoints/openrtb2/sample-requests/invalid-native/assets-with-partial-ids.json @@ -12,6 +12,11 @@ "title": { "len": 20 } + }, + { + "img": { + "wmin": 50 + } } ] }