Skip to content

Commit

Permalink
fix: Add escape_json function to the serializer (#162)
Browse files Browse the repository at this point in the history
* escape_json

* add comments on function, cargo fmt
  • Loading branch information
khorolets authored Sep 6, 2021
1 parent b3037f5 commit 90d8435
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.9.3 (hotfix)

* Escape `args_json` on the fly to avoid null-byte issues

## 0.9.2 (hotfix)

* Change `receiver_id` field type to `String` to be compatible with `nearcore` `AccessKeyPermissionView` struct (it caused problems during AccessKey serialization)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "indexer-explorer"
version = "0.9.2"
version = "0.9.3"
authors = ["Near Inc <hello@nearprotocol.com>"]
edition = "2018"

Expand Down
24 changes: 23 additions & 1 deletion src/models/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ pub(crate) fn extract_action_type_and_value_from_action_view(
// args_base64
// See for reference https://github.com/near/near-indexer-for-explorer/issues/87
if let Ok(decoded_args) = base64::decode(args) {
if let Ok(args_json) = serde_json::from_slice(&decoded_args) {
if let Ok(mut args_json) = serde_json::from_slice(&decoded_args) {
escape_json(&mut args_json);
arguments["args_json"] = args_json;
}
}
Expand Down Expand Up @@ -138,3 +139,24 @@ pub(crate) fn extract_action_type_and_value_from_action_view(
),
}
}

/// This function will modify the JSON escaping the values
/// We can not store data with null-bytes in TEXT or JSONB fields
/// of PostgreSQL
/// ref: https://www.commandprompt.com/blog/null-characters-workarounds-arent-good-enough/
fn escape_json(object: &mut serde_json::Value) {
match object {
serde_json::Value::Object(ref mut value) => {
for (_key, val) in value {
escape_json(val);
}
}
serde_json::Value::Array(ref mut values) => {
for element in values.iter_mut() {
escape_json(element)
}
}
serde_json::Value::String(ref mut value) => *value = value.escape_default().to_string(),
_ => {}
}
}

0 comments on commit 90d8435

Please sign in to comment.