Skip to content

Commit

Permalink
fix: push_back not respecting empty strings
Browse files Browse the repository at this point in the history
`Pointer::push_back`: Handle empty keys
  • Loading branch information
chanced authored Feb 23, 2024
2 parents 08d5917 + f272787 commit dfbbd87
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ impl Pointer {
/// Pushes a `Token` onto the back of this `Pointer`.
pub fn push_back(&mut self, token: Token) {
self.count += 1;
if !self.inner.ends_with('/') {
self.inner.push('/');
}
self.inner.push('/');
self.inner.push_str(token.encoded());
}

Expand All @@ -167,7 +165,6 @@ impl Pointer {
self.inner = String::from("/") + &front;
Token::from_encoded(back)
}

})
}
/// Removes and returns the first `Token` in the `Pointer` if it exists.
Expand Down Expand Up @@ -1780,6 +1777,29 @@ mod tests {
);
}

#[test]
fn nested_maps_with_empty_keys() {
let data = json!({
"": {
"": {
"bar": 42,
}
}
});

{
let ptr = Pointer::try_from("///bar").unwrap();
assert_eq!(ptr.resolve(&data).unwrap(), 42);
}
{
let mut ptr = Pointer::root();
ptr.push_back("".into());
ptr.push_back("".into());
ptr.push_back("bar".into());
assert_eq!(ptr.resolve(&data).unwrap(), 42);
}
}

#[test]
#[cfg(feature = "fluent-uri")]
fn test_try_from_fluent_uri() {
Expand Down

0 comments on commit dfbbd87

Please sign in to comment.