Repository: aiogram/aiogram
Pull request #1419 (2024-02-17 08:03:54)

Fixed default LinkPreviewOptions JSON serialization
# Description

Fixed JSON serialization of the `LinkPreviewOptions` class while it is passed
as bot-wide default options.


github-actions[bot]: # :heavy_check_mark: Changelog found.

Thank you for adding a description of the changes
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/aiogram/aiogram/pull/1419?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`eef50a2`)](https://app.codecov.io/gh/aiogram/aiogram/commit/eef50a2edb82816f495795def093576be00e3271?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram) 100.00% compared to head [(`6312959`)](https://app.codecov.io/gh/aiogram/aiogram/pull/1419?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram) 100.00%.

<details><summary>Additional details and impacted files</summary>


[![Impacted file tree graph](https://app.codecov.io/gh/aiogram/aiogram/pull/1419/graphs/tree.svg?width=650&height=150&src=pr&token=cFqEr3p9MX&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram)](https://app.codecov.io/gh/aiogram/aiogram/pull/1419?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram)

```diff
@@            Coverage Diff            @@
##           dev-3.x     #1419   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          412       412           
  Lines        10655     10657    +2     
=========================================
+ Hits         10655     10657    +2     
```

| [Flag](https://app.codecov.io/gh/aiogram/aiogram/pull/1419/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram) | Coverage Δ | |
|---|---|---|
| [unittests](https://app.codecov.io/gh/aiogram/aiogram/pull/1419/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram) | `100.00% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Files](https://app.codecov.io/gh/aiogram/aiogram/pull/1419?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram) | Coverage Δ | |
|---|---|---|
| [aiogram/client/session/base.py](https://app.codecov.io/gh/aiogram/aiogram/pull/1419?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aiogram#diff-YWlvZ3JhbS9jbGllbnQvc2Vzc2lvbi9iYXNlLnB5) | `100.00% <100.00%> (ø)` | |

</details>

Patch:
diff --git a/CHANGES/1418.bugfix.rst b/CHANGES/1418.bugfix.rst
new file mode 100644
index 00000000..f933c89d
--- /dev/null
+++ b/CHANGES/1418.bugfix.rst
@@ -0,0 +1,2 @@
+Fixed JSON serialization of the :code:`LinkPreviewOptions` class while it is passed
+as bot-wide default options.
diff --git a/aiogram/client/session/base.py b/aiogram/client/session/base.py
index 503f5298..82ec4691 100644
--- a/aiogram/client/session/base.py
+++ b/aiogram/client/session/base.py
@@ -38,7 +38,7 @@ from aiogram.exceptions import (
 
 from ...methods import Response, TelegramMethod
 from ...methods.base import TelegramType
-from ...types import InputFile
+from ...types import InputFile, TelegramObject
 from ..default import Default
 from ..telegram import PRODUCTION, TelegramAPIServer
 from .middlewares.manager import RequestMiddlewareManager
@@ -233,7 +233,13 @@ class BaseSession(abc.ABC):
             return str(round(value.timestamp()))
         if isinstance(value, Enum):
             return self.prepare_value(value.value, bot=bot, files=files)
-
+        if isinstance(value, TelegramObject):
+            return self.prepare_value(
+                value.model_dump(warnings=False),
+                bot=bot,
+                files=files,
+                _dumps_json=_dumps_json,
+            )
         if _dumps_json:
             return self.json_dumps(value)
         return value


Test patch:
diff --git a/tests/test_api/test_client/test_session/test_base_session.py b/tests/test_api/test_client/test_session/test_base_session.py
index 0b078189..1a1b3c14 100644
--- a/tests/test_api/test_client/test_session/test_base_session.py
+++ b/tests/test_api/test_client/test_session/test_base_session.py
@@ -26,7 +26,7 @@ from aiogram.exceptions import (
     TelegramUnauthorizedError,
 )
 from aiogram.methods import DeleteMessage, GetMe, TelegramMethod
-from aiogram.types import UNSET_PARSE_MODE, User
+from aiogram.types import UNSET_PARSE_MODE, User, LinkPreviewOptions
 from aiogram.types.base import UNSET_DISABLE_WEB_PAGE_PREVIEW, UNSET_PROTECT_CONTENT
 from tests.mocked_bot import MockedBot
 
@@ -111,6 +111,10 @@ class TestBaseSession:
                 ),
                 "1494994302",
             ],
+            [
+                {"link_preview": LinkPreviewOptions(is_disabled=True)},
+                '{"link_preview": {"is_disabled": true}}',
+            ],
         ],
     )
     def test_prepare_value(self, value: Any, result: str, bot: MockedBot):