From 5ff49665265ba62de223df53e4020f3d0137e23c Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Thu, 13 Apr 2017 16:26:35 -0400 Subject: [PATCH] add json a=null support and still support a="null" related to #6016 --- awx/main/fields.py | 7 ++++++- awx/main/tests/unit/test_fields.py | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/awx/main/fields.py b/awx/main/fields.py index be70f47fd0..0cb8b0e1d4 100644 --- a/awx/main/fields.py +++ b/awx/main/fields.py @@ -49,6 +49,11 @@ class JSONField(upstream_JSONField): class JSONBField(upstream_JSONBField): + def get_prep_lookup(self, lookup_type, value): + if isinstance(value, basestring) and value == "null": + return 'null' + return super(JSONBField, self).get_prep_lookup(lookup_type, value) + def get_db_prep_value(self, value, connection, prepared=False): if connection.vendor == 'sqlite': # sqlite (which we use for tests) does not support jsonb; @@ -405,7 +410,7 @@ class DynamicFilterField(models.TextField): Note: we could have totally "ripped" them off earlier when we decided what type to convert the token to. ''' - if type(v) is unicode and v.startswith('"') and v.endswith('"'): + if type(v) is unicode and v.startswith('"') and v.endswith('"') and v != u'"null"': v = v[1:-1] if contains_count == 0: diff --git a/awx/main/tests/unit/test_fields.py b/awx/main/tests/unit/test_fields.py index fc20dfcb1d..468a1d9145 100644 --- a/awx/main/tests/unit/test_fields.py +++ b/awx/main/tests/unit/test_fields.py @@ -86,6 +86,14 @@ class TestDynamicFilterFieldFilterStringToQ(): q = DynamicFilterField.filter_string_to_q(filter_string) assert unicode(q) == unicode(q_expected) + @pytest.mark.parametrize("filter_string,q_expected", [ + ('a__b__c=null', Q(**{u"a__b__c": u"null"})), + ('a__b__c="null"', Q(**{u"a__b__c": u"\"null\""})), + ]) + def test_contains_query_generated_null(self, filter_string, q_expected): + q = DynamicFilterField.filter_string_to_q(filter_string) + assert unicode(q) == unicode(q_expected) + ''' #('"facts__quoted_val"="f\"oo"', 1),