]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
🐛 Fix issues introduced by removing sqlalchemy safeguard in jsonable_encoder (#1987)
authorSebastián Ramírez <tiangolo@gmail.com>
Sat, 29 Aug 2020 12:21:00 +0000 (14:21 +0200)
committerGitHub <noreply@github.com>
Sat, 29 Aug 2020 12:21:00 +0000 (14:21 +0200)
fastapi/encoders.py

index 1c324b9156101cf95bb2610012f39493cc8a3eb8..1255b74977e3be1a184a381c2e63449428aad50d 100644 (file)
@@ -32,6 +32,7 @@ def jsonable_encoder(
     exclude_defaults: bool = False,
     exclude_none: bool = False,
     custom_encoder: dict = {},
+    sqlalchemy_safe: bool = True,
 ) -> Any:
     if include is not None and not isinstance(include, set):
         include = set(include)
@@ -56,6 +57,7 @@ def jsonable_encoder(
             exclude_none=exclude_none,
             exclude_defaults=exclude_defaults,
             custom_encoder=encoder,
+            sqlalchemy_safe=sqlalchemy_safe,
         )
     if isinstance(obj, Enum):
         return obj.value
@@ -66,8 +68,14 @@ def jsonable_encoder(
     if isinstance(obj, dict):
         encoded_dict = {}
         for key, value in obj.items():
-            if (value is not None or not exclude_none) and (
-                (include and key in include) or not exclude or key not in exclude
+            if (
+                (
+                    not sqlalchemy_safe
+                    or (not isinstance(key, str))
+                    or (not key.startswith("_sa"))
+                )
+                and (value is not None or not exclude_none)
+                and ((include and key in include) or not exclude or key not in exclude)
             ):
                 encoded_key = jsonable_encoder(
                     key,
@@ -75,6 +83,7 @@ def jsonable_encoder(
                     exclude_unset=exclude_unset,
                     exclude_none=exclude_none,
                     custom_encoder=custom_encoder,
+                    sqlalchemy_safe=sqlalchemy_safe,
                 )
                 encoded_value = jsonable_encoder(
                     value,
@@ -82,6 +91,7 @@ def jsonable_encoder(
                     exclude_unset=exclude_unset,
                     exclude_none=exclude_none,
                     custom_encoder=custom_encoder,
+                    sqlalchemy_safe=sqlalchemy_safe,
                 )
                 encoded_dict[encoded_key] = encoded_value
         return encoded_dict
@@ -98,6 +108,7 @@ def jsonable_encoder(
                     exclude_defaults=exclude_defaults,
                     exclude_none=exclude_none,
                     custom_encoder=custom_encoder,
+                    sqlalchemy_safe=sqlalchemy_safe,
                 )
             )
         return encoded_list
@@ -133,4 +144,5 @@ def jsonable_encoder(
         exclude_defaults=exclude_defaults,
         exclude_none=exclude_none,
         custom_encoder=custom_encoder,
+        sqlalchemy_safe=sqlalchemy_safe,
     )