Skip to content

Commit

Permalink
Fix guild deserialisation regression (#640)
Browse files Browse the repository at this point in the history
A regression in 'model::guild::Guild' deserialization was introduced by
commit 4541935[1].

The bug introduced was that deserialisation of
'Guild::premium_subscription_count'[2] would fail. The
'premium_subscription_count' field isn't always present in the JSON
provided as noted by the REST API documentation[3], so a default value
needs to be set for it, in this case 0.

Additionally, set a default value for 'Guild::premium_tier'[4] in case this
is ever nulled in the API too, defaulting to 'PremiumTier::Tier0'.

[1]: 4541935
[2]: 4541935#diff-e1c86b244465a2bb338d53758aa2c7efR154
[3]: discord/discord-api-docs@3d8cca0#diff-c4e1390fca89ecc96b01a093f988fa1cR48
[4]: 4541935#diff-e1c86b244465a2bb338d53758aa2c7efR156

Signed-off-by: Zeyla Hellyer <zeyla@hellyer.dev>
  • Loading branch information
zeylahellyer authored and Lakelezz committed Jun 30, 2019
1 parent 13595ff commit e628614
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/model/guild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,10 @@ pub struct Guild {
/// The server's description
pub description: Option<String>,
/// The server's premium boosting level
#[serde(default)]
pub premium_tier: PremiumTier,
/// The total number of users currently boosting this server
#[serde(default)]
pub premium_subscription_count: u64,
/// The server's banner
pub banner: Option<String>,
Expand Down Expand Up @@ -1763,14 +1765,14 @@ impl<'de> Deserialize<'de> for Guild {
Some(v) => Option::<String>::deserialize(v).map_err(DeError::custom)?,
None => None,
};
let premium_tier = map.remove("premium_tier")
.ok_or_else(|| DeError::custom("expected guild premium_tier"))
.and_then(PremiumTier::deserialize)
.map_err(DeError::custom)?;
let premium_subscription_count = map.remove("premium_subscription_count")
.ok_or_else(|| DeError::custom("expected guild premium_subscription_count"))
.and_then(u64::deserialize)
.map_err(DeError::custom)?;
let premium_tier = match map.remove("premium_tier") {
Some(v) => PremiumTier::deserialize(v).map_err(DeError::custom)?,
None => PremiumTier::default(),
};
let premium_subscription_count = match map.remove("premium_subscription_count") {
Some(Value::Null) | None => 0,
Some(v) => u64::deserialize(v).map_err(DeError::custom)?,
};
let banner = match map.remove("banner") {
Some(v) => Option::<String>::deserialize(v).map_err(DeError::custom)?,
None => None,
Expand All @@ -1780,7 +1782,6 @@ impl<'de> Deserialize<'de> for Guild {
None => None,
};


Ok(Self {
afk_channel_id,
application_id,
Expand Down
6 changes: 6 additions & 0 deletions src/model/guild/premium_tier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ impl PremiumTier {
}
}
}

impl Default for PremiumTier {
fn default() -> Self {
PremiumTier::Tier0
}
}

0 comments on commit e628614

Please sign in to comment.