Skip to content

Commit 3c0dfc0

Browse files
author
Robert Mosolgo
authored
Merge pull request #2505 from daemonsy/bad_enums
Disallow casting of Enums into scalar values
2 parents a16ce4f + 8db2ebc commit 3c0dfc0

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

lib/graphql/scalar_type.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,14 @@ def raw_coercion_input(value)
117117
value.to_h
118118
elsif value.is_a?(Array)
119119
value.map { |element| raw_coercion_input(element) }
120-
elsif value.is_a?(GraphQL::Language::Nodes::Enum)
121-
value.name
122120
else
123121
value
124122
end
125123
end
126124

127125
def validate_non_null_input(value, ctx)
128126
result = Query::InputValidationResult.new
129-
if coerce_non_null_input(value, ctx).nil?
127+
if value.is_a?(GraphQL::Language::Nodes::Enum) || coerce_non_null_input(value, ctx).nil?
130128
result.add_problem("Could not coerce value #{GraphQL::Language.serialize(value)} to #{name}")
131129
end
132130
result

spec/graphql/float_type_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
require "spec_helper"
33

44
describe GraphQL::FLOAT_TYPE do
5+
let(:enum) { GraphQL::Language::Nodes::Enum.new(name: 'MILK') }
6+
57
describe "coerce_input" do
68
it "accepts ints and floats" do
79
assert_equal 1.0, GraphQL::FLOAT_TYPE.coerce_isolated_input(1)
@@ -11,6 +13,7 @@
1113
it "rejects other types" do
1214
assert_nil GraphQL::FLOAT_TYPE.coerce_isolated_input("55")
1315
assert_nil GraphQL::FLOAT_TYPE.coerce_isolated_input(true)
16+
assert_nil GraphQL::FLOAT_TYPE.coerce_isolated_input(enum)
1417
end
1518
end
1619
end

spec/graphql/id_type_spec.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,20 @@
2222
end
2323
end
2424

25-
describe "coercion for other types" do
25+
describe "coercion for float" do
2626
let(:query_string) { %|query getMilk { cow: milk(id: 1.0) { id } }| }
2727

28-
it "doesn't allow other types" do
28+
it "results in an error" do
29+
assert_nil result["data"]
30+
31+
assert_equal 1, result["errors"].length
32+
end
33+
end
34+
35+
describe "coercion for enum values" do
36+
let(:query_string) { %|query getMilk { milk(id: dairy_rocks) { id } }|}
37+
38+
it "results in an error" do
2939
assert_nil result["data"]
3040
assert_equal 1, result["errors"].length
3141
end

spec/graphql/static_validation/rules/argument_literals_are_compatible_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,40 @@
137137
end
138138
end
139139

140+
describe "using enums for scalar arguments it adds an error" do
141+
let(:query_string) { <<-GRAPHQL
142+
{
143+
cheese(id: I_AM_ENUM_VALUE) {
144+
source
145+
}
146+
}
147+
GRAPHQL
148+
}
149+
150+
let(:enum_invalid_for_id_error) do
151+
{
152+
"message" => "Argument 'id' on Field 'cheese' has an invalid value. Expected type 'Int!'.",
153+
"locations" => [{ "line" => 2, "column" => 9 }],
154+
"path"=> ["query", "cheese", "id"],
155+
"extensions"=> { "code" => "argumentLiteralsIncompatible", "typeName" => "Field", "argumentName" => "id" }
156+
}
157+
end
158+
159+
it "works with error bubbling disabled" do
160+
without_error_bubbling(schema) do
161+
assert_includes(errors, enum_invalid_for_id_error)
162+
assert_equal 1, errors.length
163+
end
164+
end
165+
166+
it "works with error bubbling enabled" do
167+
with_error_bubbling(schema) do
168+
assert_includes(errors, enum_invalid_for_id_error)
169+
assert_equal 1, errors.length
170+
end
171+
end
172+
end
173+
140174
describe "null value" do
141175
describe "nullable arg" do
142176
let(:schema) {

0 commit comments

Comments
 (0)