Skip to content

Commit 9a4bb7f

Browse files
klkvrEvalir
andauthored
Add correct processing for non-existent json-keys (#5511)
* Add correct processing for non-existent keys * Fix clippy error * chore: include changes in changelog --------- Co-authored-by: Enrique Ortiz <[email protected]>
1 parent 25d3ce7 commit 9a4bb7f

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ To use the latest pinned nightly on your CI, modify your Foundry installation st
3434
- [precompiles will not be compatible with all cheatcodes](https://github.com/foundry-rs/foundry/pull/4905).
3535
- The difficulty and prevrandao cheatcodes now [fail if not used with the correct EVM version](https://github.com/foundry-rs/foundry/pull/4904).
3636
- 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.
37+
- 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

evm/src/executor/inspector/cheatcodes/ext.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ fn canonicalize_json_key(key: &str) -> String {
246246

247247
/// Encodes a vector of [`Token`] into a vector of bytes.
248248
fn encode_abi_values(values: Vec<Token>) -> Vec<u8> {
249-
if values.len() == 1 {
249+
if values.is_empty() {
250+
abi::encode(&[Token::Bytes(Vec::new())])
251+
} else if values.len() == 1 {
250252
abi::encode(&[Token::Bytes(abi::encode(&values))])
251253
} else {
252254
abi::encode(&[Token::Bytes(abi::encode(&[Token::Array(values)]))])

testdata/cheats/Json.t.sol

+11
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ contract ParseJsonTest is DSTest {
148148
string memory decodedData = abi.decode(data, (string));
149149
assertEq("hai", decodedData);
150150
}
151+
152+
function test_nonExistentKey() public {
153+
bytes memory data = vm.parseJson(json, ".thisKeyDoesNotExist");
154+
assertEq(0, data.length);
155+
156+
data = vm.parseJson(json, ".this.path.does.n.0.t.exist");
157+
assertEq(0, data.length);
158+
159+
data = vm.parseJson("", ".");
160+
assertEq(0, data.length);
161+
}
151162
}
152163

153164
contract WriteJsonTest is DSTest {

0 commit comments

Comments
 (0)