Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#26672 closed Bug (fixed)

forms.HStoreField crashes on an empty JSON array

Reported by: Brad Melin Owned by: Brad Melin
Component: contrib.postgres Version: 1.9
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Problem: Submitting a JSON array ([]) into the default Django admin form for HStoreField (django.contrib.postgres.fields.HStoreField) throws an error.

There is some incorrect logic with the validation where if anything but a dict is passed to the to_python method, the field tries to json.load() the data and then iterate over it with value.items(). Lists don't implement this method, and so we an AttibuteError is raised.

To reproduce:

  • Add an HStoreField to a model, and register that model with Django Admin.
  • In the model admin edit view, insert a list into the textfield
  • Submit.

Traceback: https://dpaste.de/bjUh

Change History (12)

comment:1 Changed 3 years ago by Brad Melin

comment:2 Changed 3 years ago by Brad Melin

Owner: set to Brad Melin
Status: newassigned

comment:3 Changed 3 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

I suppose a ValidationError should be raised instead.

comment:4 Changed 3 years ago by Tim Graham

Summary: When adding a JSON array to the Admin HStoreField widget, Django raises an AttibuteErrorforms.HStoreField crashes on an empty JSON array

comment:5 Changed 3 years ago by Brad Melin

Has patch: set

comment:6 Changed 3 years ago by Simon Charette

Patch needs improvement: set
Severity: NormalRelease blocker

Marking as release blocker for 1.9 as this is a bug in a new feature with possible data loss on failed submission.

PR needs minor code adjustment and a release note.

comment:7 Changed 3 years ago by Brad Melin

Patch needs improvement: unset

comment:8 Changed 3 years ago by Brad Melin

Patch needs improvement: set

comment:9 Changed 3 years ago by Brad Melin

Patch needs improvement: unset

comment:10 Changed 3 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In f6517a5:

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

comment:11 Changed 3 years ago by Tim Graham <timograham@…>

In 95ed1514:

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

Backport of f6517a5335ccc4858ee540548a1bd162bec36c46 from master

comment:12 Changed 3 years ago by Tim Graham <timograham@…>

In 3eb3186:

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

Backport of f6517a5335ccc4858ee540548a1bd162bec36c46 from master

Note: See TracTickets for help on using tickets.
Back to Top