From 1906c8b299ff86148700593cbc24244cbbb144e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Thu, 1 Sep 2016 16:18:08 +0200 Subject: [PATCH 1/2] Lenient bytes deserialization --- rpc/src/v1/types/bytes.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/rpc/src/v1/types/bytes.rs b/rpc/src/v1/types/bytes.rs index 09c8990573f..6ab2d607238 100644 --- a/rpc/src/v1/types/bytes.rs +++ b/rpc/src/v1/types/bytes.rs @@ -70,7 +70,9 @@ impl Visitor for BytesVisitor { type Value = Bytes; fn visit_str(&mut self, value: &str) -> Result where E: Error { - if value.len() >= 2 && &value[0..2] == "0x" { + if value.is_empty() { + Ok(Bytes::new(Vec::new())) + } else if value.len() >= 2 && &value[0..2] == "0x" { Ok(Bytes::new(FromHex::from_hex(&value[2..]).unwrap_or_else(|_| vec![]))) } else { Err(Error::custom("invalid hex")) @@ -95,5 +97,20 @@ mod tests { let serialized = serde_json::to_string(&bytes).unwrap(); assert_eq!(serialized, r#""0x0123456789abcdef""#); } + + #[test] + fn test_bytes_deserialize() { + let deserialized: Bytes = serde_json::from_str(r#""0x""#).unwrap(); + let deserialized2: Bytes = serde_json::from_str(r#""0x0123456789abcdef""#).unwrap(); + + assert_eq!(deserialized, Bytes(Vec::new())); + assert_eq!(deserialized2, "0123456789abcdef".from_hex().unwrap().into()); + } + + #[test] + fn test_bytes_lenient_against_the_spec_deserialize_for_empty_string_for_geth_compatibility() { + let deserialized: Bytes = serde_json::from_str(r#""""#).unwrap(); + assert_eq!(deserialized, Bytes(Vec::new())); + } } From 55b804ad6bff7f0330b06e06d0508e6341055eb6 Mon Sep 17 00:00:00 2001 From: Tomusdrw Date: Mon, 26 Sep 2016 09:29:57 +0200 Subject: [PATCH 2/2] Printing warning --- rpc/src/v1/types/bytes.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rpc/src/v1/types/bytes.rs b/rpc/src/v1/types/bytes.rs index f84d93d02dd..74d538b72e4 100644 --- a/rpc/src/v1/types/bytes.rs +++ b/rpc/src/v1/types/bytes.rs @@ -71,6 +71,10 @@ impl Visitor for BytesVisitor { fn visit_str(&mut self, value: &str) -> Result where E: Error { if value.is_empty() { + warn!( + target: "deprecated", + "Deserializing empty string as empty bytes. This is a non-standard behaviour that will be removed in future versions. Please update your code to send `0x` instead!" + ); Ok(Bytes::new(Vec::new())) } else if value.len() >= 2 && &value[0..2] == "0x" && value.len() & 1 == 0 { Ok(Bytes::new(FromHex::from_hex(&value[2..]).unwrap_or_else(|_| vec![])))