Skip to content

Commit

Permalink
Add correct processing for non-existent json-keys (#5511)
Browse files Browse the repository at this point in the history
* Add correct processing for non-existent keys

* Fix clippy error

* chore: include changes in changelog

---------

Co-authored-by: Enrique Ortiz <hi@enriqueortiz.dev>
  • Loading branch information
klkvr and Evalir authored Aug 1, 2023
1 parent 25d3ce7 commit 9a4bb7f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ To use the latest pinned nightly on your CI, modify your Foundry installation st
- [precompiles will not be compatible with all cheatcodes](https://github.com/foundry-rs/foundry/pull/4905).
- The difficulty and prevrandao cheatcodes now [fail if not used with the correct EVM version](https://github.com/foundry-rs/foundry/pull/4904).
- The default EVM version will be Shanghai. If you're using an EVM chain which is not compatible with [EIP-3855](https://eips.ethereum.org/EIPS/eip-3855) you need to change your EVM version. See [Matt Solomon's thread](https://twitter.com/msolomon44/status/1656411871635972096) for more information.
- Non-existent JSON keys are now processed correctly, and `parseJson` returns non-decodable empty bytes if they do not exist. https://github.com/foundry-rs/foundry/pull/5511
4 changes: 3 additions & 1 deletion evm/src/executor/inspector/cheatcodes/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ fn canonicalize_json_key(key: &str) -> String {

/// Encodes a vector of [`Token`] into a vector of bytes.
fn encode_abi_values(values: Vec<Token>) -> Vec<u8> {
if values.len() == 1 {
if values.is_empty() {
abi::encode(&[Token::Bytes(Vec::new())])
} else if values.len() == 1 {
abi::encode(&[Token::Bytes(abi::encode(&values))])
} else {
abi::encode(&[Token::Bytes(abi::encode(&[Token::Array(values)]))])
Expand Down
11 changes: 11 additions & 0 deletions testdata/cheats/Json.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ contract ParseJsonTest is DSTest {
string memory decodedData = abi.decode(data, (string));
assertEq("hai", decodedData);
}

function test_nonExistentKey() public {
bytes memory data = vm.parseJson(json, ".thisKeyDoesNotExist");
assertEq(0, data.length);

data = vm.parseJson(json, ".this.path.does.n.0.t.exist");
assertEq(0, data.length);

data = vm.parseJson("", ".");
assertEq(0, data.length);
}
}

contract WriteJsonTest is DSTest {
Expand Down

0 comments on commit 9a4bb7f

Please sign in to comment.