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

Added fields serializer for LinkPreviewOptions #1483

Closed
wants to merge 9 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGES/1450.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixed json serialization, deserialization for Message
31 changes: 31 additions & 0 deletions aiogram/types/link_preview_options.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import TYPE_CHECKING, Any, Optional, Union

from pydantic import field_serializer, field_validator

from ..client.default import Default
from .base import TelegramObject

Expand All @@ -22,6 +24,35 @@ class LinkPreviewOptions(TelegramObject):
show_above_text: Optional[Union[bool, Default]] = Default("link_preview_show_above_text")
"""*Optional*. :code:`True`, if the link preview must be shown above the message text; otherwise, the link preview will be shown below the message text"""

@field_serializer(
"is_disabled",
"prefer_small_media",
"prefer_large_media",
"show_above_text",
when_used="json",
)
def serialize_fields(self, value: Union[bool, Default]) -> str:
if isinstance(value, bool):
return str(value)
else:
return value.name

@classmethod
@field_validator(
"is_disabled",
"prefer_small_media",
"prefer_large_media",
"show_above_text",
mode="before",
)
def deserialize_fields(cls, value: str) -> Union[bool, Default]:
if value == "True":
return True
elif value == "False":
return False
else:
return Default(value)

if TYPE_CHECKING:
# DO NOT EDIT MANUALLY!!!
# This section was auto-generated via `butcher`
Expand Down
Loading