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 <jim@crossleys.org>
This commit is contained in:
Jim Crossley 2022-09-26 12:14:57 -04:00 committed by GitHub
parent b8487af97c
commit c380078bf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 23 deletions

View File

@ -69,6 +69,7 @@ rstest = "0.6"
claim = "0.3.1" claim = "0.3.1"
version-sync = "0.9.2" version-sync = "0.9.2"
serde_yaml = "0.8" serde_yaml = "0.8"
rmp-serde = "1"
# runtime dev-deps # runtime dev-deps
actix-rt = { version = "^2" } actix-rt = { version = "^2" }

View File

@ -199,6 +199,13 @@ mod tests {
Ok(()) Ok(())
} }
#[test]
fn message_v03_msgpack() {
let buff = rmp_serde::to_vec(&fixtures::v03::full_json_data()).unwrap();
let event = rmp_serde::from_slice::<Event>(buff.as_slice()).unwrap();
assert_eq!(event, fixtures::v03::full_json_data(),);
}
#[test] #[test]
fn message_v10_roundtrip_structured() -> Result<()> { fn message_v10_roundtrip_structured() -> Result<()> {
assert_eq!( assert_eq!(
@ -231,4 +238,11 @@ mod tests {
); );
Ok(()) Ok(())
} }
#[test]
fn message_v10_msgpack() {
let buff = rmp_serde::to_vec(&fixtures::v10::full_json_data()).unwrap();
let event = rmp_serde::from_slice::<Event>(buff.as_slice()).unwrap();
assert_eq!(event, fixtures::v10::full_json_data(),);
}
} }

View File

@ -68,16 +68,19 @@ impl<S: serde::Serializer> crate::event::format::EventFormatSerializer<S, Attrib
extensions: &HashMap<String, ExtensionValue>, extensions: &HashMap<String, ExtensionValue>,
serializer: S, serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> { ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> {
let num = let num = 4
3 + if attributes.datacontenttype.is_some() { + [
1 attributes.datacontenttype.is_some(),
} else { attributes.schemaurl.is_some(),
0 attributes.subject.is_some(),
} + if attributes.schemaurl.is_some() { 1 } else { 0 } attributes.time.is_some(),
+ if attributes.subject.is_some() { 1 } else { 0 } data.is_some(),
+ if attributes.time.is_some() { 1 } else { 0 } ]
+ if data.is_some() { 1 } else { 0 } .iter()
+ extensions.len(); .filter(|&b| *b)
.count()
+ extensions.len();
let mut state = serializer.serialize_map(Some(num))?; let mut state = serializer.serialize_map(Some(num))?;
state.serialize_entry("specversion", "0.3")?; state.serialize_entry("specversion", "0.3")?;
state.serialize_entry("id", &attributes.id)?; state.serialize_entry("id", &attributes.id)?;

View File

@ -69,19 +69,19 @@ impl<S: serde::Serializer> crate::event::format::EventFormatSerializer<S, Attrib
extensions: &HashMap<String, ExtensionValue>, extensions: &HashMap<String, ExtensionValue>,
serializer: S, serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> { ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> {
let num = let num = 4
3 + if attributes.datacontenttype.is_some() { + [
1 attributes.datacontenttype.is_some(),
} else { attributes.dataschema.is_some(),
0 attributes.subject.is_some(),
} + if attributes.dataschema.is_some() { attributes.time.is_some(),
1 data.is_some(),
} else { ]
0 .iter()
} + if attributes.subject.is_some() { 1 } else { 0 } .filter(|&b| *b)
+ if attributes.time.is_some() { 1 } else { 0 } .count()
+ if data.is_some() { 1 } else { 0 } + extensions.len();
+ extensions.len();
let mut state = serializer.serialize_map(Some(num))?; let mut state = serializer.serialize_map(Some(num))?;
state.serialize_entry("specversion", "1.0")?; state.serialize_entry("specversion", "1.0")?;
state.serialize_entry("id", &attributes.id)?; state.serialize_entry("id", &attributes.id)?;