Skip to content

Commit

Permalink
Fix serde Stream roundtrip
Browse files Browse the repository at this point in the history
If someone does a serde serialize/deserialize roundtrip with
jetstream::stream::Stream, it will result in error, as then
`ConsumerLimits` will be `null`.
This should not happen in real-world scenarios, as server does not send
nulls for those values, however its good to protect against those
scenarios in case Go/nats-server changes its behaviour as some point in time.

Signed-off-by: Tomasz Pietrek <tomasz@nats.io>
  • Loading branch information
Jarema authored Aug 5, 2024
1 parent 966df7c commit e9919a0
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions async-nats/src/jetstream/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1120,14 +1120,17 @@ fn default_consumer_limits_as_none<'de, D>(
where
D: Deserializer<'de>,
{
let consumer_limits = ConsumerLimits::deserialize(deserializer)?;
if consumer_limits == ConsumerLimits::default() {
Ok(None)
let consumer_limits = Option::<ConsumerLimits>::deserialize(deserializer)?;
if let Some(cl) = consumer_limits {
if cl == ConsumerLimits::default() {
Ok(None)
} else {
Ok(Some(cl))
}
} else {
Ok(Some(consumer_limits))
Ok(None)
}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)]
pub struct ConsumerLimits {
/// Sets the maximum [crate::jetstream::consumer::Config::inactive_threshold] that can be set on the consumer.
Expand Down Expand Up @@ -2062,3 +2065,21 @@ impl From<super::context::RequestError> for ConsumerCreateStrictError {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn consumer_limits_de() {
let config = Config {
..Default::default()
};

let roundtrip: Config = {
let ser = serde_json::to_string(&config).unwrap();
serde_json::from_str(&ser).unwrap()
};
assert_eq!(config, roundtrip);
}
}

0 comments on commit e9919a0

Please sign in to comment.