Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions src/utils/json/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ pub enum JsonFlattenError {
ExpectedObjectInArray,
#[error("Found non-object element while flattening array of objects")]
NonObjectInArray,
#[error("heavily nested, cannot flatten this JSON")]
HeavilyNestedJson,
}

// Recursively flattens JSON objects and arrays, e.g. with the separator `.`, starting from the TOP
Expand Down Expand Up @@ -285,10 +283,6 @@ pub fn flatten_array_objects(
/// 4. `{"a": [{"b": 1}, {"c": 2}], "d": {"e": 4}}` ~> `[{"a": {"b":1}, "d": {"e":4}}, {"a": {"c":2}, "d": {"e":4}}]`
/// 5. `{"a":{"b":{"c":{"d":{"e":["a","b"]}}}}}` ~> returns error - heavily nested, cannot flatten this JSON
pub fn generic_flattening(value: &Value) -> Result<Vec<Value>, JsonFlattenError> {
if has_more_than_four_levels(value, 1) {
return Err(JsonFlattenError::HeavilyNestedJson);
}

match value {
Value::Array(arr) => Ok(arr
.iter()
Expand Down Expand Up @@ -342,7 +336,7 @@ pub fn generic_flattening(value: &Value) -> Result<Vec<Value>, JsonFlattenError>
/// example -
/// 1. `{"a":{"b":{"c":{"d":{"e":["a","b"]}}}}}` ~> returns true
/// 2. `{"a": [{"b": 1}, {"c": 2}], "d": {"e": 4}}` ~> returns false
fn has_more_than_four_levels(value: &Value, current_level: usize) -> bool {
pub fn has_more_than_four_levels(value: &Value, current_level: usize) -> bool {
if current_level > 4 {
return true;
}
Expand Down Expand Up @@ -649,15 +643,9 @@ mod tests {
}

#[test]
fn flatten_json_success() {
fn flatten_json() {
let value = json!({"a":{"b":{"e":["a","b"]}}});
let expected = vec![json!({"a":{"b":{"e":"a"}}}), json!({"a":{"b":{"e":"b"}}})];
assert_eq!(generic_flattening(&value).unwrap(), expected);
}

#[test]
fn flatten_json_error() {
let value = json!({"a":{"b":{"c":{"d":{"e":["a","b"]}}}}});
assert!(generic_flattening(&value).is_err());
}
}
17 changes: 8 additions & 9 deletions src/utils/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use std::num::NonZeroU32;

use flatten::{convert_to_array, generic_flattening};
use flatten::{convert_to_array, generic_flattening, has_more_than_four_levels};
use serde_json;
use serde_json::Value;

Expand All @@ -37,15 +37,14 @@ pub fn flatten_json_body(
schema_version: SchemaVersion,
validation_required: bool,
) -> Result<Value, anyhow::Error> {
let mut nested_value = if schema_version == SchemaVersion::V1 {
if let Ok(flattened_json) = generic_flattening(&body) {
convert_to_array(flattened_json)?
} else {
// Flatten the json body only if new schema and has less than 4 levels of nesting
let mut nested_value =
if schema_version == SchemaVersion::V0 || has_more_than_four_levels(&body, 1) {
body
}
} else {
body
};
} else {
let flattened_json = generic_flattening(&body)?;
convert_to_array(flattened_json)?
};
flatten::flatten(
&mut nested_value,
"_",
Expand Down
Loading