Skip to content

Commit

Permalink
Update PydanticEncoder mode (#410)
Browse files Browse the repository at this point in the history
* Update PydanticEncoder mode
* Update release notes
  • Loading branch information
tarsil authored Oct 10, 2024
1 parent 0364c7a commit 72f950e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
10 changes: 10 additions & 0 deletions docs/en/docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ hide:

# Release Notes

## 3.4.3

### Changed

- PydanticEncoder now tries mode `json` first as default.
- Stop ignoring warnings in the tests.
- Stop shadowing the BaseDirective `help` from Lilya.
- Asyncz settings for empty tasks.
- Update the docs for the templates.

## 3.4.2

### Changed
Expand Down
2 changes: 1 addition & 1 deletion esmerald/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "3.4.2"
__version__ = "3.4.3"


from lilya import status
Expand Down
6 changes: 5 additions & 1 deletion esmerald/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)
from msgspec import Struct
from pydantic import BaseModel
from pydantic_core import PydanticSerializationError

from esmerald.exceptions import ImproperlyConfigured
from esmerald.utils.helpers import is_union
Expand Down Expand Up @@ -64,7 +65,10 @@ def is_type(self, value: Any) -> bool:
return isinstance(value, BaseModel) or is_class_and_subclass(value, BaseModel)

def serialize(self, obj: BaseModel) -> dict[str, Any]:
return obj.model_dump()
try:
return obj.model_dump(mode="json")
except PydanticSerializationError:
return obj.model_dump()

def encode(self, annotation: Any, value: Any) -> Any:
if isinstance(value, BaseModel) or is_class_and_subclass(value, BaseModel):
Expand Down
9 changes: 2 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies = [
"email-validator >=2.2.0,<3.0.0",
"itsdangerous>=2.1.2,<3.0.0",
"jinja2>=3.1.2,<4.0.0",
"lilya>=0.9.1",
"lilya>=0.10.0",
"loguru>=0.7.0,<0.8.0",
"pydantic>=2.9.1,<3.0.0",
"pydantic-settings>=2.0.0,<3.0.0",
Expand Down Expand Up @@ -98,12 +98,7 @@ dev = [

jwt = ["passlib==1.7.4", "python-jose>=3.3.0,<4"]
schedulers = ["asyncz>=0.11.0"]
all = [
"esmerald[test,dev,jwt,schedulers]",
"ipython",
"ptpython",
"a2wsgi",
]
all = ["esmerald[test,dev,jwt,schedulers]", "ipython", "ptpython", "a2wsgi"]
testing = [
"mako>=1.2.4,<2.0.0",
"ujson>=5.7.0,<6",
Expand Down

0 comments on commit 72f950e

Please sign in to comment.