Properly serialize get_tags Response

`get_tags` route response was adding the `tags` property twice, which is
not the expected behaviour. This is an example of the previous
serialization of the `get_tags` route:

`{"tags":{"tags":{"u.example":{"order":0.55},"u.another":{"order":0.11}}}}`

The new serialization removes the outer `tags` property.
This commit is contained in:
gnieto 2020-07-05 13:20:47 +02:00 committed by GitHub
parent 9ecc7c11cf
commit 6fd4b9b8b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 3 deletions

View File

@ -4,6 +4,7 @@ Bug fixes:
* Fix deserialization of `r0::room::get_room_event::Response` * Fix deserialization of `r0::room::get_room_event::Response`
* More missing fields in `r0::sync::sync_events::Response` can be deserialized * More missing fields in `r0::sync::sync_events::Response` can be deserialized
* Fix `get_tags::Response` serialization
Breaking changes: Breaking changes:

View File

@ -1,7 +1,7 @@
//! [GET /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags](https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-user-userid-rooms-roomid-tags) //! [GET /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags](https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-user-userid-rooms-roomid-tags)
use ruma_api::ruma_api; use ruma_api::ruma_api;
use ruma_events::{tag::TagEventContent, EventJson}; use ruma_events::tag::Tags;
use ruma_identifiers::{RoomId, UserId}; use ruma_identifiers::{RoomId, UserId};
ruma_api! { ruma_api! {
@ -26,8 +26,43 @@ ruma_api! {
response: { response: {
/// The user's tags for the room. /// The user's tags for the room.
pub tags: EventJson<TagEventContent>, pub tags: Tags,
} }
error: crate::Error error: crate::Error
} }
#[cfg(test)]
mod tests {
use serde_json::json;
use super::Response;
use ruma_events::tag::{TagInfo, Tags};
use std::convert::TryFrom;
#[test]
fn test_serializing_get_tags_response() {
let mut tags = Tags::new();
tags.insert("m.favourite".to_string(), TagInfo { order: Some(0.25) });
tags.insert("u.user_tag".to_string(), TagInfo { order: Some(0.11) });
let response = Response { tags };
let http_response = http::Response::<Vec<u8>>::try_from(response).unwrap();
let json_response: serde_json::Value =
serde_json::from_slice(http_response.body()).unwrap();
assert_eq!(
json_response,
json!({
"tags": {
"m.favourite": {
"order": 0.25,
},
"u.user_tag": {
"order": 0.11,
}
}
})
);
}
}

View File

@ -9,13 +9,15 @@ use crate::BasicEvent;
/// Informs the client of tags on a room. /// Informs the client of tags on a room.
pub type TagEvent = BasicEvent<TagEventContent>; pub type TagEvent = BasicEvent<TagEventContent>;
/// Map of tag names to tag info.
pub type Tags = BTreeMap<String, TagInfo>;
/// The payload for `TagEvent`. /// The payload for `TagEvent`.
#[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)] #[derive(Clone, Debug, Deserialize, Serialize, BasicEventContent)]
#[ruma_event(type = "m.tag")] #[ruma_event(type = "m.tag")]
pub struct TagEventContent { pub struct TagEventContent {
/// A map of tag names to tag info. /// A map of tag names to tag info.
pub tags: BTreeMap<String, TagInfo>, pub tags: Tags,
} }
/// Information about a tag. /// Information about a tag.