From 625ac9d632659b798b0fc6a7d315444366d4d743 Mon Sep 17 00:00:00 2001 From: Andrii Nikolaienko Date: Sun, 24 Jul 2022 19:52:21 +0400 Subject: [PATCH] fixed issue with dictionary collection --- CHANGELOG.md | 5 ++- automapper/mapper.py | 2 +- pyproject.toml | 2 +- tests/test_automapper_dict_field.py | 47 +++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 tests/test_automapper_dict_field.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 7229274..4b18ec7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ +1.0.3 - 2022/07/24 +* Fixed issue with dictionary collection: https://github.com/anikolaienko/py-automapper/issues/4 + 1.0.2 - 2022/07/24 * Bug fix: pass parameters override in MappingWrapper.map -* Added support for mapping fields with different names +* Added support for mapping fields with different names: https://github.com/anikolaienko/py-automapper/issues/3 1.0.1 - 2022/01/05 * Bug fix diff --git a/automapper/mapper.py b/automapper/mapper.py index 3c64996..7e78f59 100644 --- a/automapper/mapper.py +++ b/automapper/mapper.py @@ -237,7 +237,7 @@ def _map_subobject( k: self._map_subobject( v, _visited_stack, skip_none_values=skip_none_values ) - for k, v in obj + for k, v in obj.items() } else: result = type(obj)( # type: ignore [call-arg] diff --git a/pyproject.toml b/pyproject.toml index 762f275..5ac5210 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "py-automapper" -version = "1.0.0" +version = "1.0.3" description = "Library for automatically mapping one object to another" authors = ["Andrii Nikolaienko "] license = "MIT" diff --git a/tests/test_automapper_dict_field.py b/tests/test_automapper_dict_field.py new file mode 100644 index 0000000..c63ec49 --- /dev/null +++ b/tests/test_automapper_dict_field.py @@ -0,0 +1,47 @@ +from copy import deepcopy +from typing import Any, Dict + +from automapper import mapper + + +class Candy: + def __init__(self, name: str, brand: str): + self.name = name + self.brand = brand + + +class Shop: + def __init__(self, products: Dict[str, Any], annual_income: int): + self.products: Dict[str, Any] = deepcopy(products) + self.annual_income = annual_income + + +class ShopPublicInfo: + def __init__(self, products: Dict[str, Any]): + self.products: Dict[str, Any] = deepcopy(products) + + +def test_map__with_dict_field(): + products = { + "magazines": ["Forbes", "Time", "The New Yorker"], + "candies": [ + Candy("Reese's cups", "The Hershey Company"), + Candy("Snickers", "Mars, Incorporated"), + ], + } + shop = Shop(products=products, annual_income=10000000) + + public_info = mapper.to(ShopPublicInfo).map(shop) + + assert public_info.products["magazines"] == shop.products["magazines"] + assert id(public_info.products["magazines"]) != id(shop.products["magazines"]) + + assert public_info.products["candies"] != shop.products["candies"] + assert public_info.products["candies"][0] != shop.products["candies"][0] + assert public_info.products["candies"][1] != shop.products["candies"][1] + + assert public_info.products["candies"][0].name == "Reese's cups" + assert public_info.products["candies"][0].brand == "The Hershey Company" + + assert public_info.products["candies"][1].name == "Snickers" + assert public_info.products["candies"][1].brand == "Mars, Incorporated"