client-api: Deserialize empty avatar_url to None in set_avatar_url (feature = "compat")

This commit is contained in:
Jonas Platte 2021-02-18 13:28:21 +01:00
parent 6dd738c315
commit f5e67d0f63
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
3 changed files with 40 additions and 1 deletions

View File

@ -34,6 +34,7 @@ maplit = "1.0.2"
matches = "0.1.8" matches = "0.1.8"
[features] [features]
compat = []
unstable-exhaustive-types = [] unstable-exhaustive-types = []
unstable-pre-spec = [] unstable-pre-spec = []
unstable-synapse-quirks = [] unstable-synapse-quirks = []

View File

@ -22,6 +22,10 @@ ruma_api! {
/// ///
/// `None` is used to unset the avatar. /// `None` is used to unset the avatar.
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(
feature = "compat",
serde(default, deserialize_with = "ruma_serde::empty_string_as_none")
)]
pub avatar_url: Option<&'a str>, pub avatar_url: Option<&'a str>,
} }
@ -44,3 +48,37 @@ impl Response {
Self Self
} }
} }
#[cfg(test)]
mod tests {
use matches::assert_matches;
use ruma_api::IncomingRequest as _;
use super::IncomingRequest;
#[test]
fn deserialize_unset_request() -> Result<(), Box<dyn std::error::Error>> {
assert_matches!(
IncomingRequest::try_from_http_request(
http::Request::builder()
.method("PUT")
.uri("https://bar.org/_matrix/client/r0/profile/@foo:bar.org/avatar_url")
.body(Vec::<u8>::new())?,
)?,
IncomingRequest { user_id, avatar_url: None } if user_id == "@foo:bar.org"
);
#[cfg(feature = "compat")]
assert_matches!(
IncomingRequest::try_from_http_request(
http::Request::builder()
.method("PUT")
.uri("https://bar.org/_matrix/client/r0/profile/@foo:bar.org/avatar_url")
.body(serde_json::to_vec(&serde_json::json!({ "avatar_url": "" }))?)?,
)?,
IncomingRequest { user_id, avatar_url: None } if user_id == "@foo:bar.org"
);
Ok(())
}
}

View File

@ -57,7 +57,7 @@ markdown = ["ruma-events/markdown"]
# #
# For example, some mandatory string fields are defaulted to an empty string if # For example, some mandatory string fields are defaulted to an empty string if
# missing with this feature. # missing with this feature.
compat = ["ruma-common/compat", "ruma-events/compat"] compat = ["ruma-common/compat", "ruma-events/compat", "ruma-client-api/compat"]
# unstable: by using any of these, you opt out of all semver guarantees Ruma # unstable: by using any of these, you opt out of all semver guarantees Ruma
# otherwise provides! # otherwise provides!