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,
/// 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,
};

View File

@ -231,11 +231,14 @@ pub struct UnsignedData {
/// 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
/// cause the age to either be negative or greater than it actually is.
#[serde(skip_serializing_if = "Option::is_none")]
age: Option<Int>,
/// The event that redacted this event, if any.
#[serde(skip_serializing_if = "Option::is_none")]
redacted_because: Option<EventJson<RedactionEvent>>,
/// 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<String>,
}

View File

@ -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
}
});

View File

@ -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
}
});

View File

@ -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"
});