fix(solc): serialize metadata as raw string (#1474)

This commit is contained in:
Matthias Seitz 2022-07-12 20:28:29 +02:00 committed by GitHub
parent 98e6288036
commit ef8d97e05e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 6 deletions

View File

@ -898,7 +898,7 @@ impl Serialize for LosslessMetadata {
where where
S: Serializer, S: Serializer,
{ {
self.raw_metadata.serialize(serializer) serializer.serialize_str(&self.raw_metadata)
} }
} }
@ -920,8 +920,8 @@ impl<'de> Deserialize<'de> for LosslessMetadata {
where where
E: serde::de::Error, E: serde::de::Error,
{ {
let raw_metadata = value.to_string();
let metadata = serde_json::from_str(value).map_err(serde::de::Error::custom)?; let metadata = serde_json::from_str(value).map_err(serde::de::Error::custom)?;
let raw_metadata = value.to_string();
Ok(LosslessMetadata { raw_metadata, metadata }) Ok(LosslessMetadata { raw_metadata, metadata })
} }
} }
@ -2097,4 +2097,27 @@ mod tests {
]) ])
); );
} }
#[test]
fn test_lossless_metadata() {
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Contract {
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "serde_helpers::json_string_opt"
)]
pub metadata: Option<LosslessMetadata>,
}
let s = r#"{"metadata":"{\"compiler\":{\"version\":\"0.4.18+commit.9cf6e910\"},\"language\":\"Solidity\",\"output\":{\"abi\":[{\"constant\":true,\"inputs\":[],\"name\":\"owner\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"newOwner\",\"type\":\"address\"}],\"name\":\"transferOwnership\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}],\"devdoc\":{\"methods\":{\"transferOwnership(address)\":{\"details\":\"Allows the current owner to transfer control of the contract to a newOwner.\",\"params\":{\"newOwner\":\"The address to transfer ownership to.\"}}},\"title\":\"Ownable\"},\"userdoc\":{\"methods\":{}}},\"settings\":{\"compilationTarget\":{\"src/Contract.sol\":\"Ownable\"},\"libraries\":{},\"optimizer\":{\"enabled\":true,\"runs\":1000000},\"remappings\":[\":src/=src/\"]},\"sources\":{\"src/Contract.sol\":{\"keccak256\":\"0x3e0d611f53491f313ae035797ed7ecfd1dfd8db8fef8f82737e6f0cd86d71de7\",\"urls\":[\"bzzr://9c33025fa9d1b8389e4c7c9534a1d70fad91c6c2ad70eb5e4b7dc3a701a5f892\"]}},\"version\":1}"}"#;
let value: serde_json::Value = serde_json::from_str(s).unwrap();
let c: Contract = serde_json::from_value(value).unwrap();
assert_eq!(c.metadata.as_ref().unwrap().raw_metadata, "{\"compiler\":{\"version\":\"0.4.18+commit.9cf6e910\"},\"language\":\"Solidity\",\"output\":{\"abi\":[{\"constant\":true,\"inputs\":[],\"name\":\"owner\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"newOwner\",\"type\":\"address\"}],\"name\":\"transferOwnership\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}],\"devdoc\":{\"methods\":{\"transferOwnership(address)\":{\"details\":\"Allows the current owner to transfer control of the contract to a newOwner.\",\"params\":{\"newOwner\":\"The address to transfer ownership to.\"}}},\"title\":\"Ownable\"},\"userdoc\":{\"methods\":{}}},\"settings\":{\"compilationTarget\":{\"src/Contract.sol\":\"Ownable\"},\"libraries\":{},\"optimizer\":{\"enabled\":true,\"runs\":1000000},\"remappings\":[\":src/=src/\"]},\"sources\":{\"src/Contract.sol\":{\"keccak256\":\"0x3e0d611f53491f313ae035797ed7ecfd1dfd8db8fef8f82737e6f0cd86d71de7\",\"urls\":[\"bzzr://9c33025fa9d1b8389e4c7c9534a1d70fad91c6c2ad70eb5e4b7dc3a701a5f892\"]}},\"version\":1}");
let value = serde_json::to_string(&c).unwrap();
pretty_assertions::assert_eq!(s, value);
}
} }

View File

@ -41,7 +41,7 @@ where
pub mod json_string_opt { pub mod json_string_opt {
use serde::{ use serde::{
de::{self, DeserializeOwned}, de::{self, DeserializeOwned},
ser, Deserialize, Deserializer, Serialize, Serializer, Deserialize, Deserializer, Serialize, Serializer,
}; };
pub fn serialize<T, S>(value: &Option<T>, serializer: S) -> Result<S::Ok, S::Error> pub fn serialize<T, S>(value: &Option<T>, serializer: S) -> Result<S::Ok, S::Error>
@ -50,8 +50,7 @@ pub mod json_string_opt {
T: Serialize, T: Serialize,
{ {
if let Some(value) = value { if let Some(value) = value {
let value = serde_json::to_string(value).map_err(ser::Error::custom)?; value.serialize(serializer)
serializer.serialize_str(&value)
} else { } else {
serializer.serialize_none() serializer.serialize_none()
} }

View File

@ -253,7 +253,7 @@ where
CURRENT_STATE CURRENT_STATE
.try_with(|state| { .try_with(|state| {
let scoped = state.scoped.borrow_mut(); let scoped = state.scoped.borrow_mut();
f(&*scoped) f(&scoped)
}) })
.unwrap_or_else(|_| f(&Report::none())) .unwrap_or_else(|_| f(&Report::none()))
} }