Skip to content

Commit

Permalink
Escape graphql text in codegen
Browse files Browse the repository at this point in the history
Reviewed By: kassens

Differential Revision: D21559109

fbshipit-source-id: 2e40a38f030ddda5dd481d6c0e76802c19eeb571
  • Loading branch information
tyao1 authored and facebook-github-bot committed May 14, 2020
1 parent c56b89d commit 2a3a802
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions compiler/crates/relay-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod constants;
mod indentation;
mod printer;
mod relay_test_operation;
mod utils;

pub use ast::{Primitive, RequestParameters};
pub use build_ast::{build_request_params, MetadataGeneratorFn};
Expand Down
8 changes: 7 additions & 1 deletion compiler/crates/relay-codegen/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::ast::{Ast, AstBuilder, AstKey, Primitive, RequestParameters};
use crate::build_ast::{build_fragment, build_operation, build_request, MetadataGeneratorFn};
use crate::constants::CODEGEN_CONSTANTS;
use crate::indentation::print_indentation;
use crate::utils::escape;

use graphql_ir::{FragmentDefinition, OperationDefinition};
use schema::Schema;
Expand Down Expand Up @@ -201,7 +202,12 @@ impl<'b> DedupedJSONPrinter<'b> {
match primitive {
Primitive::Null => write!(f, "null"),
Primitive::Bool(b) => write!(f, "{}", if *b { "true" } else { "false" }),
Primitive::RawString(str) => write!(f, "\"{}\"", str),
Primitive::RawString(str) => {
f.push('\"');
escape(str, f);
f.push('\"');
Ok(())
}
Primitive::String(key) => write!(f, "\"{}\"", key),
Primitive::Float(value) => write!(f, "{}", value.as_float()),
Primitive::Int(value) => write!(f, "{}", value),
Expand Down
37 changes: 37 additions & 0 deletions compiler/crates/relay-codegen/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/// Escape graphql text
pub fn escape(text: &str, output: &mut String) {
for char in text.chars() {
match char {
'\n' => output.push_str("\\n"),
'"' => output.push_str("\\\""),
'\\' => output.push_str("\\\\"),
'\t' => output.push_str("\\t"),
'\r' => output.push_str("\\r"),
_ => output.push(char),
}
}
}

#[test]
fn test_escape() {
let input = r#"query Query(
$unixname: String!
$passcode: String!
) @owner(oncall: "oncall") {
auth_2fa {
is_auth(unixname: $unixname, passcode: $passcode)
}
}
"#;
let expected = r#"query Query(\n $unixname: String!\n $passcode: String!\n) @owner(oncall: \"oncall\") {\n auth_2fa {\n is_auth(unixname: $unixname, passcode: $passcode)\n }\n}\n"#;
let mut output = String::new();
escape(input, &mut output);
assert_eq!(output, expected)
}

0 comments on commit 2a3a802

Please sign in to comment.