diff --git a/ruma-client-api/Cargo.toml b/ruma-client-api/Cargo.toml index a8e98b69..e08c005c 100644 --- a/ruma-client-api/Cargo.toml +++ b/ruma-client-api/Cargo.toml @@ -34,6 +34,7 @@ maplit = "1.0.2" matches = "0.1.8" [features] +compat = [] unstable-exhaustive-types = [] unstable-pre-spec = [] unstable-synapse-quirks = [] diff --git a/ruma-client-api/src/r0/profile/set_avatar_url.rs b/ruma-client-api/src/r0/profile/set_avatar_url.rs index 9fd7dcfa..769f67ec 100644 --- a/ruma-client-api/src/r0/profile/set_avatar_url.rs +++ b/ruma-client-api/src/r0/profile/set_avatar_url.rs @@ -22,6 +22,10 @@ ruma_api! { /// /// `None` is used to unset the avatar. #[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>, } @@ -44,3 +48,37 @@ impl Response { Self } } + +#[cfg(test)] +mod tests { + use matches::assert_matches; + use ruma_api::IncomingRequest as _; + + use super::IncomingRequest; + + #[test] + fn deserialize_unset_request() -> Result<(), Box> { + 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::::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(()) + } +} diff --git a/ruma/Cargo.toml b/ruma/Cargo.toml index 594d3dc3..7fd40d1f 100644 --- a/ruma/Cargo.toml +++ b/ruma/Cargo.toml @@ -57,7 +57,7 @@ markdown = ["ruma-events/markdown"] # # For example, some mandatory string fields are defaulted to an empty string if # 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 # otherwise provides!