diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fd54cf..d917336 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # NEXT-RELEASE - Added `get_default(name)` function to scripting. It allows lookup of constants like the `Shopware/src/Core/Defaults.php` +- Fixed import of "To-Many-Associations" when the value is null it will be ignored instead of added to the entity # v0.4.0 - "To-One-Association" values are now imported correctly diff --git a/src/data/transform.rs b/src/data/transform.rs index 1135996..6902159 100644 --- a/src/data/transform.rs +++ b/src/data/transform.rs @@ -270,6 +270,8 @@ trait EntityPath { fn get_by_path(&self, path: &str) -> Option<&serde_json::Value>; /// Insert a value into a given path + /// ## Invariant: + /// Does nothing if the value is Null (to not create objects with only null values) fn insert_by_path(&mut self, path: &str, value: serde_json::Value); } @@ -328,6 +330,9 @@ impl EntityPath for Entity { if path.is_empty() { panic!("empty entity_path encountered"); } + if value.is_null() { + return; // do nothing + } let mut tokens = path.split('.').map(|t| t.trim_end_matches('?')).peekable(); @@ -541,5 +546,20 @@ mod tests { }, }) ); + + entity.insert_by_path("another.cousin.value", serde_json::Value::Null); + assert_eq!( + Value::Object(entity.clone()), + json!({ + "fiz": 42, + "child": { + "bar": "buz", + "hello": "world", + }, + "another": { + "nested": "replaced" + }, + }) + ); } }