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(encoder): serialize NameEmail to str #2479

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/2341-alecgerona.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix(encoder): serialize `NameEmail` to str
2 changes: 2 additions & 0 deletions pydantic/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Pattern = re.compile('a').__class__

from .color import Color
from .networks import NameEmail
from .types import SecretBytes, SecretStr

__all__ = 'pydantic_encoder', 'custom_pydantic_encoder', 'timedelta_isoformat'
Expand Down Expand Up @@ -65,6 +66,7 @@ def decimal_encoder(dec_value: Decimal) -> Union[int, float]:
IPv6Address: str,
IPv6Interface: str,
IPv6Network: str,
NameEmail: str,
Path: str,
Pattern: lambda o: o.pattern,
SecretBytes: str,
Expand Down
3 changes: 2 additions & 1 deletion tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import pytest

from pydantic import BaseModel, create_model
from pydantic import BaseModel, NameEmail, create_model
from pydantic.color import Color
from pydantic.dataclasses import dataclass as pydantic_dataclass
from pydantic.json import pydantic_encoder, timedelta_isoformat
Expand All @@ -35,6 +35,7 @@ class MyEnum(Enum):
(SecretStr(''), '""'),
(SecretBytes(b'xyz'), '"**********"'),
(SecretBytes(b''), '""'),
(NameEmail('foo bar', 'foobaR@example.com'), '"foo bar <foobaR@example.com>"'),
(IPv6Address('::1:0:1'), '"::1:0:1"'),
(IPv4Interface('192.168.0.0/24'), '"192.168.0.0/24"'),
(IPv6Interface('2001:db00::/120'), '"2001:db00::/120"'),
Expand Down