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"
version-sync = "0.9.2"
serde_yaml = "0.8"
rmp-serde = "1"
# runtime dev-deps
actix-rt = { version = "^2" }

View File

@ -199,6 +199,13 @@ mod tests {
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]
fn message_v10_roundtrip_structured() -> Result<()> {
assert_eq!(
@ -231,4 +238,11 @@ mod tests {
);
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>,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> {
let num =
3 + if attributes.datacontenttype.is_some() {
1
} else {
0
} + if attributes.schemaurl.is_some() { 1 } else { 0 }
+ if attributes.subject.is_some() { 1 } else { 0 }
+ if attributes.time.is_some() { 1 } else { 0 }
+ if data.is_some() { 1 } else { 0 }
let num = 4
+ [
attributes.datacontenttype.is_some(),
attributes.schemaurl.is_some(),
attributes.subject.is_some(),
attributes.time.is_some(),
data.is_some(),
]
.iter()
.filter(|&b| *b)
.count()
+ extensions.len();
let mut state = serializer.serialize_map(Some(num))?;
state.serialize_entry("specversion", "0.3")?;
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>,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> {
let num =
3 + if attributes.datacontenttype.is_some() {
1
} else {
0
} + if attributes.dataschema.is_some() {
1
} else {
0
} + if attributes.subject.is_some() { 1 } else { 0 }
+ if attributes.time.is_some() { 1 } else { 0 }
+ if data.is_some() { 1 } else { 0 }
let num = 4
+ [
attributes.datacontenttype.is_some(),
attributes.dataschema.is_some(),
attributes.subject.is_some(),
attributes.time.is_some(),
data.is_some(),
]
.iter()
.filter(|&b| *b)
.count()
+ extensions.len();
let mut state = serializer.serialize_map(Some(num))?;
state.serialize_entry("specversion", "1.0")?;
state.serialize_entry("id", &attributes.id)?;