]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Gracefully degrade for SQLite JSON receiving direct numeric value
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 29 Nov 2019 15:50:44 +0000 (10:50 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 29 Nov 2019 15:54:42 +0000 (10:54 -0500)
Fixed issue to workaround SQLite's behavior of assigning "numeric" affinity
to JSON datatypes, first described at :ref:`change_3850`, which returns
scalar numeric JSON values as a number and not as a string that can be JSON
deserialized.  The SQLite-specific JSON deserializer now gracefully
degrades for this case as an exception and bypasses deserialization for
single numeric values, as from a JSON perspective they are already
deserialized.

Also adds a combinatoric fixture for JSON single values within
the dialect-general test suite.

Fixes: #5014
Change-Id: Id38221dce1271fec527ca198b23908547b25d8a0
(cherry picked from commit bb338b91752f4f758edd9b2549a228e891596ae0)

doc/build/changelog/unreleased_13/5014.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/sqlite/base.py
lib/sqlalchemy/testing/suite/test_types.py

diff --git a/doc/build/changelog/unreleased_13/5014.rst b/doc/build/changelog/unreleased_13/5014.rst
new file mode 100644 (file)
index 0000000..2c371be
--- /dev/null
@@ -0,0 +1,13 @@
+.. change::
+    :tags: bug, sqlite
+    :tickets: 5014
+
+    Fixed issue to workaround SQLite's behavior of assigning "numeric" affinity
+    to JSON datatypes, first described at :ref:`change_3850`, which returns
+    scalar numeric JSON values as a number and not as a string that can be JSON
+    deserialized.  The SQLite-specific JSON deserializer now gracefully
+    degrades for this case as an exception and bypasses deserialization for
+    single numeric values, as from a JSON perspective they are already
+    deserialized.
+
+
index be8c856480335b1677c62b671cce7bf34d4ba23d..280ba18565bf5c520a710159825bf76a5f15daee 100644 (file)
@@ -570,6 +570,7 @@ When using the per-:class:`.Engine` execution option, note that
 """  # noqa
 
 import datetime
+import numbers
 import re
 
 from .json import JSON
@@ -599,6 +600,24 @@ from ...types import TIMESTAMP  # noqa
 from ...types import VARCHAR  # noqa
 
 
+class _SQliteJson(JSON):
+    def result_processor(self, dialect, coltype):
+        default_processor = super(_SQliteJson, self).result_processor(
+            dialect, coltype
+        )
+
+        def process(value):
+            try:
+                return default_processor(value)
+            except TypeError:
+                if isinstance(value, numbers.Number):
+                    return value
+                else:
+                    raise
+
+        return process
+
+
 class _DateTimeMixin(object):
     _reg = None
     _storage_format = None
@@ -899,7 +918,7 @@ class TIME(_DateTimeMixin, sqltypes.Time):
 colspecs = {
     sqltypes.Date: DATE,
     sqltypes.DateTime: DATETIME,
-    sqltypes.JSON: JSON,
+    sqltypes.JSON: _SQliteJson,
     sqltypes.JSON.JSONIndexType: JSONIndexType,
     sqltypes.JSON.JSONPathType: JSONPathType,
     sqltypes.Time: TIME,
index bf5b18d0e8e3ef70dde3b0fbcc2b428e6774aef1..2a5dad2d6c016519e3c10971de70cae9f8b92073 100644 (file)
@@ -827,6 +827,38 @@ class JSONTest(_LiteralRoundTripFixture, fixtures.TablesTest):
             # make sure we get a row even if value is None
             eq_(row, (value,))
 
+    @testing.combinations(
+        (True,),
+        (False,),
+        (None,),
+        (15,),
+        (0,),
+        (-1,),
+        (-1.0,),
+        (15.052,),
+        ("a string",),
+        (util.u("réve illé"),),
+        (util.u("réve🐍 illé"),),
+    )
+    def test_single_element_round_trip(self, element):
+        data_table = self.tables.data_table
+        data_element = element
+        with config.db.connect() as conn:
+            conn.execute(
+                data_table.insert(),
+                {
+                    "name": "row1",
+                    "data": data_element,
+                    "nulldata": data_element,
+                },
+            )
+
+            row = conn.execute(
+                select([data_table.c.data, data_table.c.nulldata])
+            ).first()
+
+            eq_(row, (data_element, data_element))
+
     def test_round_trip_custom_json(self):
         data_table = self.tables.data_table
         data_element = {"key1": "data1"}