mirror of
https://github.com/storytold/deckcraft.git
synced 2026-10-27 20:16:50 -06:00
Reject invalid shape ids in ids instead of using the selection
Found while testing crates/engine/src/cmd/mod.rs:ids_param / targets (~line 335-350) Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 5.5
parent
f86928d0d5
commit
fb778030c9
@@ -355,6 +355,11 @@ pub(crate) fn with_param(p: &Value, key: &str, v: Value) -> Value {
|
||||
|
||||
/// Targets: `ids` / `id` params, else the selection.
|
||||
pub(crate) fn targets(s: &Session, p: &Value) -> Result<Vec<ShapeId>> {
|
||||
if let Some(a) = p.get("ids").and_then(Value::as_array)
|
||||
&& let Some(v) = a.iter().find(|v| v.as_u64().and_then(|v| u32::try_from(v).ok()).is_none())
|
||||
{
|
||||
return Err(bad("shape", format!("`ids` has an invalid shape id: {v}")));
|
||||
}
|
||||
if let Some(ids) = ids_param(p, "ids")
|
||||
&& !ids.is_empty()
|
||||
{
|
||||
|
||||
@@ -4403,3 +4403,14 @@ fn duplicating_a_moved_copy_repeats_the_move() {
|
||||
assert_eq!(boxed(&mut s, f), (340.0 + off, 100.0 + off));
|
||||
assert_eq!(s.doc().unwrap().doc.validate(), Vec::<String>::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_ids_array_never_falls_back_to_the_selection() {
|
||||
let mut s = session();
|
||||
let a = s.execute("shape.insert", &json!({"preset": "rect", "rect": [100, 100, 100, 100]})).unwrap()["id"].as_u64().unwrap();
|
||||
for ids in [json!([-1]), json!([1.5]), json!([4294967296u64]), json!([-1, a])] {
|
||||
let r = s.execute("edit.delete", &json!({ "ids": ids }));
|
||||
assert!(matches!(r, Err(crate::EngineError::BadParams { .. })), "invalid ids {ids} gave {r:?}");
|
||||
assert!(s.doc().unwrap().shape(deckcraft_model::ShapeId(a as u32)).is_some(), "shape deleted for {ids}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user