]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
🐛 Fix `exclude_defaults` not propagated to dict keys and values in `jsonable_encoder...
authorMuhammad Bin Gulzar <75139794+MBGrao@users.noreply.github.com>
Tue, 28 Jul 2026 12:28:18 +0000 (17:28 +0500)
committerGitHub <noreply@github.com>
Tue, 28 Jul 2026 12:28:18 +0000 (12:28 +0000)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
fastapi/encoders.py
tests/test_jsonable_encoder.py

index c9f882d2bae15511d157c017102d478f01846e66..e578768dac5b33556087b42bd6a2e0782a9d5379 100644 (file)
@@ -299,6 +299,7 @@ def jsonable_encoder(
                     key,
                     by_alias=by_alias,
                     exclude_unset=exclude_unset,
+                    exclude_defaults=exclude_defaults,
                     exclude_none=exclude_none,
                     custom_encoder=custom_encoder,
                     sqlalchemy_safe=sqlalchemy_safe,
@@ -307,6 +308,7 @@ def jsonable_encoder(
                     value,
                     by_alias=by_alias,
                     exclude_unset=exclude_unset,
+                    exclude_defaults=exclude_defaults,
                     exclude_none=exclude_none,
                     custom_encoder=custom_encoder,
                     sqlalchemy_safe=sqlalchemy_safe,
index 8f8bd3fcbbab68404286210f1a911a6f20fcce6f..96dc49a6836454f552b897f00c5c4c29342fe1a3 100644 (file)
@@ -202,6 +202,20 @@ def test_encode_model_with_default():
     }
 
 
+def test_encode_model_with_default_in_dict_and_list():
+    model = ModelWithDefault(foo="foo", bar="bar")
+    assert jsonable_encoder([model], exclude_defaults=True) == [{"foo": "foo"}]
+    assert jsonable_encoder({"key": model}, exclude_defaults=True) == {
+        "key": {"foo": "foo"}
+    }
+    assert jsonable_encoder({"key": [model]}, exclude_defaults=True) == {
+        "key": [{"foo": "foo"}]
+    }
+    assert jsonable_encoder({"key": model}) == {
+        "key": {"foo": "foo", "bar": "bar", "bla": "bla"}
+    }
+
+
 def test_custom_encoders():
     class safe_datetime(datetime):
         pass