Skip to content

Commit 3b4f8f7

Browse files
Implement Default for a number of structs in responses
without these defaults, a request that hits a freshly booted RabbitMQ node would panic because one of the metrics/rates fields would be missing, because some statistics tracking events haven't yet happened.
1 parent 87f611d commit 3b4f8f7

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

src/responses.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,7 +1864,8 @@ pub struct MessageRouted {
18641864
#[serde(transparent)]
18651865
pub struct MessageProperties(pub Map<String, serde_json::Value>);
18661866

1867-
#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
1867+
#[derive(Debug, Deserialize, Clone, Eq, PartialEq, Default)]
1868+
#[serde(default)]
18681869
#[cfg_attr(feature = "tabled", derive(Tabled))]
18691870
pub struct ChurnRates {
18701871
pub connection_created: u32,
@@ -1876,13 +1877,15 @@ pub struct ChurnRates {
18761877
pub channel_closed: u32,
18771878
}
18781879

1879-
#[derive(Debug, Deserialize, Clone, PartialEq, PartialOrd)]
1880+
#[derive(Debug, Deserialize, Clone, PartialEq, PartialOrd, Default)]
1881+
#[serde(default)]
18801882
#[cfg_attr(feature = "tabled", derive(Tabled))]
18811883
pub struct Rate {
18821884
pub rate: f64,
18831885
}
18841886

1885-
#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
1887+
#[derive(Debug, Deserialize, Clone, Eq, PartialEq, Default)]
1888+
#[serde(default)]
18861889
#[cfg_attr(feature = "tabled", derive(Tabled))]
18871890
pub struct ObjectTotals {
18881891
pub connections: u64,
@@ -1892,50 +1895,51 @@ pub struct ObjectTotals {
18921895
pub consumers: u64,
18931896
}
18941897

1895-
#[derive(Debug, Deserialize, Clone, PartialEq)]
1898+
#[derive(Debug, Deserialize, Clone, PartialEq, Default)]
1899+
#[serde(default)]
18961900
#[cfg_attr(feature = "tabled", derive(Tabled))]
18971901
pub struct QueueTotals {
18981902
pub messages: u64,
1899-
#[serde(rename = "messages_ready")]
1903+
#[serde(rename = "messages_ready", default)]
19001904
pub messages_ready_for_delivery: u64,
1901-
#[serde(rename = "messages_unacknowledged")]
1905+
#[serde(rename = "messages_unacknowledged", default)]
19021906
pub messages_delivered_but_unacknowledged_by_consumers: u64,
19031907
pub messages_details: Rate,
1904-
#[serde(rename = "messages_ready_details")]
1908+
#[serde(rename = "messages_ready_details", default)]
19051909
pub messages_ready_for_delivery_details: Rate,
1906-
#[serde(rename = "messages_unacknowledged_details")]
1910+
#[serde(rename = "messages_unacknowledged_details", default)]
19071911
pub messages_delivered_but_unacknowledged_by_consumers_details: Rate,
19081912
}
19091913

1910-
#[derive(Debug, Deserialize, Clone, PartialEq)]
1914+
#[derive(Debug, Deserialize, Clone, PartialEq, Default)]
19111915
#[cfg_attr(feature = "tabled", derive(Tabled))]
19121916
pub struct MessageStats {
19131917
/// Consumer delivery rate plus polling (via 'basic.get') rate
1914-
#[serde(rename = "deliver_get_details")]
1918+
#[serde(rename = "deliver_get_details", default)]
19151919
#[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
19161920
pub delivery_details: Option<Rate>,
1917-
#[serde(rename = "publish_details")]
1921+
#[serde(rename = "publish_details", default)]
19181922
#[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
19191923
pub publishing_details: Option<Rate>,
19201924

1921-
#[serde(rename = "deliver_no_ack_details")]
1925+
#[serde(rename = "deliver_no_ack_details", default)]
19221926
#[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
19231927
pub delivery_with_automatic_acknowledgement_details: Option<Rate>,
1924-
#[serde(rename = "redeliver_details")]
1928+
#[serde(rename = "redeliver_details", default)]
19251929
#[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
19261930
pub redelivery_details: Option<Rate>,
19271931

1928-
#[serde(rename = "confirm_details")]
1932+
#[serde(rename = "confirm_details", default)]
19291933
#[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
19301934
pub publisher_confirmation_details: Option<Rate>,
1931-
#[serde(rename = "ack_details")]
1935+
#[serde(rename = "ack_details", default)]
19321936
#[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
19331937
pub consumer_acknowledgement_details: Option<Rate>,
19341938

1935-
#[serde(rename = "drop_unroutable_details")]
1939+
#[serde(rename = "drop_unroutable_details", default)]
19361940
#[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
19371941
pub unroutable_dropped_message_details: Option<Rate>,
1938-
#[serde(rename = "return_unroutable_details")]
1942+
#[serde(rename = "return_unroutable_details", default)]
19391943
#[cfg_attr(feature = "tabled", tabled(display = "display_option"))]
19401944
pub unroutable_returned_message_details: Option<Rate>,
19411945
}
@@ -1954,7 +1958,8 @@ pub struct Listener {
19541958
#[serde(transparent)]
19551959
pub struct TagMap(pub Map<String, serde_json::Value>);
19561960

1957-
#[derive(Debug, Deserialize, Clone, PartialEq)]
1961+
#[derive(Debug, Deserialize, Clone, PartialEq, Default)]
1962+
#[serde(default)]
19581963
#[cfg_attr(feature = "tabled", derive(Tabled))]
19591964
pub struct Overview {
19601965
pub cluster_name: String,

tests/unit_default_tests.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (C) 2023-2025 RabbitMQ Core Team ([email protected])
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
mod test_helpers;
15+
16+
use rabbitmq_http_client::responses::Rate;
17+
18+
#[test]
19+
fn test_unit_default_rate() {
20+
let def = Rate::default();
21+
22+
// Specifically for this test, we don't really need to use `approx`
23+
assert_eq!(def.rate, 0.0);
24+
}

0 commit comments

Comments
 (0)