Skip to content

Commit

Permalink
Fix deserialization of GuildMembersChunkEvent (serenity-rs#2672)
Browse files Browse the repository at this point in the history
Specifically, the `members` field was being incorrectly deserialized. In
serenity-rs#2287, the logic for collecting the received array of members into a
hashmap was removed, meaning the deserializer now looked for a map
instead of a sequence, causing deserialization to fail. This PR adds
that logic back.

Closes serenity-rs#2666
  • Loading branch information
mkrasnitski committed Dec 24, 2023
1 parent a13625a commit e80999f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/model/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ use serde::Serialize;

use super::application::ActionRow;
use super::prelude::*;
use super::utils::{deserialize_val, emojis, remove_from_map, remove_from_map_opt, stickers};
use super::utils::{
deserialize_val,
emojis,
members,
remove_from_map,
remove_from_map_opt,
stickers,
};
use crate::constants::Opcode;
use crate::model::application::{CommandPermissions, Interaction};
use crate::model::guild::audit_log::AuditLogEntry;
Expand Down Expand Up @@ -272,6 +279,7 @@ pub struct GuildMembersChunkEvent {
/// ID of the guild.
pub guild_id: GuildId,
/// Set of guild members.
#[serde(with = "members")]
pub members: HashMap<UserId, Member>,
/// Chunk index in the expected chunks for this response (0 <= chunk_index < chunk_count).
pub chunk_index: u32,
Expand Down
3 changes: 1 addition & 2 deletions src/model/guild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ pub struct Guild {
///
/// Members might not all be available when the [`ReadyEvent`] is received if the
/// [`Self::member_count`] is greater than the [`LARGE_THRESHOLD`] set by the library.
#[serde(serialize_with = "serialize_map_values")]
#[serde(deserialize_with = "deserialize_members")]
#[serde(with = "members")]
pub members: HashMap<UserId, Member>,
/// All voice and text channels contained within a guild.
///
Expand Down
21 changes: 17 additions & 4 deletions src/model/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,23 @@ pub fn deserialize_guild_channels<'de, D: Deserializer<'de>>(
Ok(map)
}

pub fn deserialize_members<'de, D: Deserializer<'de>>(
deserializer: D,
) -> StdResult<HashMap<UserId, Member>, D::Error> {
deserializer.deserialize_seq(SequenceToMapVisitor::new(|member: &Member| member.user.id))
/// Used with `#[serde(with = "members")]
pub mod members {
use std::collections::HashMap;

use serde::Deserializer;

use super::SequenceToMapVisitor;
use crate::model::guild::Member;
use crate::model::id::UserId;

pub fn deserialize<'de, D: Deserializer<'de>>(
deserializer: D,
) -> Result<HashMap<UserId, Member>, D::Error> {
deserializer.deserialize_seq(SequenceToMapVisitor::new(|member: &Member| member.user.id))
}

pub use super::serialize_map_values as serialize;
}

/// Used with `#[serde(with = "presences")]`
Expand Down

0 comments on commit e80999f

Please sign in to comment.