Skip to content

Commit 14317e0

Browse files
committed
Described solution for issue #18
1 parent 6466da5 commit 14317e0

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from dataclasses import dataclass
2+
from typing import Optional
3+
4+
from automapper import mapper
5+
6+
7+
@dataclass
8+
class UserDomain:
9+
id: int
10+
name: str
11+
email: str
12+
13+
14+
@dataclass
15+
class TodoDomain:
16+
description: str
17+
user: UserDomain
18+
19+
20+
@dataclass
21+
class TodoModel:
22+
description: str
23+
user: UserDomain
24+
user_id: Optional[int] = None
25+
26+
27+
def test_map__implicit_mapping_of_child_obj_field_to_parent_obj_field_not_supported():
28+
user = UserDomain(id=123, name="carlo", email="mail_carlo")
29+
todo = TodoDomain(description="todo_carlo", user=user)
30+
31+
mapper.add(TodoDomain, TodoModel)
32+
todo_model: TodoModel = mapper.map(todo)
33+
34+
# Implicit field mapping between parent and child objects is not supported
35+
# TodoDomain.user.user_id should not map to TodoModel.user_id implicitly
36+
assert todo_model.user_id is None
37+
38+
# Workaround: use explicit mapping
39+
todo_model1: TodoModel = mapper.map(todo, fields_mapping={"user_id": todo.user.id})
40+
41+
assert todo_model1.user_id == 123

0 commit comments

Comments
 (0)