From f783ea616743b49cef6d141ce3ddf7fa63ff9d83 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 28 Apr 2020 00:30:05 +0200 Subject: [PATCH] Fix unsigned field deserialization --- ruma-events-macros/src/gen.rs | 2 +- src/lib.rs | 3 +++ src/room/name.rs | 8 ++++++-- src/room/power_levels.rs | 9 ++++++--- tests/ruma_events_macros.rs | 15 +++++++++++---- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/ruma-events-macros/src/gen.rs b/ruma-events-macros/src/gen.rs index fdd72263..a0ee1d8d 100644 --- a/ruma-events-macros/src/gen.rs +++ b/ruma-events-macros/src/gen.rs @@ -295,7 +295,7 @@ fn populate_room_event_fields(content_name: Ident, fields: Vec) -> Vec, /// The event that redacted this event, if any. + #[serde(skip_serializing_if = "Option::is_none")] redacted_because: Option>, /// The client-supplied transaction ID, if the client being given the event /// is the same one which sent it. + #[serde(skip_serializing_if = "Option::is_none")] transaction_id: Option, } diff --git a/src/room/name.rs b/src/room/name.rs index f65cefc6..b8cfd259 100644 --- a/src/room/name.rs +++ b/src/room/name.rs @@ -158,6 +158,7 @@ mod tests { time::{Duration, UNIX_EPOCH}, }; + use js_int::Int; use ruma_identifiers::{EventId, RoomId, UserId}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; @@ -209,7 +210,10 @@ mod tests { room_id: Some(RoomId::try_from("!n8f893n9:example.com").unwrap()), sender: UserId::try_from("@carl:example.com").unwrap(), state_key: "".to_string(), - unsigned: serde_json::from_str(r#"{"foo": "bar"}"#).unwrap(), + unsigned: UnsignedData { + age: Some(Int::from(100)), + ..UnsignedData::default() + }, }; let actual = to_json_value(&name_event).unwrap(); @@ -225,7 +229,7 @@ mod tests { "state_key": "", "type": "m.room.name", "unsigned": { - "foo": "bar" + "age": 100 } }); diff --git a/src/room/power_levels.rs b/src/room/power_levels.rs index 2fa31321..09c9a3ba 100644 --- a/src/room/power_levels.rs +++ b/src/room/power_levels.rs @@ -275,7 +275,7 @@ mod tests { use super::{ default_power_level, NotificationPowerLevels, PowerLevelsEvent, PowerLevelsEventContent, }; - use crate::{EventType,UnsignedData}; + use crate::{EventType, UnsignedData}; #[test] fn serialization_with_optional_fields_as_none() { @@ -360,7 +360,10 @@ mod tests { }, }), room_id: Some(RoomId::try_from("!n8f893n9:example.com").unwrap()), - unsigned: serde_json::from_str(r#"{"foo": "bar"}"#).unwrap(), + unsigned: UnsignedData { + age: Some(Int::from(100)), + ..UnsignedData::default() + }, sender: user, state_key: "".to_string(), }; @@ -410,7 +413,7 @@ mod tests { "state_key": "", "type": "m.room.power_levels", "unsigned": { - "foo": "bar" + "age": 100 } }); diff --git a/tests/ruma_events_macros.rs b/tests/ruma_events_macros.rs index ca0e7f9d..90bed215 100644 --- a/tests/ruma_events_macros.rs +++ b/tests/ruma_events_macros.rs @@ -4,6 +4,7 @@ use std::{ time::{Duration, UNIX_EPOCH}, }; +use js_int::Int; use ruma_events::util::serde_json_eq_try_from_raw; use ruma_events::UnsignedData; use ruma_events_macros::ruma_event; @@ -100,7 +101,10 @@ mod common_case { room_id: Some(RoomId::try_from("!n8f893n9:example.com").unwrap()), sender: UserId::try_from("@carl:example.com").unwrap(), state_key: "example.com".to_string(), - unsigned: serde_json::from_str(r#"{"foo":"bar"}"#).unwrap(), + unsigned: UnsignedData { + age: Some(Int::from(100)), + ..UnsignedData::default() + }, }; let json = json!({ "content": { @@ -115,7 +119,7 @@ mod common_case { "sender": "@carl:example.com", "state_key": "example.com", "unsigned": { - "foo": "bar" + "age": 100 }, "type": "m.room.aliases" }); @@ -151,7 +155,10 @@ mod extra_fields { origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), room_id: Some(RoomId::try_from("!n8f893n9:example.com").unwrap()), sender: UserId::try_from("@carl:example.com").unwrap(), - unsigned: serde_json::from_str(r#"{"foo":"bar"}"#).unwrap(), + unsigned: UnsignedData { + age: Some(Int::from(100)), + ..UnsignedData::default() + }, }; let json = json!({ "content": { @@ -163,7 +170,7 @@ mod extra_fields { "room_id": "!n8f893n9:example.com", "sender": "@carl:example.com", "unsigned": { - "foo": "bar" + "age": 100 }, "type": "m.room.redaction" });