openrtb/v2_5/
bid_response.rs

1// Copyright (c) 2018 The openrtb-rust authors
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use super::seat_bid::SeatBid;
10use serde_utils;
11
12// 4.2.1 Object: BidResponse
13//
14// This object is the top-level bid response object (i.e., the unnamed outer
15// JSON object). The id attribute is a reflection of the bid request ID for
16// logging purposes. Similarly, bidid is an optional response tracking ID for
17// bidders. If specified, it can be included in the subsequent win notice call
18// if the bidder wins. At least one seatbid object is required, which contains
19// at least one bid for an impression. Other attributes are optional.
20// To express a “no-bid”, the options are to return an empty response with
21// HTTP 204. Alternately if the bidder wishes to convey to the exchange a
22// reason for not bidding, just a BidResponse object is returned with a reason
23// code in the nbr attribute.
24#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
25pub struct BidResponse {
26    // Required. ID of the bid request to which this is a response.
27    pub id: String,
28
29    #[serde(rename = "seatbid", default, skip_serializing_if = "Vec::is_empty")]
30    pub seat_bid: Vec<SeatBid>,
31
32    #[serde(rename = "bidid", skip_serializing_if = "Option::is_none")]
33    pub bid_id: Option<String>,
34
35    #[serde(rename = "cur", skip_serializing_if = "Option::is_none")]
36    pub currency: Option<String>,
37
38    #[serde(rename = "customdata", skip_serializing_if = "Option::is_none")]
39    pub custom_data: Option<String>,
40
41    #[serde(rename = "nbr", skip_serializing_if = "Option::is_none")]
42    pub no_bidding_reason: Option<u32>,
43
44    // Placeholder for exchange-specific extensions to OpenRTB.
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub ext: Option<serde_utils::Ext>,
47}
48
49impl BidResponse {
50    pub fn new(id: String) -> BidResponse {
51        BidResponse {
52            id: id,
53            seat_bid: vec![],
54            bid_id: None,
55            currency: None,
56            custom_data: None,
57            no_bidding_reason: None,
58            ext: None,
59        }
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66    use serde_json;
67
68    #[test]
69    fn serialization_skip_fields() {
70        let b = BidResponse {
71            id: "1234".to_string(),
72            seat_bid: vec![],
73            bid_id: None,
74            currency: None,
75            custom_data: None,
76            no_bidding_reason: None,
77            ext: None,
78        };
79
80        let expected = r#"{"id":"1234"}"#;
81        let serialized = serde_json::to_string(&b).unwrap();
82
83        assert_eq!(expected, serialized)
84    }
85
86    #[test]
87    fn deserialize_defaults() {
88        let serialized = r#"{
89            "id": "1234"
90        }"#;
91
92        let res = serde_json::from_str(serialized);
93
94        let b: BidResponse = match res {
95            Ok(x) => x,
96            Err(e) => panic!("{:?}", e),
97        };
98
99        let expected = BidResponse {
100            id: "1234".to_string(),
101            seat_bid: vec![],
102            bid_id: None,
103            currency: None,
104            custom_data: None,
105            no_bidding_reason: None,
106            ext: None,
107        };
108
109        assert_eq!(expected.id, b.id);
110    }
111}