From 21ae0f5cabb2796166d7085748e37dedc52ecbf4 Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Sat, 21 Nov 2026 18:16:17 +0100 Subject: [PATCH] Build param defaults and custom type coercions from frozen objects 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 --- CHANGELOG.md | 1 + .../validations/types/custom_type_coercer.rb | 68 ++++++++++--------- .../validators/default_validator.rb | 23 ++++++- 3 files changed, 58 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c9d05392..e5ea0c76e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ * [#3092](https://github.com/ruby-grape/grape/pull/3092): Skip posting empty Danger report comments - [@dblock](https://github.com/dblock). * [#3095](https://github.com/ruby-grape/grape/pull/3095): Freeze Grape's constants all the way down - [@ericproulx](https://github.com/ericproulx). * [#3096](https://github.com/ruby-grape/grape/pull/3096): Define Grape's callable constants as modules rather than lambdas - [@ericproulx](https://github.com/ericproulx). +* [#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). * [#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). * Your contribution here. diff --git a/lib/grape/validations/types/custom_type_coercer.rb b/lib/grape/validations/types/custom_type_coercer.rb index 788d9e00e..f5bd9ccd2 100644 --- a/lib/grape/validations/types/custom_type_coercer.rb +++ b/lib/grape/validations/types/custom_type_coercer.rb @@ -37,7 +37,39 @@ class CustomTypeCoercer TYPE_CHECK_METHODS = %i[coerced? parsed?].freeze COLLECTION_TYPES = [Array, Set].freeze - private_constant :TYPE_CHECK_METHODS, :COLLECTION_TYPES + + # What a coercer calls to coerce and to check. Frozen objects rather + # than lambdas: a lambda carries the coercer as its self and the values + # it closes over as they are, so a compiled API holding one could not + # be frozen all the way down. + HashSymbolizer = Data.define(:coercion) do + def call(val) = coercion.call(val).deep_symbolize_keys + end + + # +dup+ before +map!+: the collection is whatever the user's coercion + # method returned — possibly frozen, possibly the input itself — so we + # must not mutate it in place. +dup+ also preserves the class, keeping a + # +Set+ a +Set+ (unlike +map+, which would return an +Array+). + CollectionSymbolizer = Data.define(:coercion) do + def call(val) = coercion.call(val).dup.map! { |item| item.is_a?(Hash) ? item.deep_symbolize_keys : item } + end + + InstanceCheck = Data.define(:type) do + def call(value) = value.is_a?(type) + end + + EnumerableCheck = Data.define(:type) do + def call(value) = value.is_a?(Enumerable) && value.all? { |val| matches?(type.first, val) } + + private + + def matches?(type, value) + return value.all? { |val| matches?(type.first, val) } if type.is_a?(Enumerable) && value.is_a?(Enumerable) + + !type.is_a?(Enumerable) && value.is_a?(type) + end + end + private_constant :TYPE_CHECK_METHODS, :COLLECTION_TYPES, :HashSymbolizer, :CollectionSymbolizer, :InstanceCheck, :EnumerableCheck # A new coercer for the given type specification # and coercion method. @@ -72,8 +104,8 @@ def coerced?(val) def build_coercion_method(type, method) coercion_method = infer_coercion_method(type, method) - return hash_symbolizer(coercion_method) if type == Hash - return collection_symbolizer(coercion_method) if COLLECTION_TYPES.include?(type) + return HashSymbolizer.new(coercion_method) if type == Hash + return CollectionSymbolizer.new(coercion_method) if COLLECTION_TYPES.include?(type) coercion_method end @@ -85,39 +117,13 @@ def infer_coercion_method(type, method) method.method(:parse) end - def hash_symbolizer(method) - ->(val) { method.call(val).deep_symbolize_keys } - end - - # +dup+ before +map!+: the collection is whatever the user's coercion - # method returned — possibly frozen, possibly the input itself — so we - # must not mutate it in place. +dup+ also preserves the class, keeping a - # +Set+ a +Set+ (unlike +map+, which would return an +Array+). - def collection_symbolizer(method) - ->(val) { method.call(val).dup.map! { |item| symbolize_if_hash(item) } } - end - - def symbolize_if_hash(item) - item.is_a?(Hash) ? item.deep_symbolize_keys : item - end - def infer_type_check(type) method_name = TYPE_CHECK_METHODS.detect { |m| type.respond_to?(m) } return type.method(method_name) if method_name return type if type.respond_to?(:call) - return enumerable_type_check(type) if type.is_a?(Enumerable) - - ->(value) { value.is_a? type } - end - - def enumerable_type_check(type) - ->(value) { value.is_a?(Enumerable) && value.all? { |val| recursive_type_check(type.first, val) } } - end - - def recursive_type_check(type, value) - return value.all? { |val| recursive_type_check(type.first, val) } if type.is_a?(Enumerable) && value.is_a?(Enumerable) + return EnumerableCheck.new(type) if type.is_a?(Enumerable) - !type.is_a?(Enumerable) && value.is_a?(type) + InstanceCheck.new(type) end end end diff --git a/lib/grape/validations/validators/default_validator.rb b/lib/grape/validations/validators/default_validator.rb index 6625adc4e..eb8d2f695 100644 --- a/lib/grape/validations/validators/default_validator.rb +++ b/lib/grape/validations/validators/default_validator.rb @@ -4,16 +4,33 @@ module Grape module Validations module Validators class DefaultValidator < Base + # What answers a missing param's default. Frozen objects rather than + # procs: a proc carries the validator as its self and the values it + # closes over as they are, so a compiled API holding one could not be + # frozen all the way down. + Value = Data.define(:value) do + def call(_params) = value + end + + Copy = Data.define(:value) do + def call(_params) = value.dup + end + + Deferred = Data.define(:callable) do + def call(_params) = callable.call + end + private_constant :Value, :Copy, :Deferred + def initialize(attrs, options, required, scope, opts) super # !important, lazy call at runtime @default_call = if options.is_a?(Proc) - options.arity.zero? ? proc { options.call } : options + options.arity.zero? ? Deferred.new(options) : options elsif options.duplicable? - proc { options.dup } + Copy.new(options) else - proc { options } + Value.new(options) end end