From a43f4f5235d031b574e91abb19a9f23a4ff14717 Mon Sep 17 00:00:00 2001 From: ClaireGuerreGiordano <52660442+ClaireGuerreGiordano@users.noreply.github.com> Date: Wed, 25 Sep 2024 14:05:25 +0200 Subject: [PATCH] ABI serialization not symmetric #23 (#29) --- src/erc7730/model/context.py | 4 ++-- tests/src/erc7730/common/test_datamodel.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/erc7730/model/context.py b/src/erc7730/model/context.py index b0948da..6d5b9b0 100644 --- a/src/erc7730/model/context.py +++ b/src/erc7730/model/context.py @@ -82,8 +82,8 @@ class AbiJsonSchemaItem(BaseLibraryModel): outputs: Optional[list[AbiParameter]] stateMutability: Optional[StateMutability] = None type: Type - constant: Optional[bool] = None - payable: Optional[bool] = None + constant: bool | None = None + payable: bool | None = None class AbiJsonSchema(RootModel[list[AbiJsonSchemaItem]]): diff --git a/tests/src/erc7730/common/test_datamodel.py b/tests/src/erc7730/common/test_datamodel.py index 82a1aeb..33e32ee 100644 --- a/tests/src/erc7730/common/test_datamodel.py +++ b/tests/src/erc7730/common/test_datamodel.py @@ -1,6 +1,7 @@ from pathlib import Path from erc7730.common.json import read_json_with_includes from erc7730.common.pydantic import json_file_from_model +from erc7730.model.context import AbiJsonSchemaItem from erc7730.model.erc7730_descriptor import ERC7730Descriptor import pytest import glob @@ -26,3 +27,14 @@ def test_from_erc7730(file: str) -> None: pytest.fail(f"Invalid schema for serialized data from {file}: {ex}") print_diff(json_from_model, original_dict_with_includes) assert json_from_model == original_dict_with_includes + + +abi_item_json_str = '{"name":"approve","inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"outputs":[{"name":"","type":"bool"}],"type":"function"}' + + +def test_23_unset_attributes_must_not_be_serialized_as_set() -> None: + abi_item_json = json.loads(abi_item_json_str) + abi_item = AbiJsonSchemaItem.model_validate(abi_item_json, strict=False) + abi_item_json_str_deserialized = json_file_from_model(AbiJsonSchemaItem, abi_item) + print_diff(abi_item_json_str, abi_item_json_str_deserialized) + assert abi_item_json_str == abi_item_json_str_deserialized