Skip to content

Commit eb2208e

Browse files
cleanup helper
1 parent 7ba6f3f commit eb2208e

4 files changed

Lines changed: 37 additions & 34 deletions

File tree

crates/common/src/auction/formats.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ use crate::auction::context::ContextValue;
1616
use crate::creative;
1717
use crate::error::TrustedServerError;
1818
use crate::geo::GeoInfo;
19-
use crate::openrtb::{
20-
maybe_object_from_serializable, OpenRtbBid, OpenRtbResponse, ResponseExt, SeatBid,
21-
};
19+
use crate::openrtb::{OpenRtbBid, OpenRtbResponse, ResponseExt, SeatBid, ToExt};
2220
use crate::settings::Settings;
2321
use crate::synthetic::{generate_synthetic_id, get_or_generate_synthetic_id};
2422

@@ -278,15 +276,16 @@ pub fn convert_to_openrtb_response(
278276
let response_body = OpenRtbResponse {
279277
id: auction_request.id.to_string(),
280278
seatbid: Some(seatbids),
281-
ext: maybe_object_from_serializable(&ResponseExt {
279+
ext: ResponseExt {
282280
orchestrator: OrchestratorExt {
283281
strategy: strategy_name.to_string(),
284282
providers: result.provider_responses.len(),
285283
total_bids: result.total_bids(),
286284
time_ms: result.total_time_ms,
287285
provider_details,
288286
},
289-
}),
287+
}
288+
.to_ext(),
290289
..Default::default()
291290
};
292291

crates/common/src/integrations/prebid.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use crate::integrations::{
2323
IntegrationRegistration,
2424
};
2525
use crate::openrtb::{
26-
maybe_object_from_serializable, Banner, Device, Format, Geo, Imp, ImpExt, OpenRtbRequest,
27-
PrebidExt, PrebidImpExt, Publisher, Regs, RequestExt, Site, TrustedServerExt, User, UserExt,
26+
Banner, Device, Format, Geo, Imp, ImpExt, OpenRtbRequest, PrebidExt, PrebidImpExt, Publisher,
27+
Regs, RequestExt, Site, ToExt, TrustedServerExt, User, UserExt,
2828
};
2929
use crate::request_signing::{RequestSigner, SigningParams, SIGNING_VERSION};
3030
use crate::settings::{IntegrationConfig, Settings};
@@ -605,9 +605,10 @@ impl PrebidAuctionProvider {
605605
bidfloorcur: slot.floor_price.map(|_| "USD".to_string()),
606606
secure: Some(1), // require HTTPS creatives
607607
tagid: Some(slot.id.clone()),
608-
ext: maybe_object_from_serializable(&ImpExt {
608+
ext: ImpExt {
609609
prebid: PrebidImpExt { bidder },
610-
}),
610+
}
611+
.to_ext(),
611612
..Default::default()
612613
}
613614
})
@@ -626,9 +627,10 @@ impl PrebidAuctionProvider {
626627
let user = Some(User {
627628
id: Some(request.user.id.clone()),
628629
consent: request.user.consent.clone(),
629-
ext: maybe_object_from_serializable(&UserExt {
630+
ext: UserExt {
630631
synthetic_fresh: Some(request.user.fresh_id.clone()),
631-
}),
632+
}
633+
.to_ext(),
632634
..Default::default()
633635
});
634636

@@ -725,7 +727,7 @@ impl PrebidAuctionProvider {
725727

726728
let debug_enabled = self.config.debug;
727729

728-
let ext = maybe_object_from_serializable(&RequestExt {
730+
let ext = RequestExt {
729731
prebid: Some(PrebidExt {
730732
debug: debug_enabled.then_some(true),
731733
returnallbidstatus: debug_enabled.then_some(true),
@@ -738,7 +740,8 @@ impl PrebidAuctionProvider {
738740
request_scheme: Some(request_info.scheme),
739741
ts,
740742
}),
741-
});
743+
}
744+
.to_ext();
742745

743746
// Extract Referer header for site.ref
744747
let referer = context

crates/common/src/openrtb.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
11
use serde::Serialize;
2-
use serde_json::{Map, Value};
2+
use serde_json::Value;
33

44
use crate::auction::types::OrchestratorExt;
55

6-
pub type Object = trusted_server_openrtb::Object;
76
pub type OpenRtbRequest = trusted_server_openrtb::BidRequest;
87
pub type OpenRtbResponse = trusted_server_openrtb::BidResponse;
98
pub type OpenRtbBid = trusted_server_openrtb::Bid;
109

1110
pub use trusted_server_openrtb::{
12-
Banner, Bid, BidResponse, Device, Format, Geo, Imp, Publisher, Regs, SeatBid, Site, User,
11+
Banner, Bid, BidResponse, Device, Format, Geo, Imp, Publisher, Regs, SeatBid, Site, ToExt, User,
1312
};
1413

15-
pub fn object_from_serializable<T: Serialize>(value: &T) -> Object {
16-
match serde_json::to_value(value) {
17-
Ok(Value::Object(map)) => map,
18-
Ok(_) | Err(_) => Map::new(),
19-
}
20-
}
21-
22-
pub fn maybe_object_from_serializable<T: Serialize>(value: &T) -> Option<Object> {
23-
let map = object_from_serializable(value);
24-
if map.is_empty() {
25-
None
26-
} else {
27-
Some(map)
28-
}
29-
}
30-
3114
// ============================================================================
3215
// Extension types (project-specific, not part of the OpenRTB spec)
3316
// ============================================================================
@@ -112,15 +95,16 @@ mod tests {
11295
..Default::default()
11396
};
11497

115-
let ext = maybe_object_from_serializable(&ResponseExt {
98+
let ext = ResponseExt {
11699
orchestrator: OrchestratorExt {
117100
strategy: "parallel_only".to_string(),
118101
providers: 2,
119102
total_bids: 3,
120103
time_ms: 12,
121104
provider_details: vec![],
122105
},
123-
});
106+
}
107+
.to_ext();
124108

125109
let response = OpenRtbResponse {
126110
id: "auction-1".to_string(),

crates/openrtb/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ use serde_json::{Map, Value};
55

66
pub type Object = Map<String, Value>;
77

8+
/// Convert a serializable struct into an `Option<Object>` suitable for an
9+
/// `OpenRTB` `ext` field. Returns `None` when serialization produces an empty
10+
/// map (i.e. all fields were skipped), so that `ext` is omitted from the JSON
11+
/// output rather than emitting `"ext": {}`.
12+
pub trait ToExt {
13+
fn to_ext(&self) -> Option<Object>;
14+
}
15+
16+
impl<T: Serialize> ToExt for T {
17+
fn to_ext(&self) -> Option<Object> {
18+
match serde_json::to_value(self) {
19+
Ok(Value::Object(map)) if !map.is_empty() => Some(map),
20+
_ => None,
21+
}
22+
}
23+
}
24+
825
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
926
pub struct BidRequest {
1027
pub id: String,

0 commit comments

Comments
 (0)