Skip to content

Commit 05807fd

Browse files
committed
Don't fail to deserialize data_base64 even if datacontenttype lies
This fixes #160 From the spec: "When a CloudEvent is deserialized from JSON, the presence of the data_base64 member clearly indicates that the value is a Base64 encoded binary data, which the deserializer MUST decode into a binary runtime data type. The deserializer MAY further interpret this binary data according to the datacontenttype." https://github.com/cloudevents/spec/blob/master/cloudevents/formats/json-format.md#312-payload-deserialization Signed-off-by: Jim Crossley <[email protected]>
1 parent 1e89203 commit 05807fd

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/event/builder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ mod tests {
200200
fixtures::v10::full_json_base64_data_json(),
201201
fixtures::v10::full_json_data()
202202
),
203+
case::full_v10_with_non_json_base64_data(
204+
fixtures::v10::full_non_json_base64_data(),
205+
fixtures::v10::full_non_json_data()
206+
),
203207
case::full_v10_with_xml_string_data(
204208
fixtures::v10::full_xml_string_data_json(),
205209
fixtures::v10::full_xml_string_data()

src/event/v10/format.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ impl crate::event::format::EventFormatDeserializer for EventFormatDeserializer {
4242
Ok(match (data, data_base64, is_json) {
4343
(Some(d), None, true) => Some(Data::Json(parse_data_json!(d, E)?)),
4444
(Some(d), None, false) => Some(Data::String(parse_data_string!(d, E)?)),
45-
(None, Some(d), true) => Some(Data::Json(parse_json_data_base64!(d, E)?)),
45+
(None, Some(d), true) => {
46+
let dc = d.to_owned();
47+
match parse_json_data_base64!(dc, E) {
48+
Ok(x) => Some(Data::Json(x)),
49+
Err(_) => Some(Data::Binary(parse_data_base64!(d, E)?)),
50+
}
51+
}
4652
(None, Some(d), false) => Some(Data::Binary(parse_data_base64!(d, E)?)),
4753
(Some(_), Some(_), _) => {
4854
return Err(E::custom("Cannot have both data and data_base64 field"))

src/test/fixtures/v10.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,28 @@ pub fn full_json_base64_data_json() -> Value {
170170
})
171171
}
172172

173+
pub fn full_non_json_base64_data() -> Value {
174+
match full_json_base64_data_json() {
175+
Value::Object(mut m) => {
176+
m.insert(
177+
"data_base64".to_string(),
178+
Value::String(base64::encode(b"hello world")),
179+
);
180+
Value::Object(m)
181+
}
182+
_ => Value::Null,
183+
}
184+
}
185+
186+
pub fn full_non_json_data() -> Event {
187+
let mut event = full_json_data();
188+
let value = full_non_json_base64_data();
189+
if let Value::Object(m) = value {
190+
event.set_data_unchecked(base64::decode(m["data_base64"].as_str().unwrap()).unwrap());
191+
}
192+
event
193+
}
194+
173195
pub fn full_xml_string_data() -> Event {
174196
let (string_ext_name, string_ext_value) = string_extension();
175197
let (bool_ext_name, bool_ext_value) = bool_extension();

0 commit comments

Comments
 (0)