Skip to content

Commit

Permalink
fix(encoder): serialize NameEmail to str (#2479)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecgerona committed May 9, 2021
1 parent 82fb6eb commit 9f654a1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
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

0 comments on commit 9f654a1

Please sign in to comment.