Skip to content

Commit 39ea811

Browse files
committed
Don't make replacements in block strings
1 parent 119617e commit 39ea811

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

lib/graphql/language/lexer.rb

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,20 @@ def string_value
114114
is_block = str.start_with?('"""')
115115
if is_block
116116
str.gsub!(/\A"""|"""\z/, '')
117+
return Language::BlockString.trim_whitespace(str)
117118
else
118119
str.gsub!(/\A"|"\z/, '')
119-
end
120120

121-
if is_block
122-
str = Language::BlockString.trim_whitespace(str)
123-
end
124-
125-
if !str.valid_encoding? || !str.match?(VALID_STRING)
126-
raise_parse_error("Bad unicode escape in #{str.inspect}")
127-
else
128-
Lexer.replace_escaped_characters_in_place(str)
129-
130-
if !str.valid_encoding?
121+
if !str.valid_encoding? || !str.match?(VALID_STRING)
131122
raise_parse_error("Bad unicode escape in #{str.inspect}")
132123
else
133-
str
124+
Lexer.replace_escaped_characters_in_place(str)
125+
126+
if !str.valid_encoding?
127+
raise_parse_error("Bad unicode escape in #{str.inspect}")
128+
else
129+
str
130+
end
134131
end
135132
end
136133
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
example1: getString(string: "\u0064") # should return "d"
3+
example2: getString(string: "\\u0064") # should return "\\u0064"
4+
# example3: getString(string: "\u006") # validation error
5+
example4: getString(string: "\\u006") # should return "\\u006"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
query bug2 {
2+
example1: getString(string: """\a""") # should be "\\a"
3+
example2: getString(string: """\u006""") # should be "\\u006"
4+
example3: getString(string: """\n""") # should be "\\n"
5+
example4: getString(string: """\u0064""") # should be "\\u0064"
6+
}

spec/graphql/types/string_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,15 @@ def get_string(string:)
129129
res2 = UnicodeEscapeSchema.execute(error_query_str)
130130
assert_equal ["Expected string or block string, but it was malformed"], res2["errors"].map { |err| err["message"] }
131131
end
132+
133+
it "parses escapes properly in triple-quoted strings" do
134+
query_str = File.read("./spec/fixtures/unicode_escapes/query2.graphql")
135+
res = UnicodeEscapeSchema.execute(query_str)
136+
# No replacing in block strings:
137+
assert_equal "\\a", res["data"]["example1"]
138+
assert_equal "\\u006", res["data"]["example2"]
139+
assert_equal "\\n", res["data"]["example3"]
140+
assert_equal "\\u0064", res["data"]["example4"]
141+
end
132142
end
133143
end

0 commit comments

Comments
 (0)