Skip to content

Commit

Permalink
Issue #284
Browse files Browse the repository at this point in the history
Add TypedpyDefault setting that determines of to fail deserialization if additional properties is false.
  • Loading branch information
loyada committed Feb 12, 2024
1 parent 561d12f commit 495e2ff
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
license="MIT",
long_description=long_description,
url="http://github.com/loyada/typedpy",
download_url="https://github.com/loyada/typedpy/archive/v2.27.4.tar.gz",
download_url="https://github.com/loyada/typedpy/archive/v2.27.5.tar.gz",
keywords=["testing", "type-safe", "strict", "schema", "validation"],
version="2.27.4",
version="2.27.5",
)

# coverage run --source=typedpy/ setup.py test
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ def fixture_additional_props_default_is_false():
Structure.set_additional_properties_default(True)


@pytest.fixture(name="fail_on_additional_props_in_deserialization")
def fixture_fail_on_additional_props_in_deserialization():
TypedPyDefaults.ignore_invalid_additional_properties_in_deserialization = False
yield
TypedPyDefaults.ignore_invalid_additional_properties_in_deserialization = True



@pytest.fixture(name="compact_serialization")
def fixture_compact_serialization():
Structure.set_compact_serialization_default(True)
Expand Down
22 changes: 21 additions & 1 deletion tests/test_deserialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -1503,7 +1503,18 @@ class Foo(Structure):
deserialize({"a": 5})


def test_additional_properties_turned_off_err(additional_props_default_is_false):
def test_additional_properties_turned_off_err_silent_ignore(additional_props_default_is_false):
class Foo(Structure):
a: int
b: int
_required = []

Deserializer(Foo).deserialize({"c": 1, "a": 2})


def test_additional_properties_turned_off_err_throw(
additional_props_default_is_false, fail_on_additional_props_in_deserialization
):
class Foo(Structure):
a: int
b: int
Expand All @@ -1517,6 +1528,7 @@ class Foo(Structure):
in str(excinfo.value)
)


@mark.skipif(sys.version_info < (3, 9), reason="requires python3.9 or higher")
def test_smart_compact_deserialization_is_turned_off_by_default():
class Foo(Structure):
Expand All @@ -1530,10 +1542,18 @@ class Foo(Structure):
in str(excinfo.value)
)


@mark.skipif(sys.version_info < (3, 9), reason="requires python3.9 or higher")
def test_smart_compact_deserialization_is_turned_on(compact_serialization):
class Foo(Structure):
a: list[int]
_additional_properties = False

assert Deserializer(Foo).deserialize([2, 3]) == Foo(a=[2, 3])


def test_float_deserialization():
class Foo(Structure):
f: float

assert Deserializer(Foo).deserialize({"f": 5}) == Foo(f=5.0)
9 changes: 7 additions & 2 deletions typedpy/fields/floats.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ def __set__(self, instance, value):
super().__set__(instance, converted)

def _validate(self, value):
super()._validate(value)
Number._validate_static(self, value)
converted = (
float(value)
if isinstance(value, int) and value is not True and value is not False
else value
)
super()._validate(converted)
Number._validate_static(self, converted)


class PositiveFloat(Float, Positive):
Expand Down
2 changes: 1 addition & 1 deletion typedpy/serialization/serialization_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def deserialize(
adjusted_keep_undefined = (
keep_undefined
if keep_undefined is not None or additional_props_allowed
else True
else not TypedPyDefaults.ignore_invalid_additional_properties_in_deserialization
)
return deserialize_structure(
self.target_class,
Expand Down
1 change: 1 addition & 0 deletions typedpy/structures/defaults.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class TypedPyDefaults:
additional_properties_default: bool = True
ignore_invalid_additional_properties_in_deserialization: bool = True
compact_serialization_default: bool = False
automatic_enum_conversion: bool = True
support_json_schema_v6: bool = False
Expand Down

0 comments on commit 495e2ff

Please sign in to comment.