Skip to content

Commit

Permalink
Merge pull request #4324 from rmosolgo/fix-json-hash-nils
Browse files Browse the repository at this point in the history
Fix nils in JSON Type hash values
  • Loading branch information
rmosolgo authored Feb 3, 2023
2 parents fe87d07 + 58d058e commit 7f599f4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
7 changes: 2 additions & 5 deletions lib/graphql/static_validation/literal_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ def replace_nulls_in(ast_value)
case ast_value
when Array
ast_value.map { |v| replace_nulls_in(v) }
when Hash
new_v = {}
ast_value.each do |k, v|
new_v[k] = replace_nulls_in(v)
end
when GraphQL::Language::Nodes::InputObject
ast_value.to_h
when GraphQL::Language::Nodes::NullValue
nil
else
Expand Down
24 changes: 23 additions & 1 deletion spec/graphql/schema/build_from_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,12 @@ def call(parent_type, field, object, args, context)
end

def coerce_input(type, value, ctx)
(ctx[:nils] ||= []).push(value[2])
nils = ctx[:nils] ||= []
if value.is_a?(Array)
nils << value[2]
else
nils << value["abc"]
end
::JSON.generate(value)
end

Expand Down Expand Up @@ -1707,6 +1712,23 @@ def coerce_result(type, value, ctx)
assert_equal([3, "abc", nil, 7], res_2["data"]["echoJsonValue"])
assert_equal [nil, nil], res_2.context[:nils]

res_3 = app.execute_query(<<~EOS, arg: { "abc" => nil, "def" => 7 })
query WithArg($arg: JsonValue) {
echoJsonValue(arg: $arg)
}
EOS

assert_equal({ "abc" => nil, "def" => 7 }, res_3["data"]["echoJsonValue"])
assert_equal [nil, nil], res_3.context[:nils]

res_4 = app.execute_query(<<~EOS)
query {
echoJsonValue(arg: { abc: null, def: 7, ghi: { jkl: null } })
}
EOS

assert_equal({ "abc" => nil, "def" => 7, "ghi"=>{"jkl"=>nil} }, res_4["data"]["echoJsonValue"])
assert_equal [nil, nil], res_4.context[:nils]
end
end
end

0 comments on commit 7f599f4

Please sign in to comment.