Skip to content

Commit d3446bf

Browse files
lukemeliaclaude
andcommitted
Add Rails 8 patches: routing_ext for 8.1+, whitelist→allowlist rename
- Add Rails 8.1+ routing support in routing_ext.rb where @scope no longer supports []= and Resource.new/SingletonResource.new signatures changed - Rename exception_class_whitelist → exception_class_allowlist and whitelist_all_exceptions → allow_all_exceptions per upstream convention - Add deprecated wrapper methods for old names with deprecation warnings - Update callers in acts_as_resource_controller.rb and operation_dispatcher.rb Cherry-picked from cerebris/jsonapi-resources#1480 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a7e6dc9 commit d3446bf

File tree

4 files changed

+63
-21
lines changed

4 files changed

+63
-21
lines changed

lib/jsonapi/acts_as_resource_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def handle_exceptions(e)
264264
when JSONAPI::Exceptions::Error
265265
render_errors(e.errors)
266266
else
267-
if JSONAPI.configuration.exception_class_whitelisted?(e)
267+
if JSONAPI.configuration.exception_class_allowed?(e)
268268
fail e
269269
else
270270
(self.class.server_error_callbacks || []).each { |callback|

lib/jsonapi/configuration.rb

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class Configuration
2424
:top_level_meta_page_count_key,
2525
:allow_transactions,
2626
:include_backtraces_in_errors,
27-
:exception_class_whitelist,
28-
:whitelist_all_exceptions,
27+
:exception_class_allowlist,
28+
:allow_all_exceptions,
2929
:always_include_to_one_linkage_data,
3030
:always_include_to_many_linkage_data,
3131
:cache_formatters,
@@ -83,12 +83,12 @@ def initialize
8383
# raise a Pundit::NotAuthorizedError at some point during operations
8484
# processing. If you want to use Rails' `rescue_from` macro to
8585
# catch this error and render a 403 status code, you should add
86-
# the `Pundit::NotAuthorizedError` to the `exception_class_whitelist`.
87-
self.exception_class_whitelist = []
86+
# the `Pundit::NotAuthorizedError` to the `exception_class_allowlist`.
87+
self.exception_class_allowlist = []
8888

89-
# If enabled, will override configuration option `exception_class_whitelist`
90-
# and whitelist all exceptions.
91-
self.whitelist_all_exceptions = false
89+
# If enabled, will override configuration option `exception_class_allowlist`
90+
# and allow all exceptions.
91+
self.allow_all_exceptions = false
9292

9393
# Resource Linkage
9494
# Controls the serialization of resource linkage for non compound documents
@@ -202,9 +202,9 @@ def route_formatter
202202
return formatter
203203
end
204204

205-
def exception_class_whitelisted?(e)
206-
@whitelist_all_exceptions ||
207-
@exception_class_whitelist.flatten.any? { |k| e.class.ancestors.map(&:to_s).include?(k.to_s) }
205+
def exception_class_allowed?(e)
206+
@allow_all_exceptions ||
207+
@exception_class_allowlist.flatten.any? { |k| e.class.ancestors.map(&:to_s).include?(k.to_s) }
208208
end
209209

210210
def default_processor_klass=(default_processor_klass)
@@ -235,9 +235,27 @@ def default_processor_klass=(default_processor_klass)
235235

236236
attr_writer :include_backtraces_in_errors
237237

238-
attr_writer :exception_class_whitelist
238+
attr_writer :exception_class_allowlist
239+
240+
attr_writer :allow_all_exceptions
241+
242+
# Deprecated: use exception_class_allowlist= instead
243+
def exception_class_whitelist=(val)
244+
JSONAPI.warn_deprecated('`exception_class_whitelist` has been replaced by `exception_class_allowlist`')
245+
self.exception_class_allowlist = val
246+
end
247+
248+
# Deprecated: use allow_all_exceptions= instead
249+
def whitelist_all_exceptions=(val)
250+
JSONAPI.warn_deprecated('`whitelist_all_exceptions` has been replaced by `allow_all_exceptions`')
251+
self.allow_all_exceptions = val
252+
end
239253

240-
attr_writer :whitelist_all_exceptions
254+
# Deprecated: use exception_class_allowed? instead
255+
def exception_class_whitelisted?(e)
256+
JSONAPI.warn_deprecated('`exception_class_whitelisted?` has been replaced by `exception_class_allowed?`')
257+
exception_class_allowed?(e)
258+
end
241259

242260
attr_writer :always_include_to_one_linkage_data
243261

lib/jsonapi/operation_dispatcher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def process_operation(operation)
6262
def with_default_handling(&block)
6363
block.yield
6464
rescue => e
65-
if JSONAPI.configuration.exception_class_whitelisted?(e)
65+
if JSONAPI.configuration.exception_class_allowed?(e)
6666
raise e
6767
else
6868
@server_error_callbacks.each { |callback|

lib/jsonapi/routing_ext.rb

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,30 @@ def jsonapi_resource(*resources, &_block)
4646

4747
resource @resource_type, options do
4848
# :nocov:
49-
if @scope.respond_to? :[]=
49+
if @scope.respond_to?(:[]=)
5050
# Rails 4
5151
@scope[:jsonapi_resource] = @resource_type
5252

53+
if block_given?
54+
yield
55+
else
56+
jsonapi_relationships
57+
end
58+
elsif Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR >= 1
59+
# Rails 8.1+
60+
# Rails 8.1 changed Scope to not support []= and Resource.new signature
61+
# Use instance variable to track resource type
62+
@jsonapi_resource_type = @resource_type
5363
if block_given?
5464
yield
5565
else
5666
jsonapi_relationships
5767
end
5868
else
59-
# Rails 5
60-
jsonapi_resource_scope(SingletonResource.new(@resource_type, api_only?, @scope[:shallow], options), @resource_type) do
69+
# Rails 5-8.0
70+
resource_arg = SingletonResource.new(@resource_type, api_only?, @scope[:shallow], options)
71+
72+
jsonapi_resource_scope(resource_arg, @resource_type) do
6173
if block_given?
6274
yield
6375
else
@@ -121,17 +133,29 @@ def jsonapi_resources(*resources, &_block)
121133

122134
resources @resource_type, options do
123135
# :nocov:
124-
if @scope.respond_to? :[]=
136+
if @scope.respond_to?(:[]=)
125137
# Rails 4
126138
@scope[:jsonapi_resource] = @resource_type
127139
if block_given?
128140
yield
129141
else
130142
jsonapi_relationships
131143
end
144+
elsif Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR >= 1
145+
# Rails 8.1+
146+
# Rails 8.1 changed Scope to not support []= and Resource.new signature
147+
# Use instance variable to track resource type
148+
@jsonapi_resource_type = @resource_type
149+
if block_given?
150+
yield
151+
else
152+
jsonapi_relationships
153+
end
132154
else
133-
# Rails 5
134-
jsonapi_resource_scope(Resource.new(@resource_type, api_only?, @scope[:shallow], options), @resource_type) do
155+
# Rails 5-8.0
156+
resource_arg = Resource.new(@resource_type, api_only?, @scope[:shallow], options)
157+
158+
jsonapi_resource_scope(resource_arg, @resource_type) do
135159
if block_given?
136160
yield
137161
else
@@ -277,7 +301,7 @@ def jsonapi_resource_scope(resource, resource_type) #:nodoc:
277301
private
278302

279303
def resource_type_with_module_prefix(resource = nil)
280-
resource_name = resource || @scope[:jsonapi_resource]
304+
resource_name = resource || @scope[:jsonapi_resource] || @jsonapi_resource_type
281305
[@scope[:module], resource_name].compact.collect(&:to_s).join('/')
282306
end
283307
end

0 commit comments

Comments
 (0)