From 7eef4daad449d29cc037173aa58de40448112116 Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Sat, 21 Nov 2026 18:16:17 +0100 Subject: [PATCH] Freeze Grape's constants all the way down A non-main Ractor reads a constant only when its value is frozen all the way down. SPECIAL_JSON, Types::SPECIAL and Types::GROUPS held Array[JSON], an unfrozen Array; MESSAGE_STEPS held unfrozen Strings; DELETED_COOKIES_ATTRS held a Time. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + lib/grape/cookies.rb | 2 +- lib/grape/exceptions/base.rb | 2 +- lib/grape/validations/params_scope.rb | 2 +- lib/grape/validations/types.rb | 4 ++-- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ab3c6211..c626c9ba4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * [#3088](https://github.com/ruby-grape/grape/pull/3088): Pin workflow actions to commits and grant their tokens only what they need - [@ericproulx](https://github.com/ericproulx). * [#3090](https://github.com/ruby-grape/grape/pull/3090): Call Danger's workflows from danger/danger-pr-comment, where they moved - [@ericproulx](https://github.com/ericproulx). * [#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). * Your contribution here. #### Fixes diff --git a/lib/grape/cookies.rb b/lib/grape/cookies.rb index 37be3828c..5057268a9 100644 --- a/lib/grape/cookies.rb +++ b/lib/grape/cookies.rb @@ -7,7 +7,7 @@ class Cookies DELETED_COOKIES_ATTRS = { max_age: '0', value: '', - expires: Time.at(0) + expires: Time.at(0).freeze }.freeze def_delegators :cookies, :[], :each diff --git a/lib/grape/exceptions/base.rb b/lib/grape/exceptions/base.rb index 019c3d98e..398906289 100644 --- a/lib/grape/exceptions/base.rb +++ b/lib/grape/exceptions/base.rb @@ -5,7 +5,7 @@ module Exceptions class Base < StandardError include Grape::Util::Translation - MESSAGE_STEPS = %w[problem summary resolution].to_h { |s| [s.to_sym, s.capitalize] }.freeze + MESSAGE_STEPS = %w[problem summary resolution].to_h { |s| [s.to_sym, s.capitalize.freeze] }.freeze attr_reader :status, :headers diff --git a/lib/grape/validations/params_scope.rb b/lib/grape/validations/params_scope.rb index 02244ac4d..e12dc0f62 100644 --- a/lib/grape/validations/params_scope.rb +++ b/lib/grape/validations/params_scope.rb @@ -42,7 +42,7 @@ def params(params) # we maintain a list of them here and skip looking up validators for them. RESERVED_DOCUMENTATION_KEYWORDS = %i[as required param_type is_array format example].freeze - SPECIAL_JSON = [JSON, Array[JSON]].freeze + SPECIAL_JSON = [JSON, [JSON].freeze].freeze # What {#validated_params} returns for a scope that should not be # validated. A plain +false+ would be indistinguishable from params of diff --git a/lib/grape/validations/types.rb b/lib/grape/validations/types.rb index e2f94e32d..f01c08ce0 100644 --- a/lib/grape/validations/types.rb +++ b/lib/grape/validations/types.rb @@ -40,12 +40,12 @@ module Types SPECIAL = { ::JSON => Json, - Array[JSON] => JsonArray, + [JSON].freeze => JsonArray, ::File => File, Rack::Multipart::UploadedFile => File }.freeze - GROUPS = [Array, Hash, JSON, Array[JSON]].freeze + GROUPS = [Array, Hash, JSON, [JSON].freeze].freeze # Is the given class a primitive type as recognized by Grape? #