From 7ff7edde9dc0cf180339de2c5cfb75e36ad58dcb Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Tue, 19 Sep 2023 15:56:38 +0200 Subject: [PATCH] [FIX] extendable_fastapi: Coercion must be possible On the StrictExtendableBaseModel, disable strict mode to enable coercion from json by FastAPI --- extendable_fastapi/schemas.py | 3 --- .../tests/test_strict_extendable_base_model.py | 13 ++++--------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/extendable_fastapi/schemas.py b/extendable_fastapi/schemas.py index 6ad7a9b31..dfa7a1664 100644 --- a/extendable_fastapi/schemas.py +++ b/extendable_fastapi/schemas.py @@ -6,7 +6,6 @@ class StrictExtendableBaseModel( revalidate_instances="always", validate_assignment=True, extra="forbid", - strict=True, ): """ An ExtendableBaseModel with strict validation. @@ -24,6 +23,4 @@ class StrictExtendableBaseModel( (default is "never") * validate_assignment=True: revalidate the model when the data is changed (default is False) * extra="forbid": Forbid any extra attributes (default is "ignore") - * strict=True: raise an error if a value's type does not match the field's type - annotation (default is False; Pydantic attempts to coerce values to the correct type) """ diff --git a/extendable_fastapi/tests/test_strict_extendable_base_model.py b/extendable_fastapi/tests/test_strict_extendable_base_model.py index 650982f43..f05dfd79c 100644 --- a/extendable_fastapi/tests/test_strict_extendable_base_model.py +++ b/extendable_fastapi/tests/test_strict_extendable_base_model.py @@ -55,14 +55,9 @@ def test_StrictModel_extra_forbidden(self): with self.assertRaises(ValidationError): self.StrictModel(x=1, z=3, d=None) - def test_Model_strict_false(self): - # Coerce str->date is allowed - m = self.Model(x=1, d=None) + def test_StrictModel_strict_false(self): + # Coerce str->date is allowed to enable coercion from JSON + # by FastAPI + m = self.StrictModel(x=1, d=None) m.d = "2023-01-01" self.assertTrue(m.model_validate(m)) - - def test_StrictModel_strict_true(self): - # Coerce str->date is forbidden - m = self.StrictModel(x=1, d=None) - with self.assertRaises(ValidationError): - m.d = "2023-01-01"