[1.10.x] Fixed #26672 -- Fixed HStoreField to raise ValidationError instead of crashing on non-dict JSON input.

Backport of f6517a5335 from master
This commit is contained in:
Brad Melin 2016-05-29 14:17:07 +02:00 committed by Tim Graham
parent 5641a80d49
commit 3eb31867bb
4 changed files with 22 additions and 1 deletions

View File

@ -108,6 +108,7 @@ answer newbie questions, and generally made Django that much better:
Bojan Mihelac <bmihelac@mihelac.org> Bojan Mihelac <bmihelac@mihelac.org>
Bouke Haarsma <bouke@haarsma.eu> Bouke Haarsma <bouke@haarsma.eu>
Božidar Benko <bbenko@gmail.com> Božidar Benko <bbenko@gmail.com>
Brad Melin <melinbrad@gmail.com>
Brant Harris Brant Harris
Brendan Hayward <brendanhayward85@gmail.com> Brendan Hayward <brendanhayward85@gmail.com>
Brenton Simpson <http://theillustratedlife.com> Brenton Simpson <http://theillustratedlife.com>

View File

@ -9,10 +9,13 @@ __all__ = ['HStoreField']
class HStoreField(forms.CharField): class HStoreField(forms.CharField):
"""A field for HStore data which accepts JSON input.""" """
A field for HStore data which accepts dictionary JSON input.
"""
widget = forms.Textarea widget = forms.Textarea
default_error_messages = { default_error_messages = {
'invalid_json': _('Could not load JSON data.'), 'invalid_json': _('Could not load JSON data.'),
'invalid_format': _('Input must be a JSON dictionary.'),
} }
def prepare_value(self, value): def prepare_value(self, value):
@ -31,6 +34,13 @@ class HStoreField(forms.CharField):
self.error_messages['invalid_json'], self.error_messages['invalid_json'],
code='invalid_json', code='invalid_json',
) )
if not isinstance(value, dict):
raise ValidationError(
self.error_messages['invalid_format'],
code='invalid_format',
)
# Cast everything to strings for ease. # Cast everything to strings for ease.
for key, val in value.items(): for key, val in value.items():
value[key] = six.text_type(val) value[key] = six.text_type(val)

View File

@ -20,3 +20,6 @@ Bugfixes
* Fixed ``on_commit`` callbacks execution order when callbacks make * Fixed ``on_commit`` callbacks execution order when callbacks make
transactions (:ticket:`26627`). transactions (:ticket:`26627`).
* Fixed ``HStoreField`` to raise a ``ValidationError`` instead of crashing on
non-dictionary JSON input (:ticket:`26672`).

View File

@ -208,6 +208,13 @@ class TestFormField(PostgreSQLTestCase):
self.assertEqual(cm.exception.messages[0], 'Could not load JSON data.') self.assertEqual(cm.exception.messages[0], 'Could not load JSON data.')
self.assertEqual(cm.exception.code, 'invalid_json') self.assertEqual(cm.exception.code, 'invalid_json')
def test_non_dict_json(self):
field = forms.HStoreField()
msg = 'Input must be a JSON dictionary.'
with self.assertRaisesMessage(exceptions.ValidationError, msg) as cm:
field.clean('["a", "b", 1]')
self.assertEqual(cm.exception.code, 'invalid_format')
def test_not_string_values(self): def test_not_string_values(self):
field = forms.HStoreField() field = forms.HStoreField()
value = field.clean('{"a": 1}') value = field.clean('{"a": 1}')