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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `Delete::delete` no longer panics when a pointer's final token resolves to
an array index equal to the array's length (e.g. `-` on any array, or a
numeric index equal to the length). It now returns `None` and leaves the
document unmodified, as documented. Fixes
[#120](https://github.com/chanced/jsonptr/issues/120).
- `EncodingError` and `ParseIndexError` now implement `Diagnostic`, which
unifies the API for errors originating from parse-like operations.
- Fixes returning an incorrect error type when parsing a `Token` that
Expand Down
52 changes: 50 additions & 2 deletions src/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ mod json {
.ok()
.and_then(|parent| match parent {
Value::Array(children) => {
let idx = last.to_index().ok()?.for_len_incl(children.len()).ok()?;
let idx = last.to_index().ok()?.for_len(children.len()).ok()?;
children.remove(idx).into()
}
Value::Object(children) => children.remove(last.decoded().as_ref()),
Expand Down Expand Up @@ -143,7 +143,7 @@ mod toml {
.ok()
.and_then(|parent| match parent {
Value::Array(children) => {
let idx = last.to_index().ok()?.for_len_incl(children.len()).ok()?;
let idx = last.to_index().ok()?.for_len(children.len()).ok()?;
children.remove(idx).into()
}
Value::Table(children) => children.remove(last.decoded().as_ref()),
Expand Down Expand Up @@ -269,6 +269,38 @@ mod tests {
expected_data: json!(null),
expected_deleted: Some(json!({"Example": 21, "test": "test"})),
},
// 9
// issue #120 - `-` on an array must miss rather than panic
Test {
ptr: "/-",
data: json!([1, 2, 3]),
expected_data: json!([1, 2, 3]),
expected_deleted: None,
},
// 10
// issue #120 - numeric index equal to the array length misses
Test {
ptr: "/3",
data: json!([1, 2, 3]),
expected_data: json!([1, 2, 3]),
expected_deleted: None,
},
// 11
// issue #120 - `/0` on an empty array misses
Test {
ptr: "/0",
data: json!([]),
expected_data: json!([]),
expected_deleted: None,
},
// 12
// issue #120 - nested `-` on an array misses
Test {
ptr: "/foo/-",
data: json!({"foo": [1, 2]}),
expected_data: json!({"foo": [1, 2]}),
expected_deleted: None,
},
]);
}
/*
Expand Down Expand Up @@ -339,6 +371,22 @@ mod tests {
expected_data: toml! {"test" = "test"}.into(),
expected_deleted: Some(21.into()),
},
// 8
// issue #120 - `-` on an array must miss rather than panic
Test {
data: toml! {"foo" = [1, 2, 3]}.into(),
ptr: "/foo/-",
expected_data: toml! {"foo" = [1, 2, 3]}.into(),
expected_deleted: None,
},
// 9
// issue #120 - numeric index equal to the array length misses
Test {
data: toml! {"foo" = [1, 2, 3]}.into(),
ptr: "/foo/3",
expected_data: toml! {"foo" = [1, 2, 3]}.into(),
expected_deleted: None,
},
]);
}
}
Loading