Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Pydantic Unions #1231

Merged
merged 4 commits into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Release type: patch

Fixes a bug in the Pydantic conversion code around `Union` values.
11 changes: 11 additions & 0 deletions strawberry/experimental/pydantic/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from strawberry.field import StrawberryField
from strawberry.scalars import is_scalar
from strawberry.type import StrawberryList, StrawberryOptional, StrawberryType
from strawberry.union import StrawberryUnion


def _convert_from_pydantic_to_strawberry_type(
Expand All @@ -16,6 +17,16 @@ def _convert_from_pydantic_to_strawberry_type(
return _convert_from_pydantic_to_strawberry_type(
type_.of_type, data_from_model=data, extra=extra
)
if isinstance(type_, StrawberryUnion):
for option_type in type_.types:
if hasattr(option_type, "_pydantic_type"):
source_type = option_type._pydantic_type # type: ignore
else:
source_type = cast(type, option_type)
if isinstance(data, source_type):
return _convert_from_pydantic_to_strawberry_type(
option_type, data_from_model=data, extra=extra
)
if isinstance(type_, StrawberryList):
items = []
for index, item in enumerate(data):
Expand Down
1 change: 1 addition & 0 deletions strawberry/experimental/pydantic/object_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def wrap(cls):
)

model._strawberry_type = cls # type: ignore
cls._pydantic_type = model # type: ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a patter we want to move away from :)

I think we can keep this for now though, we still need to do some refactoring anyway ^^


def from_pydantic(instance: Any, extra: Dict[str, Any] = None) -> Any:
return convert_pydantic_model_to_strawberry_class(
Expand Down
116 changes: 115 additions & 1 deletion tests/experimental/pydantic/test_conversion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional
from typing import List, Optional, Union

import pydantic

Expand Down Expand Up @@ -227,6 +227,120 @@ class User:
]


def test_can_convert_pydantic_type_to_strawberry_with_union():
class BranchA(pydantic.BaseModel):
field_a: str

class BranchB(pydantic.BaseModel):
field_b: int

class User(pydantic.BaseModel):
age: int
union_field: Union[BranchA, BranchB]

@strawberry.experimental.pydantic.type(BranchA, fields=["field_a"])
class BranchAType:
pass

@strawberry.experimental.pydantic.type(BranchB, fields=["field_b"])
class BranchBType:
pass

@strawberry.experimental.pydantic.type(User, fields=["age", "union_field"])
class UserType:
pass

origin_user = User(age=1, union_field=BranchA(field_a="abc"))
user = UserType.from_pydantic(origin_user)

assert user.age == 1
assert isinstance(user.union_field, BranchAType)
assert user.union_field.field_a == "abc"

origin_user = User(age=1, union_field=BranchB(field_b=123))
user = UserType.from_pydantic(origin_user)

assert user.age == 1
assert isinstance(user.union_field, BranchBType)
assert user.union_field.field_b == 123


def test_can_convert_pydantic_type_to_strawberry_with_union_of_strawberry_types():
@strawberry.type
class BranchA:
field_a: str

@strawberry.type
class BranchB:
field_b: int

class User(pydantic.BaseModel):
age: int
union_field: Union[BranchA, BranchB]

@strawberry.experimental.pydantic.type(User, fields=["age", "union_field"])
class UserType:
pass

origin_user = User(age=1, union_field=BranchA(field_a="abc"))
user = UserType.from_pydantic(origin_user)

assert user.age == 1
assert isinstance(user.union_field, BranchA)
assert user.union_field.field_a == "abc"

origin_user = User(age=1, union_field=BranchB(field_b=123))
user = UserType.from_pydantic(origin_user)

assert user.age == 1
assert isinstance(user.union_field, BranchB)
assert user.union_field.field_b == 123


def test_can_convert_pydantic_type_to_strawberry_with_union_nullable():
class BranchA(pydantic.BaseModel):
field_a: str

class BranchB(pydantic.BaseModel):
field_b: int

class User(pydantic.BaseModel):
age: int
union_field: Union[None, BranchA, BranchB]

@strawberry.experimental.pydantic.type(BranchA, fields=["field_a"])
class BranchAType:
pass

@strawberry.experimental.pydantic.type(BranchB, fields=["field_b"])
class BranchBType:
pass

@strawberry.experimental.pydantic.type(User, fields=["age", "union_field"])
class UserType:
pass

origin_user = User(age=1, union_field=BranchA(field_a="abc"))
user = UserType.from_pydantic(origin_user)

assert user.age == 1
assert isinstance(user.union_field, BranchAType)
assert user.union_field.field_a == "abc"

origin_user = User(age=1, union_field=BranchB(field_b=123))
user = UserType.from_pydantic(origin_user)

assert user.age == 1
assert isinstance(user.union_field, BranchBType)
assert user.union_field.field_b == 123

origin_user = User(age=1, union_field=None)
user = UserType.from_pydantic(origin_user)

assert user.age == 1
assert user.union_field is None


def test_can_convert_pydantic_type_to_strawberry_with_additional_fields():
class UserModel(pydantic.BaseModel):
password: Optional[str]
Expand Down