Skip to content

Commit c380078

Browse files
authored
Fix off-by-one bug in Event serializer (#191)
Fixes #189 Added breaking tests for both V03 and V10 to confirm bug and changed the logic slightly to clarify intent Signed-off-by: Jim Crossley <[email protected]>
1 parent b8487af commit c380078

File tree

4 files changed

+41
-23
lines changed

4 files changed

+41
-23
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ rstest = "0.6"
6969
claim = "0.3.1"
7070
version-sync = "0.9.2"
7171
serde_yaml = "0.8"
72+
rmp-serde = "1"
7273

7374
# runtime dev-deps
7475
actix-rt = { version = "^2" }

src/event/message.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ mod tests {
199199
Ok(())
200200
}
201201

202+
#[test]
203+
fn message_v03_msgpack() {
204+
let buff = rmp_serde::to_vec(&fixtures::v03::full_json_data()).unwrap();
205+
let event = rmp_serde::from_slice::<Event>(buff.as_slice()).unwrap();
206+
assert_eq!(event, fixtures::v03::full_json_data(),);
207+
}
208+
202209
#[test]
203210
fn message_v10_roundtrip_structured() -> Result<()> {
204211
assert_eq!(
@@ -231,4 +238,11 @@ mod tests {
231238
);
232239
Ok(())
233240
}
241+
242+
#[test]
243+
fn message_v10_msgpack() {
244+
let buff = rmp_serde::to_vec(&fixtures::v10::full_json_data()).unwrap();
245+
let event = rmp_serde::from_slice::<Event>(buff.as_slice()).unwrap();
246+
assert_eq!(event, fixtures::v10::full_json_data(),);
247+
}
234248
}

src/event/v03/format.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,19 @@ impl<S: serde::Serializer> crate::event::format::EventFormatSerializer<S, Attrib
6868
extensions: &HashMap<String, ExtensionValue>,
6969
serializer: S,
7070
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> {
71-
let num =
72-
3 + if attributes.datacontenttype.is_some() {
73-
1
74-
} else {
75-
0
76-
} + if attributes.schemaurl.is_some() { 1 } else { 0 }
77-
+ if attributes.subject.is_some() { 1 } else { 0 }
78-
+ if attributes.time.is_some() { 1 } else { 0 }
79-
+ if data.is_some() { 1 } else { 0 }
80-
+ extensions.len();
71+
let num = 4
72+
+ [
73+
attributes.datacontenttype.is_some(),
74+
attributes.schemaurl.is_some(),
75+
attributes.subject.is_some(),
76+
attributes.time.is_some(),
77+
data.is_some(),
78+
]
79+
.iter()
80+
.filter(|&b| *b)
81+
.count()
82+
+ extensions.len();
83+
8184
let mut state = serializer.serialize_map(Some(num))?;
8285
state.serialize_entry("specversion", "0.3")?;
8386
state.serialize_entry("id", &attributes.id)?;

src/event/v10/format.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ impl<S: serde::Serializer> crate::event::format::EventFormatSerializer<S, Attrib
6969
extensions: &HashMap<String, ExtensionValue>,
7070
serializer: S,
7171
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> {
72-
let num =
73-
3 + if attributes.datacontenttype.is_some() {
74-
1
75-
} else {
76-
0
77-
} + if attributes.dataschema.is_some() {
78-
1
79-
} else {
80-
0
81-
} + if attributes.subject.is_some() { 1 } else { 0 }
82-
+ if attributes.time.is_some() { 1 } else { 0 }
83-
+ if data.is_some() { 1 } else { 0 }
84-
+ extensions.len();
72+
let num = 4
73+
+ [
74+
attributes.datacontenttype.is_some(),
75+
attributes.dataschema.is_some(),
76+
attributes.subject.is_some(),
77+
attributes.time.is_some(),
78+
data.is_some(),
79+
]
80+
.iter()
81+
.filter(|&b| *b)
82+
.count()
83+
+ extensions.len();
84+
8585
let mut state = serializer.serialize_map(Some(num))?;
8686
state.serialize_entry("specversion", "1.0")?;
8787
state.serialize_entry("id", &attributes.id)?;

0 commit comments

Comments
 (0)