Skip to content

Commit

Permalink
Merge pull request #166 from honeybadger-io/ignore-tuple-keys
Browse files Browse the repository at this point in the history
Fix filtering of dictionaries with tuple keys
  • Loading branch information
Kelvin4664 authored Apr 7, 2024
2 parents 0c7d51e + 435d3c6 commit 13e1e1d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions honeybadger/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ def test_filter_dict_with_nested_dict():
expected = {'foo': 'bar', 'bar': 'baz', 'nested': {'password': '[FILTERED]'}}
filter_keys = ['password']
assert filter_dict(data, filter_keys) == expected

def test_ignores_dict_with_tuple_key():
data = {('foo', 'bar'): 'baz', "key": "value"}
expected = {"key": "value"}
filter_keys = ['foo']
assert filter_dict(data, filter_keys) == expected
12 changes: 11 additions & 1 deletion honeybadger/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import json


Expand All @@ -14,7 +15,16 @@ def filter_dict(data, filter_keys):
if type(data) != dict:
return data

for key, value in data.items():
data_copy = copy.deepcopy(data)

for key, value in data_copy.items():
# While tuples are considered valid dictionary keys,
# they are not json serializable
# so we remove them from the dictionary
if type(key) == tuple:
data.pop(key)
continue

if key in filter_keys:
data[key] = "[FILTERED]"

Expand Down

0 comments on commit 13e1e1d

Please sign in to comment.