#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)
follow-up: 2 comment:1 by , 10 years ago
| Triage Stage: | Unreviewed → Accepted | 
|---|
comment:2 by , 10 years ago
Replying to claudep:
What about not calling
dumpson 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
follow-up: 4 comment:3 by , 10 years ago
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 by , 10 years ago
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 by , 10 years ago
| Cc: | added | 
|---|
comment:6 by , 10 years ago
| Version: | 1.9a1 → 1.9 | 
|---|
comment:7 by , 10 years ago
| Cc: | added | 
|---|
comment:9 by , 10 years ago
| Cc: | added | 
|---|
by , 10 years ago
| Attachment: | 0001-Fix-issue-in-JSONField.patch added | 
|---|
This is how I fixed the issue when I ran across it.
comment:10 by , 10 years ago
@tbeadle On invalid input, json.loads() will produce a ValueError, how would that solve the issue?
comment:11 by , 10 years ago
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 by , 10 years ago
| Patch needs improvement: | unset | 
|---|---|
| Summary: | JSONField form field double encodes → JSONField form field double encodes invalid values | 
| Triage Stage: | Accepted → Ready for checkin | 
comment:17 by , 10 years ago
| 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 by , 10 years ago
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 by , 10 years ago
| Resolution: | → fixed | 
|---|---|
| Status: | new → closed | 
What about not calling
dumpson string values?