Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
Fixed import of to-many-associations by ignoring null values
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteJanz committed Jun 30, 2024
1 parent b2204f0 commit f447f1c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/data/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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"
},
})
);
}
}

0 comments on commit f447f1c

Please sign in to comment.