Skip to content

Commit f616a4a

Browse files
authored
Merge pull request rmosolgo#661 from gjtorikian/get-wild-get-angry
Raise on UTF-8 encoding error
2 parents c5f45a1 + 66f2693 commit f616a4a

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

lib/graphql.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ module GraphQL
99
class Error < StandardError
1010
end
1111

12+
class CoercionError < Error
13+
end
14+
1215
class ParseError < Error
1316
attr_reader :line, :col, :query
1417
def initialize(message, line, col, query)

lib/graphql/string_type.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
coerce_result ->(value) {
77
str = value.to_s
8-
str.encoding == Encoding::US_ASCII || str.encoding == Encoding::UTF_8 ? str : nil
8+
return str if str.encoding == Encoding::US_ASCII || str.encoding == Encoding::UTF_8
9+
raise GraphQL::CoercionError.new("The string `#{str}` was encoded as #{str.encoding}! GraphQL requires all strings to be UTF-8 encoded.")
910
}
1011

1112
coerce_input ->(value) { value.is_a?(String) ? value : nil }

spec/graphql/string_type_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
describe "coerce_result" do
1212
it "requires string to be encoded as UTF-8" do
1313
binary_str = "\0\0\0foo\255\255\255".dup.force_encoding("BINARY")
14-
assert_equal nil, string_type.coerce_result(binary_str)
14+
assert_raises(GraphQL::CoercionError) {
15+
assert_equal nil, string_type.coerce_result(binary_str)
16+
}
1517
end
1618
end
1719

0 commit comments

Comments
 (0)