Fix unsigned field deserialization

This commit is contained in:
Jonas Platte 2020-04-28 00:30:05 +02:00
parent ed9888181c
commit f783ea6167
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
5 changed files with 27 additions and 10 deletions

View File

@ -295,7 +295,7 @@ fn populate_room_event_fields(content_name: Ident, fields: Vec<Field>) -> Vec<Fi
pub sender: ruma_identifiers::UserId, pub sender: ruma_identifiers::UserId,
/// Additional key-value pairs not signed by the homeserver. /// Additional key-value pairs not signed by the homeserver.
#[serde(skip_serializing_if = "ruma_events::UnsignedData::is_empty")] #[serde(default, skip_serializing_if = "ruma_events::UnsignedData::is_empty")]
pub unsigned: ruma_events::UnsignedData, pub unsigned: ruma_events::UnsignedData,
}; };

View File

@ -231,11 +231,14 @@ pub struct UnsignedData {
/// field is generated by the local homeserver, and may be incorrect if the /// field is generated by the local homeserver, and may be incorrect if the
/// local time on at least one of the two servers is out of sync, which can /// local time on at least one of the two servers is out of sync, which can
/// cause the age to either be negative or greater than it actually is. /// cause the age to either be negative or greater than it actually is.
#[serde(skip_serializing_if = "Option::is_none")]
age: Option<Int>, age: Option<Int>,
/// The event that redacted this event, if any. /// The event that redacted this event, if any.
#[serde(skip_serializing_if = "Option::is_none")]
redacted_because: Option<EventJson<RedactionEvent>>, redacted_because: Option<EventJson<RedactionEvent>>,
/// The client-supplied transaction ID, if the client being given the event /// The client-supplied transaction ID, if the client being given the event
/// is the same one which sent it. /// is the same one which sent it.
#[serde(skip_serializing_if = "Option::is_none")]
transaction_id: Option<String>, transaction_id: Option<String>,
} }

View File

@ -158,6 +158,7 @@ mod tests {
time::{Duration, UNIX_EPOCH}, time::{Duration, UNIX_EPOCH},
}; };
use js_int::Int;
use ruma_identifiers::{EventId, RoomId, UserId}; use ruma_identifiers::{EventId, RoomId, UserId};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; 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()), room_id: Some(RoomId::try_from("!n8f893n9:example.com").unwrap()),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: UserId::try_from("@carl:example.com").unwrap(),
state_key: "".to_string(), 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(); let actual = to_json_value(&name_event).unwrap();
@ -225,7 +229,7 @@ mod tests {
"state_key": "", "state_key": "",
"type": "m.room.name", "type": "m.room.name",
"unsigned": { "unsigned": {
"foo": "bar" "age": 100
} }
}); });

View File

@ -275,7 +275,7 @@ mod tests {
use super::{ use super::{
default_power_level, NotificationPowerLevels, PowerLevelsEvent, PowerLevelsEventContent, default_power_level, NotificationPowerLevels, PowerLevelsEvent, PowerLevelsEventContent,
}; };
use crate::{EventType,UnsignedData}; use crate::{EventType, UnsignedData};
#[test] #[test]
fn serialization_with_optional_fields_as_none() { fn serialization_with_optional_fields_as_none() {
@ -360,7 +360,10 @@ mod tests {
}, },
}), }),
room_id: Some(RoomId::try_from("!n8f893n9:example.com").unwrap()), 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, sender: user,
state_key: "".to_string(), state_key: "".to_string(),
}; };
@ -410,7 +413,7 @@ mod tests {
"state_key": "", "state_key": "",
"type": "m.room.power_levels", "type": "m.room.power_levels",
"unsigned": { "unsigned": {
"foo": "bar" "age": 100
} }
}); });

View File

@ -4,6 +4,7 @@ use std::{
time::{Duration, UNIX_EPOCH}, time::{Duration, UNIX_EPOCH},
}; };
use js_int::Int;
use ruma_events::util::serde_json_eq_try_from_raw; use ruma_events::util::serde_json_eq_try_from_raw;
use ruma_events::UnsignedData; use ruma_events::UnsignedData;
use ruma_events_macros::ruma_event; use ruma_events_macros::ruma_event;
@ -100,7 +101,10 @@ mod common_case {
room_id: Some(RoomId::try_from("!n8f893n9:example.com").unwrap()), room_id: Some(RoomId::try_from("!n8f893n9:example.com").unwrap()),
sender: UserId::try_from("@carl:example.com").unwrap(), sender: UserId::try_from("@carl:example.com").unwrap(),
state_key: "example.com".to_string(), 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!({ let json = json!({
"content": { "content": {
@ -115,7 +119,7 @@ mod common_case {
"sender": "@carl:example.com", "sender": "@carl:example.com",
"state_key": "example.com", "state_key": "example.com",
"unsigned": { "unsigned": {
"foo": "bar" "age": 100
}, },
"type": "m.room.aliases" "type": "m.room.aliases"
}); });
@ -151,7 +155,10 @@ mod extra_fields {
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
room_id: Some(RoomId::try_from("!n8f893n9:example.com").unwrap()), room_id: Some(RoomId::try_from("!n8f893n9:example.com").unwrap()),
sender: UserId::try_from("@carl: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!({ let json = json!({
"content": { "content": {
@ -163,7 +170,7 @@ mod extra_fields {
"room_id": "!n8f893n9:example.com", "room_id": "!n8f893n9:example.com",
"sender": "@carl:example.com", "sender": "@carl:example.com",
"unsigned": { "unsigned": {
"foo": "bar" "age": 100
}, },
"type": "m.room.redaction" "type": "m.room.redaction"
}); });