besok / jsonpath-rust

Support for json-path in Rust
https://crates.io/crates/jsonpath-rust
MIT License
104 stars 27 forks source link

Regex parentheses escape #52

Closed catalinstochita closed 1 year ago

catalinstochita commented 1 year ago

Hello. It looks like escaping characters in regex is problematic

#[test]
fn regex_filter_test(){
    let json: Box<Value> = Box::new(json!({
        "author":"d(Rees)",
    }));

    let path: Box<JsonPathInst> = Box::from(
        JsonPathInst::from_str("$.[?(@.author ~= 'd(Rees)')]").expect("the path is correct"),
    );
    let finder = JsonPathFinder::new(json.clone(), path);
    assert_eq!(finder.find_slice(),vec![Slice(&json!({"author":"d(Rees)",}))]);
}

assertion left == right failed left: [NoValue] right: [Slice(Object {"author": String("d(Rees)")})]

If you try to escape with \ it won't work either 'd\(Rees\)' won't compile, expecting an atom 'd\\(Rees\\)' compiles but it will look for \ in value and () is still treated like a capture group And if you don't escape the () is considered a capture group

catalinstochita commented 1 year ago

After some testing, looks like we can change the char in .pest to accept it From

char = _{
    !("\"" | "\\" | "\'") ~ ANY
    | "\\" ~ ("\"" | "\'" |  "\\" | "/" | "b" | "f" | "n" | "r" | "t")
    | "\\" ~ ("u" ~ ASCII_HEX_DIGIT{4})
}

To

char = _{
    !("\"" | "\\" | "\'") ~ ANY
    | "\\" ~ ("\"" | "\'" |  "\\" | "/" | "b" | "f" | "n" | "r" | "t" | "(" | ")")
    | "\\" ~ ("u" ~ ASCII_HEX_DIGIT{4})
}
besok commented 1 year ago

Hi @catalinstochita ! Thanks for bringing this up. Yeah, it seems to be a bug in the parser.