Skip to content

Commit 19e5dad

Browse files
ericproulxclaude
andauthored
Build param defaults and custom type coercions from frozen objects (#3097)
DefaultValidator and CustomTypeCoercer built procs whose self was the validator or the coercer, closing over the default or the coercion method, so a compiled API holding one could not be frozen all the way down. They build small Data objects answering call instead. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 000d9ec commit 19e5dad

3 files changed

Lines changed: 58 additions & 34 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* [#3092](https://github.com/ruby-grape/grape/pull/3092): Skip posting empty Danger report comments - [@dblock](https://github.com/dblock).
1717
* [#3095](https://github.com/ruby-grape/grape/pull/3095): Freeze Grape's constants all the way down - [@ericproulx](https://github.com/ericproulx).
1818
* [#3096](https://github.com/ruby-grape/grape/pull/3096): Define Grape's callable constants as modules rather than lambdas - [@ericproulx](https://github.com/ericproulx).
19+
* [#3097](https://github.com/ruby-grape/grape/pull/3097): Build param defaults and custom type coercions from frozen objects rather than procs - [@ericproulx](https://github.com/ericproulx).
1920
* [#3098](https://github.com/ruby-grape/grape/pull/3098): Look up the header versioner's vendor pattern once, when it is built - [@ericproulx](https://github.com/ericproulx).
2021
* Your contribution here.
2122

‎lib/grape/validations/types/custom_type_coercer.rb‎

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,39 @@ class CustomTypeCoercer
3737

3838
TYPE_CHECK_METHODS = %i[coerced? parsed?].freeze
3939
COLLECTION_TYPES = [Array, Set].freeze
40-
private_constant :TYPE_CHECK_METHODS, :COLLECTION_TYPES
40+
41+
# What a coercer calls to coerce and to check. Frozen objects rather
42+
# than lambdas: a lambda carries the coercer as its self and the values
43+
# it closes over as they are, so a compiled API holding one could not
44+
# be frozen all the way down.
45+
HashSymbolizer = Data.define(:coercion) do
46+
def call(val) = coercion.call(val).deep_symbolize_keys
47+
end
48+
49+
# +dup+ before +map!+: the collection is whatever the user's coercion
50+
# method returned — possibly frozen, possibly the input itself — so we
51+
# must not mutate it in place. +dup+ also preserves the class, keeping a
52+
# +Set+ a +Set+ (unlike +map+, which would return an +Array+).
53+
CollectionSymbolizer = Data.define(:coercion) do
54+
def call(val) = coercion.call(val).dup.map! { |item| item.is_a?(Hash) ? item.deep_symbolize_keys : item }
55+
end
56+
57+
InstanceCheck = Data.define(:type) do
58+
def call(value) = value.is_a?(type)
59+
end
60+
61+
EnumerableCheck = Data.define(:type) do
62+
def call(value) = value.is_a?(Enumerable) && value.all? { |val| matches?(type.first, val) }
63+
64+
private
65+
66+
def matches?(type, value)
67+
return value.all? { |val| matches?(type.first, val) } if type.is_a?(Enumerable) && value.is_a?(Enumerable)
68+
69+
!type.is_a?(Enumerable) && value.is_a?(type)
70+
end
71+
end
72+
private_constant :TYPE_CHECK_METHODS, :COLLECTION_TYPES, :HashSymbolizer, :CollectionSymbolizer, :InstanceCheck, :EnumerableCheck
4173

4274
# A new coercer for the given type specification
4375
# and coercion method.
@@ -72,8 +104,8 @@ def coerced?(val)
72104

73105
def build_coercion_method(type, method)
74106
coercion_method = infer_coercion_method(type, method)
75-
return hash_symbolizer(coercion_method) if type == Hash
76-
return collection_symbolizer(coercion_method) if COLLECTION_TYPES.include?(type)
107+
return HashSymbolizer.new(coercion_method) if type == Hash
108+
return CollectionSymbolizer.new(coercion_method) if COLLECTION_TYPES.include?(type)
77109

78110
coercion_method
79111
end
@@ -85,39 +117,13 @@ def infer_coercion_method(type, method)
85117
method.method(:parse)
86118
end
87119

88-
def hash_symbolizer(method)
89-
->(val) { method.call(val).deep_symbolize_keys }
90-
end
91-
92-
# +dup+ before +map!+: the collection is whatever the user's coercion
93-
# method returned — possibly frozen, possibly the input itself — so we
94-
# must not mutate it in place. +dup+ also preserves the class, keeping a
95-
# +Set+ a +Set+ (unlike +map+, which would return an +Array+).
96-
def collection_symbolizer(method)
97-
->(val) { method.call(val).dup.map! { |item| symbolize_if_hash(item) } }
98-
end
99-
100-
def symbolize_if_hash(item)
101-
item.is_a?(Hash) ? item.deep_symbolize_keys : item
102-
end
103-
104120
def infer_type_check(type)
105121
method_name = TYPE_CHECK_METHODS.detect { |m| type.respond_to?(m) }
106122
return type.method(method_name) if method_name
107123
return type if type.respond_to?(:call)
108-
return enumerable_type_check(type) if type.is_a?(Enumerable)
109-
110-
->(value) { value.is_a? type }
111-
end
112-
113-
def enumerable_type_check(type)
114-
->(value) { value.is_a?(Enumerable) && value.all? { |val| recursive_type_check(type.first, val) } }
115-
end
116-
117-
def recursive_type_check(type, value)
118-
return value.all? { |val| recursive_type_check(type.first, val) } if type.is_a?(Enumerable) && value.is_a?(Enumerable)
124+
return EnumerableCheck.new(type) if type.is_a?(Enumerable)
119125

120-
!type.is_a?(Enumerable) && value.is_a?(type)
126+
InstanceCheck.new(type)
121127
end
122128
end
123129
end

‎lib/grape/validations/validators/default_validator.rb‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,33 @@ module Grape
44
module Validations
55
module Validators
66
class DefaultValidator < Base
7+
# What answers a missing param's default. Frozen objects rather than
8+
# procs: a proc carries the validator as its self and the values it
9+
# closes over as they are, so a compiled API holding one could not be
10+
# frozen all the way down.
11+
Value = Data.define(:value) do
12+
def call(_params) = value
13+
end
14+
15+
Copy = Data.define(:value) do
16+
def call(_params) = value.dup
17+
end
18+
19+
Deferred = Data.define(:callable) do
20+
def call(_params) = callable.call
21+
end
22+
private_constant :Value, :Copy, :Deferred
23+
724
def initialize(attrs, options, required, scope, opts)
825
super
926
# !important, lazy call at runtime
1027
@default_call =
1128
if options.is_a?(Proc)
12-
options.arity.zero? ? proc { options.call } : options
29+
options.arity.zero? ? Deferred.new(options) : options
1330
elsif options.duplicable?
14-
proc { options.dup }
31+
Copy.new(options)
1532
else
16-
proc { options }
33+
Value.new(options)
1734
end
1835
end
1936

0 commit comments

Comments
 (0)