Skip to content

Commit 528fa5e

Browse files
authored
Merge pull request #5285 from rmosolgo/one-of-prepare-fix
InputObject: validate even when prepare is overridden
2 parents 0baf584 + 765dfb0 commit 528fa5e

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

lib/graphql/schema/argument.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def statically_coercible?
217217
# @api private
218218
def prepare_value(obj, value, context: nil)
219219
if type.unwrap.kind.input_object?
220-
value = recursively_prepare_input_object(value, type)
220+
value = recursively_prepare_input_object(value, type, context)
221221
end
222222

223223
Schema::Validator.validate!(validators, obj, context, value)
@@ -398,15 +398,16 @@ def initialize(argument)
398398

399399
private
400400

401-
def recursively_prepare_input_object(value, type)
401+
def recursively_prepare_input_object(value, type, context)
402402
if type.non_null?
403403
type = type.of_type
404404
end
405405

406406
if type.list? && !value.nil?
407407
inner_type = type.of_type
408-
value.map { |v| recursively_prepare_input_object(v, inner_type) }
408+
value.map { |v| recursively_prepare_input_object(v, inner_type, context) }
409409
elsif value.is_a?(GraphQL::Schema::InputObject)
410+
value.validate_for(context)
410411
value.prepare
411412
else
412413
value

lib/graphql/schema/input_object.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,7 @@ def deconstruct_keys(keys = nil)
6464
end
6565

6666
def prepare
67-
if @context
68-
object = @context[:current_object]
69-
# Pass this object's class with `as` so that messages are rendered correctly from inherited validators
70-
Schema::Validator.validate!(self.class.validators, object, @context, @ruby_style_hash, as: self.class)
71-
self
72-
else
73-
self
74-
end
67+
self
7568
end
7669

7770
def unwrap_value(value)
@@ -111,6 +104,14 @@ def to_kwargs
111104
@ruby_style_hash.dup
112105
end
113106

107+
# @api private
108+
def validate_for(context)
109+
object = context[:current_object]
110+
# Pass this object's class with `as` so that messages are rendered correctly from inherited validators
111+
Schema::Validator.validate!(self.class.validators, object, context, @ruby_style_hash, as: self.class)
112+
nil
113+
end
114+
114115
class << self
115116
def authorized?(obj, value, ctx)
116117
# Authorize each argument (but this doesn't apply if `prepare` is implemented):

spec/graphql/schema/input_object_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ class TestInput2 < GraphQL::Schema::InputObject
765765
input_object = InputObjectDigTest::TestInput2.new(
766766
nil,
767767
ruby_kwargs: arg_values,
768-
context: nil,
768+
context: GraphQL::Query::NullContext.instance,
769769
defaults_used: Set.new
770770
)
771771
it "returns the value at that key" do
@@ -808,7 +808,7 @@ class TestInput2 < GraphQL::Schema::InputObject
808808
input_object = InputObjectPatternTest::TestInput2.new(
809809
nil,
810810
ruby_kwargs: arg_values,
811-
context: nil,
811+
context: GraphQL::Query::NullContext.instance,
812812
defaults_used: Set.new
813813
)
814814
it "matches the value at that key" do

spec/graphql/schema/validator/validator_helpers.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def build_schema(arg_type, validates_config)
2626
argument :b, arg_type, required: false
2727
argument :c, arg_type, required: false
2828
validates(validates_config)
29+
30+
def prepare
31+
self
32+
end
2933
end
3034

3135
validated_resolver = Class.new(GraphQL::Schema::Resolver) do

0 commit comments

Comments
 (0)