Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect parsing of string with trailing \" #611

Closed
SimonSapin opened this issue Aug 7, 2023 · 0 comments · Fixed by #633
Closed

Incorrect parsing of string with trailing \" #611

SimonSapin opened this issue Aug 7, 2023 · 0 comments · Fixed by #633
Assignees
Labels
apollo-parser bug Something isn't working

Comments

@SimonSapin
Copy link
Contributor

Description

The conversion of StringValue to String is incorrect when the value ends with an escaped double-quote \". There is likely a similar bug with block strings that end with \""" or starts with " but it is masked by #609. The offending code removes any number of " characters here:

unescape_string(text.trim_start_matches('"').trim_end_matches('"'))

Steps to reproduce

crates/apollo-parser/src/ast/node_ext.rs

#[cfg(test)]
mod tests {
    use crate::ast;
    use crate::Parser;

    #[test]
    fn test_tailing_escaped_quote() {
        let input = r#"
            query ($arg: Int!) {
                field(
                    string: "a\""
                )
            }
        "#;
        let values = extract_argument_values(input);
        let ast::Value::StringValue(block_string) = &values[0] else { panic!() };
        assert_eq!(String::try_from(block_string).as_deref(), Ok(r#"a""#));
    }

    fn extract_argument_values(input: &str) -> Vec<ast::Value> {
        let ast = Parser::new(input).parse();
        assert_eq!(ast.errors().as_slice(), []);
        let ast::Definition::OperationDefinition(operation) = ast
            .document()
            .definitions()
            .next()
            .unwrap()
        else { panic!("expected an operation") };
        let ast::Selection::Field(field) = operation
            .selection_set()
            .unwrap()
            .selections()
            .next()
            .unwrap()
        else { panic!("expected a field") };
        field
            .arguments()
            .unwrap()
            .arguments()
            .map(|arg| arg.value().unwrap())
            .collect()
    }
}

Expected result

Test passes

Actual result

thread 'ast::node_ext::tests::test_tailing_escaped_quote' panicked at 'assertion failed: `(left == right)`
  left: `Ok("a\\")`,
 right: `Ok("a\"")`', crates/apollo-parser/src/ast/node_ext.rs:301:9

Environment

  • apollo-rs crate: apollo-parser
  • Crate version: either 0.5.3 or main 126816f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
apollo-parser bug Something isn't working
Projects
None yet
2 participants