Skip to content

Commit c86750c

Browse files
committed
Merge pull request rails#37435 from abhaynikam/37428-fix-filter-attributes-for-json-data-type
Closes rails#37435.
2 parents 5f596e2 + 339d1cd commit c86750c

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

activerecord/test/cases/json_shared_test_cases.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "support/schema_dumping_helper"
4+
require "pp"
45

56
module JSONSharedTestCases
67
include SchemaDumpingHelper
@@ -249,6 +250,25 @@ def test_json_with_serialized_attributes
249250
assert_equal({ "three" => "four" }, record.reload.settings.to_hash)
250251
end
251252

253+
class JsonDataTypeWithFilter < ActiveRecord::Base
254+
self.table_name = "json_data_type"
255+
256+
attribute :payload, :json
257+
258+
def self.filter_attributes
259+
# Rails.application.config.filter_parameters += [:password]
260+
super + [:password]
261+
end
262+
end
263+
264+
def test_pretty_print
265+
x = JsonDataTypeWithFilter.create!(payload: {})
266+
x.payload[11] = "foo"
267+
io = StringIO.new
268+
PP.pp(x, io)
269+
assert io.string
270+
end
271+
252272
private
253273
def klass
254274
JsonDataType

activesupport/lib/active_support/parameter_filter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def call(params, parents = [], original_params = params)
107107

108108
def value_for_key(key, value, parents = [], original_params = nil)
109109
parents.push(key) if deep_regexps
110-
if regexps.any? { |r| r.match?(key) }
110+
if regexps.any? { |r| r.match?(key.to_s) }
111111
value = @mask
112112
elsif deep_regexps && (joined = parents.join(".")) && deep_regexps.any? { |r| r.match?(joined) }
113113
value = @mask

activesupport/test/parameter_filter_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,16 @@ class ParameterFilterTest < ActiveSupport::TestCase
109109
assert_equal mask, parameter_filter.filter_param("barbar", "secret vlaue")
110110
assert_equal "non secret value", parameter_filter.filter_param("baz", "non secret value")
111111
end
112+
113+
test "process parameter filter with hash having integer keys" do
114+
test_hashes = [
115+
[{ 13 => "bar" }, { 13 => "[FILTERED]" }, %w'13'],
116+
[{ 20 => "bar" }, { 20 => "bar" }, %w'13'],
117+
]
118+
119+
test_hashes.each do |before_filter, after_filter, filter_words|
120+
parameter_filter = ActiveSupport::ParameterFilter.new(filter_words)
121+
assert_equal after_filter, parameter_filter.filter(before_filter)
122+
end
123+
end
112124
end

0 commit comments

Comments
 (0)