#32213: QuerySet.values()/.values_list() on KeyTransforms return wrong values 
for
double-quoted strings on SQLite and Oracle
-------------------------------------+-------------------------------------
     Reporter:  Sage Abdullah        |                    Owner:  (none)
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  3.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  sqlite oracle json   |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by bcail):

 Here are some tests that show the issues with various strings in a
 JSONField for sqlite (they pass on postgresql):
 {{{
 +    def test_str_with_quotes(self):
 +        value = {"key": "test_value", "key2": '"value"'}
 +        obj = NullableJSONModel.objects.create(value=value)
 +
 +
 
self.assertSequenceEqual(NullableJSONModel.objects.filter(value__key='test_value').values("value__key2"),
 [{"value__key2": '"value"'}])
 +
 
self.assertSequenceEqual(NullableJSONModel.objects.filter(value__key='test_value').values_list("value__key2"),
 [('"value"',)])
 +
 +    def test_str_with_null_false_true(self):
 +        value = {"key": "test_value", "key2": "null", "key3": "false",
 "key4": "true"}
 +        obj = NullableJSONModel.objects.create(value=value)
 +
 +
 
self.assertSequenceEqual(NullableJSONModel.objects.filter(value__key='test_value').values_list("value__key2"),
 [('null',)])
 +
 
self.assertSequenceEqual(NullableJSONModel.objects.filter(value__key='test_value').values("value__key3"),
 [{"value__key3": 'false'}])
 +
 
self.assertSequenceEqual(NullableJSONModel.objects.filter(value__key='test_value').values("value__key4"),
 [{"value__key4": 'true'}])
 +
 +    def test_str_with_list_dict(self):
 +        value = {"key": "test_value", "key2": "[]", "key3": "{}"}
 +        obj = NullableJSONModel.objects.create(value=value)
 +
 +
 
self.assertSequenceEqual(NullableJSONModel.objects.filter(value__key='test_value').values_list("value__key2"),
 [("[]",)])
 +
 
self.assertSequenceEqual(NullableJSONModel.objects.filter(value__key='test_value').values("value__key3"),
 [{"value__key3": "{}"}])
 }}}

 This code change gets the str_with_quotes test passing, but not the other
 two:
 {{{
 diff --git a/django/db/models/fields/json.py
 b/django/db/models/fields/json.py
 index 7296fe42bc..6277ce9935 100644
 --- a/django/db/models/fields/json.py
 +++ b/django/db/models/fields/json.py
 @@ -82,8 +82,11 @@ class JSONField(CheckFieldDefaultMixin, Field):
              return value
          # Some backends (SQLite at least) extract non-string values in
 their
          # SQL datatypes.
 -        if isinstance(expression, KeyTransform) and not isinstance(value,
 str):
 -            return value
 +        if connection.vendor == "sqlite" and isinstance(expression,
 KeyTransform):
 +            if not isinstance(value, str):
 +                return value
 +            if value not in ['true', 'false', 'null'] and not
 value.startswith('[') and not value.startswith('{'):
 +                return value
          try:
              return json.loads(value, cls=self.decoder)
          except json.JSONDecodeError:
 }}}

 I think that once sqlite 3.38.0 is in widespread use, and ticket #33548 is
 implemented (using -> and ->> in sqlite), that may take care of this
 issue.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32213#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070183ce0a0168-4543c246-d19a-4c66-b82d-2feaeda60d70-000000%40eu-central-1.amazonses.com.

Reply via email to