#25532 closed Bug (fixed)
JSONField form field double encodes invalid values
Reported by: | David Szotten | Owned by: | |
---|---|---|---|
Component: | contrib.postgres | Version: | 1.9 |
Severity: | Normal | Keywords: | |
Cc: | me@…, tbeadle@…, xblitz@… | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
the JSONField form field double-encodes invalid json strings,
e.g. entering the string {"foo"}
and submitting the form, it comes back with an error, but the form field populated with the string "{\"foo\"}"
not sure how to fix, since prepare_value
doesn't know if we have valid json (and we, like postgres, but unlike the json spec consider bare strings as valid json)
Attachments (1)
Change History (23)
comment:1 follow-up: 2 Changed 8 years ago by
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 Changed 8 years ago by
Replying to claudep:
What about not calling
dumps
on string values?
because bare strings are allowed as valid json (e.g. "foo"
) this would instead cause problems for such values, which would get stripped of their quotes
comment:3 follow-up: 4 Changed 8 years ago by
I made a tentative patch here: https://github.com/django/django/compare/master...claudep:25532
If we would choose this way, we would have to provide a deprecation path for custom fields without kwargs for prepare_value
, even if it seems currently undocumented.
comment:4 Changed 8 years ago by
Replying to claudep:
Looks like it fixes the problem. Can't speak to whether this is the best solution; what's the next step here?
comment:5 Changed 8 years ago by
Cc: | me@… added |
---|
comment:6 Changed 7 years ago by
Version: | 1.9a1 → 1.9 |
---|
comment:7 Changed 7 years ago by
Cc: | tbeadle@… added |
---|
comment:9 Changed 7 years ago by
Cc: | xblitz@… added |
---|
Changed 7 years ago by
Attachment: | 0001-Fix-issue-in-JSONField.patch added |
---|
This is how I fixed the issue when I ran across it.
comment:10 Changed 7 years ago by
@tbeadle On invalid input, json.loads()
will produce a ValueError
, how would that solve the issue?
comment:11 Changed 7 years ago by
I see now, your patch is a fix when the form is redisplayed because of another invalid value, but not when the JSON input itself is wrong, right?
comment:14 Changed 7 years ago by
Patch needs improvement: | unset |
---|---|
Summary: | JSONField form field double encodes → JSONField form field double encodes invalid values |
Triage Stage: | Accepted → Ready for checkin |
comment:15 Changed 7 years ago by
Owner: | set to Claude Paroz <claude@…> |
---|---|
Resolution: | → fixed |
Status: | new → closed |
In db196195:
comment:17 Changed 7 years ago by
Has patch: | unset |
---|---|
Resolution: | fixed |
Status: | closed → new |
Triage Stage: | Ready for checkin → Accepted |
The new test_formfield_disabled
test is failing on the 1.9 branch with TypeError: the JSON object must be str, not 'list'
.
comment:18 Changed 7 years ago by
I knew that test was failing on 1.9 and it was not supposed to be committed for that reason. Sorry for the mess.
comment:20 Changed 7 years ago by
Resolution: | → fixed |
---|---|
Status: | new → closed |
What about not calling
dumps
on string values?