Skip to content

Commit fd861d2

Browse files
authored
Merge pull request #1329 from vgsantoniazzi/master
Use inclusive terminology
2 parents 5517f17 + ea8a3f9 commit fd861d2

File tree

4 files changed

+74
-28
lines changed

4 files changed

+74
-28
lines changed

lib/jsonapi/acts_as_resource_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def handle_exceptions(e)
273273
when ActionController::ParameterMissing
274274
errors = JSONAPI::Exceptions::ParameterMissing.new(e.param).errors
275275
else
276-
if JSONAPI.configuration.exception_class_whitelisted?(e)
276+
if JSONAPI.configuration.exception_class_allowed?(e)
277277
raise e
278278
else
279279
if self.class.server_error_callbacks
@@ -308,7 +308,7 @@ def safe_run_callback(callback, error)
308308
# caught that is not a JSONAPI::Exceptions::Error
309309
# Useful for additional logging or notification configuration that
310310
# would normally depend on rails catching and rendering an exception.
311-
# Ignores whitelist exceptions from config
311+
# Ignores allowlist exceptions from config
312312

313313
module ClassMethods
314314

lib/jsonapi/configuration.rb

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class Configuration
2828
:allow_transactions,
2929
:include_backtraces_in_errors,
3030
:include_application_backtraces_in_errors,
31-
:exception_class_whitelist,
32-
:whitelist_all_exceptions,
31+
:exception_class_allowlist,
32+
:allow_all_exceptions,
3333
:always_include_to_one_linkage_data,
3434
:always_include_to_many_linkage_data,
3535
:cache_formatters,
@@ -95,12 +95,12 @@ def initialize
9595
# raise a Pundit::NotAuthorizedError at some point during operations
9696
# processing. If you want to use Rails' `rescue_from` macro to
9797
# catch this error and render a 403 status code, you should add
98-
# the `Pundit::NotAuthorizedError` to the `exception_class_whitelist`.
99-
self.exception_class_whitelist = []
98+
# the `Pundit::NotAuthorizedError` to the `exception_class_allowlist`.
99+
self.exception_class_allowlist = []
100100

101-
# If enabled, will override configuration option `exception_class_whitelist`
102-
# and whitelist all exceptions.
103-
self.whitelist_all_exceptions = false
101+
# If enabled, will override configuration option `exception_class_allowlist`
102+
# and allow all exceptions.
103+
self.allow_all_exceptions = false
104104

105105
# Resource Linkage
106106
# Controls the serialization of resource linkage for non compound documents
@@ -219,9 +219,9 @@ def route_formatter
219219
return formatter
220220
end
221221

222-
def exception_class_whitelisted?(e)
223-
@whitelist_all_exceptions ||
224-
@exception_class_whitelist.flatten.any? { |k| e.class.ancestors.map(&:to_s).include?(k.to_s) }
222+
def exception_class_allowed?(e)
223+
@allow_all_exceptions ||
224+
@exception_class_allowlist.flatten.any? { |k| e.class.ancestors.map(&:to_s).include?(k.to_s) }
225225
end
226226

227227
def default_processor_klass=(default_processor_klass)
@@ -244,6 +244,16 @@ def allow_include=(allow_include)
244244
@default_allow_include_to_many = allow_include
245245
end
246246

247+
def whitelist_all_exceptions=(allow_all_exceptions)
248+
ActiveSupport::Deprecation.warn('`whitelist_all_exceptions` has been replaced by `allow_all_exceptions`')
249+
@allow_all_exceptions = allow_all_exceptions
250+
end
251+
252+
def exception_class_whitelist=(exception_class_allowlist)
253+
ActiveSupport::Deprecation.warn('`exception_class_whitelist` has been replaced by `exception_class_allowlist`')
254+
@exception_class_allowlist = exception_class_allowlist
255+
end
256+
247257
attr_writer :allow_sort, :allow_filter, :default_allow_include_to_one, :default_allow_include_to_many
248258

249259
attr_writer :default_paginator
@@ -270,9 +280,9 @@ def allow_include=(allow_include)
270280

271281
attr_writer :include_application_backtraces_in_errors
272282

273-
attr_writer :exception_class_whitelist
283+
attr_writer :exception_class_allowlist
274284

275-
attr_writer :whitelist_all_exceptions
285+
attr_writer :allow_all_exceptions
276286

277287
attr_writer :always_include_to_one_linkage_data
278288

test/controllers/controller_test.rb

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,40 @@ def test_accept_header_not_jsonapi
8181
assert_equal "All requests must use the '#{JSONAPI::MEDIA_TYPE}' Accept without media type parameters. This request specified '#{@request.headers['Accept']}'.", json_response['errors'][0]['detail']
8282
end
8383

84-
def test_exception_class_whitelist
85-
original_whitelist = JSONAPI.configuration.exception_class_whitelist.dup
84+
def test_exception_class_allowlist
85+
original_allowlist = JSONAPI.configuration.exception_class_allowlist.dup
8686
$PostProcessorRaisesErrors = true
8787
# test that the operations dispatcher rescues the error when it
88-
# has not been added to the exception_class_whitelist
88+
# has not been added to the exception_class_allowlist
8989
assert_cacheable_get :index
9090
assert_response 500
9191

9292
# test that the operations dispatcher does not rescue the error when it
93-
# has been added to the exception_class_whitelist
94-
JSONAPI.configuration.exception_class_whitelist << PostsController::SpecialError
93+
# has been added to the exception_class_allowlist
94+
JSONAPI.configuration.exception_class_allowlist << PostsController::SpecialError
9595
assert_cacheable_get :index
9696
assert_response 403
9797
ensure
9898
$PostProcessorRaisesErrors = false
99-
JSONAPI.configuration.exception_class_whitelist = original_whitelist
99+
JSONAPI.configuration.exception_class_allowlist = original_allowlist
100+
end
101+
102+
def test_allow_all_exceptions
103+
original_config = JSONAPI.configuration.allow_all_exceptions
104+
$PostProcessorRaisesErrors = true
105+
assert_cacheable_get :index
106+
assert_response 500
107+
108+
JSONAPI.configuration.allow_all_exceptions = true
109+
assert_cacheable_get :index
110+
assert_response 403
111+
ensure
112+
$PostProcessorRaisesErrors = false
113+
JSONAPI.configuration.allow_all_exceptions = original_config
100114
end
101115

102116
def test_whitelist_all_exceptions
103-
original_config = JSONAPI.configuration.whitelist_all_exceptions
117+
original_config = JSONAPI.configuration.allow_all_exceptions
104118
$PostProcessorRaisesErrors = true
105119
assert_cacheable_get :index
106120
assert_response 500
@@ -114,18 +128,18 @@ def test_whitelist_all_exceptions
114128
end
115129

116130
def test_exception_added_to_request_env
117-
original_config = JSONAPI.configuration.whitelist_all_exceptions
131+
original_config = JSONAPI.configuration.allow_all_exceptions
118132
$PostProcessorRaisesErrors = true
119133
refute @request.env['action_dispatch.exception']
120134
assert_cacheable_get :index
121135
assert @request.env['action_dispatch.exception']
122136

123-
JSONAPI.configuration.whitelist_all_exceptions = true
137+
JSONAPI.configuration.allow_all_exceptions = true
124138
assert_cacheable_get :index
125139
assert @request.env['action_dispatch.exception']
126140
ensure
127141
$PostProcessorRaisesErrors = false
128-
JSONAPI.configuration.whitelist_all_exceptions = original_config
142+
JSONAPI.configuration.allow_all_exceptions = original_config
129143
end
130144

131145
def test_exception_includes_backtrace_when_enabled
@@ -168,7 +182,7 @@ def test_exception_includes_application_backtrace_when_enabled
168182

169183
def test_on_server_error_block_callback_with_exception
170184
original_config = JSONAPI.configuration.dup
171-
JSONAPI.configuration.exception_class_whitelist = []
185+
JSONAPI.configuration.exception_class_allowlist = []
172186
$PostProcessorRaisesErrors = true
173187

174188
@controller.class.instance_variable_set(:@callback_message, "none")
@@ -189,7 +203,7 @@ def test_on_server_error_block_callback_with_exception
189203

190204
def test_on_server_error_method_callback_with_exception
191205
original_config = JSONAPI.configuration.dup
192-
JSONAPI.configuration.exception_class_whitelist = []
206+
JSONAPI.configuration.exception_class_allowlist = []
193207
$PostProcessorRaisesErrors = true
194208

195209
#ignores methods that don't exist
@@ -208,7 +222,7 @@ def test_on_server_error_method_callback_with_exception
208222

209223
def test_on_server_error_method_callback_with_exception_on_serialize
210224
original_config = JSONAPI.configuration.dup
211-
JSONAPI.configuration.exception_class_whitelist = []
225+
JSONAPI.configuration.exception_class_allowlist = []
212226
$PostSerializerRaisesErrors = true
213227

214228
#ignores methods that don't exist
@@ -4000,6 +4014,16 @@ def test_uncaught_error_in_controller_translated_to_internal_server_error
40004014
assert_match /Internal Server Error/, json_response['errors'][0]['detail']
40014015
end
40024016

4017+
def test_not_allowed_error_in_controller
4018+
original_config = JSONAPI.configuration.dup
4019+
JSONAPI.configuration.exception_class_allowlist = []
4020+
get :show, params: {id: '1'}
4021+
assert_response 500
4022+
assert_match /Internal Server Error/, json_response['errors'][0]['detail']
4023+
ensure
4024+
JSONAPI.configuration = original_config
4025+
end
4026+
40034027
def test_not_whitelisted_error_in_controller
40044028
original_config = JSONAPI.configuration.dup
40054029
JSONAPI.configuration.exception_class_whitelist = []
@@ -4010,6 +4034,18 @@ def test_not_whitelisted_error_in_controller
40104034
JSONAPI.configuration = original_config
40114035
end
40124036

4037+
def test_allowed_error_in_controller
4038+
original_config = JSONAPI.configuration.dup
4039+
$PostProcessorRaisesErrors = true
4040+
JSONAPI.configuration.exception_class_allowlist = [PostsController::SubSpecialError]
4041+
assert_raises PostsController::SubSpecialError do
4042+
assert_cacheable_get :show, params: {id: '1'}
4043+
end
4044+
ensure
4045+
JSONAPI.configuration = original_config
4046+
$PostProcessorRaisesErrors = false
4047+
end
4048+
40134049
def test_whitelisted_error_in_controller
40144050
original_config = JSONAPI.configuration.dup
40154051
$PostProcessorRaisesErrors = true

test/fixtures/active_record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ class SpecialError < StandardError; end
917917
class SubSpecialError < PostsController::SpecialError; end
918918
class SerializeError < StandardError; end
919919

920-
# This is used to test that classes that are whitelisted are reraised by
920+
# This is used to test that classes that are allowed are reraised by
921921
# the operations dispatcher.
922922
rescue_from PostsController::SpecialError do
923923
head :forbidden

0 commit comments

Comments
 (0)