From 968b9af9b7f7e49af17fc82de5a2de9a91b86e44 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 14 Aug 2019 15:25:35 +0200 Subject: [PATCH] [2.1.x] Fixed #30672 -- Fixed crash of JSONField/HStoreField key transforms on expressions with params. Regression in 4f5b58f5cd3c57fee9972ab074f8dc6895d8f387. Thanks Florian Apolloner for the report and helping with tests. Backport of 1f8382d34d54061eddc41df6994e20ee38c60907 from master. --- django/contrib/postgres/fields/hstore.py | 2 +- django/contrib/postgres/fields/jsonb.py | 2 +- docs/releases/1.11.24.txt | 15 +++++++++++++++ docs/releases/2.1.12.txt | 15 +++++++++++++++ docs/releases/index.txt | 2 ++ tests/postgres_tests/test_hstore.py | 9 +++++++++ tests/postgres_tests/test_json.py | 22 +++++++++++++++++++++- 7 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 docs/releases/1.11.24.txt create mode 100644 docs/releases/2.1.12.txt diff --git a/django/contrib/postgres/fields/hstore.py b/django/contrib/postgres/fields/hstore.py index 8d6cd6c812..33a741b0c8 100644 --- a/django/contrib/postgres/fields/hstore.py +++ b/django/contrib/postgres/fields/hstore.py @@ -86,7 +86,7 @@ class KeyTransform(Transform): def as_sql(self, compiler, connection): lhs, params = compiler.compile(self.lhs) - return '(%s -> %%s)' % lhs, [self.key_name] + params + return '(%s -> %%s)' % lhs, params + [self.key_name] class KeyTransformFactory: diff --git a/django/contrib/postgres/fields/jsonb.py b/django/contrib/postgres/fields/jsonb.py index be98ff2d48..be93b11d9f 100644 --- a/django/contrib/postgres/fields/jsonb.py +++ b/django/contrib/postgres/fields/jsonb.py @@ -112,7 +112,7 @@ class KeyTransform(Transform): lookup = int(self.key_name) except ValueError: lookup = self.key_name - return '(%s %s %%s)' % (lhs, self.operator), [lookup] + params + return '(%s %s %%s)' % (lhs, self.operator), params + [lookup] class KeyTextTransform(KeyTransform): diff --git a/docs/releases/1.11.24.txt b/docs/releases/1.11.24.txt new file mode 100644 index 0000000000..3013c866d0 --- /dev/null +++ b/docs/releases/1.11.24.txt @@ -0,0 +1,15 @@ +============================ +Django 1.11.24 release notes +============================ + +*Expected September 2, 2019* + +Django 1.11.24 fixes a regression in 1.11.23. + +Bugfixes +======== + +* Fixed crash of ``KeyTransform()`` for + :class:`~django.contrib.postgres.fields.JSONField` and + :class:`~django.contrib.postgres.fields.HStoreField` when using on + expressions with params (:ticket:`30672`). diff --git a/docs/releases/2.1.12.txt b/docs/releases/2.1.12.txt new file mode 100644 index 0000000000..696a6c4890 --- /dev/null +++ b/docs/releases/2.1.12.txt @@ -0,0 +1,15 @@ +=========================== +Django 2.1.12 release notes +=========================== + +*Expected September 2, 2019* + +Django 2.1.12 fixes a regression in 2.1.11. + +Bugfixes +======== + +* Fixed crash of ``KeyTransform()`` for + :class:`~django.contrib.postgres.fields.JSONField` and + :class:`~django.contrib.postgres.fields.HStoreField` when using on + expressions with params (:ticket:`30672`). diff --git a/docs/releases/index.txt b/docs/releases/index.txt index 4e6ac70f0f..68c624128e 100644 --- a/docs/releases/index.txt +++ b/docs/releases/index.txt @@ -25,6 +25,7 @@ versions of the documentation contain the release notes for any later releases. .. toctree:: :maxdepth: 1 + 2.1.12 2.1.11 2.1.10 2.1.9 @@ -63,6 +64,7 @@ versions of the documentation contain the release notes for any later releases. .. toctree:: :maxdepth: 1 + 1.11.24 1.11.23 1.11.22 1.11.21 diff --git a/tests/postgres_tests/test_hstore.py b/tests/postgres_tests/test_hstore.py index f322231a4e..65f0b3616b 100644 --- a/tests/postgres_tests/test_hstore.py +++ b/tests/postgres_tests/test_hstore.py @@ -2,6 +2,7 @@ import json from django.core import checks, exceptions, serializers from django.db import connection +from django.db.models.expressions import RawSQL from django.forms import Form from django.test.utils import ( CaptureQueriesContext, isolate_apps, modify_settings, @@ -13,6 +14,7 @@ from .models import HStoreModel, PostgreSQLModel try: from django.contrib.postgres import forms from django.contrib.postgres.fields import HStoreField + from django.contrib.postgres.fields.hstore import KeyTransform from django.contrib.postgres.validators import KeysValidator except ImportError: pass @@ -133,6 +135,13 @@ class TestQuerying(HStoreTestCase): self.objs[:2] ) + def test_key_transform_raw_expression(self): + expr = RawSQL('%s::hstore', ['x => b, y => c']) + self.assertSequenceEqual( + HStoreModel.objects.filter(field__a=KeyTransform('x', expr)), + self.objs[:2] + ) + def test_keys(self): self.assertSequenceEqual( HStoreModel.objects.filter(field__keys=['a']), diff --git a/tests/postgres_tests/test_json.py b/tests/postgres_tests/test_json.py index e58c7a2e7f..01215aa954 100644 --- a/tests/postgres_tests/test_json.py +++ b/tests/postgres_tests/test_json.py @@ -5,7 +5,9 @@ from decimal import Decimal from django.core import checks, exceptions, serializers from django.core.serializers.json import DjangoJSONEncoder from django.db import connection -from django.db.models import Q +from django.db.models import F, Q +from django.db.models.expressions import RawSQL +from django.db.models.functions import Cast from django.forms import CharField, Form, widgets from django.test.utils import CaptureQueriesContext, isolate_apps from django.utils.html import escape @@ -16,6 +18,7 @@ from .models import JSONModel, PostgreSQLModel try: from django.contrib.postgres import forms from django.contrib.postgres.fields import JSONField + from django.contrib.postgres.fields.jsonb import KeyTransform except ImportError: pass @@ -154,6 +157,23 @@ class TestQuerying(PostgreSQLTestCase): query = JSONModel.objects.filter(field__name__isnull=False).order_by('field__ord') self.assertSequenceEqual(query, [objs[4], objs[2], objs[3], objs[1], objs[0]]) + def test_key_transform_raw_expression(self): + expr = RawSQL('%s::jsonb', ['{"x": "bar"}']) + self.assertSequenceEqual( + JSONModel.objects.filter(field__foo=KeyTransform('x', expr)), + [self.objs[-1]], + ) + + def test_key_transform_expression(self): + self.assertSequenceEqual( + JSONModel.objects.filter(field__d__0__isnull=False).annotate( + key=KeyTransform('d', 'field'), + chain=KeyTransform('0', 'key'), + expr=KeyTransform('0', Cast('key', JSONField())), + ).filter(chain=F('expr')), + [self.objs[8]], + ) + def test_deep_values(self): query = JSONModel.objects.values_list('field__k__l') self.assertSequenceEqual(