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

Error serializing to JSON: PydanticSerializationError: Unable to serialize unknown type: <class 'aiogram.client.default.Default'> #1450

Open
2 tasks done
derby-vv opened this issue Apr 6, 2024 · 2 comments
Labels
bug Something is wrong with the framework

Comments

@derby-vv
Copy link

derby-vv commented Apr 6, 2024

Checklist

  • I am sure the error is coming from aiogram code
  • I have searched in the issue tracker for similar bug reports, including closed ones

Operating system

Windows 11

Python version

3.11

aiogram version

3.4.1

Expected behavior

Correctly serialized json of Message object

Current behavior

Seems like aiogram doesn't properly describe pydantic Default class for link_preview_options.

Steps to reproduce

  1. Send message to bot with link preview
  2. Try to call model_dump_json method on it

Code example

>>> msg.model_dump(exclude_none=True) # default is python mode
 ... link_preview_options=LinkPreviewOptions(is_disabled=<Default('link_preview_is_disabled')>, url='http://t.me/ffffff', ...

>>> msg
 ... link_preview_options=LinkPreviewOptions(is_disabled=<Default('link_preview_is_disabled')>, url='http://t.me/ffffff', prefer_small_media= ...

>>> msg.model_dump_json(exclude_none=True) #trying to serialize to json
pydantic_core._pydantic_core.PydanticSerializationError: Error serializing to JSON: PydanticSerializationError: Unable to serialize unknown type: <class 'aiogram.client.default.Default'>

Logs

No response

Additional information

No response

@derby-vv derby-vv added the bug Something is wrong with the framework label Apr 6, 2024
DanZ-ix pushed a commit to DanZ-ix/aiogram that referenced this issue May 8, 2024
@JrooTJunior
Copy link
Member

If you want to dump objects with the Default placeholder you should use serializer from the Bot session like

files = {}
dump = bot.session.prepare_value(message, bot=bot, files=files)

Note that if you have customized global link preview options dumped object will be affected too in due to this values is unset in the original object.


#1483 seems little bit weird, i need to research to find another solution.

@DanZ-ix
Copy link
Contributor

DanZ-ix commented May 9, 2024

@JrooTJunior
As far as i understood, link_preview_is_disabled is bot parameter, not message. Message object just inherits the value from the bot, when it is sent. But the Message objects are not connected to a Bot object, so we cant really get link_preview_is_disabled value before the message is sent. So there is no way to serialize this value before the message is sent using a Bot object, therefore i think its ok to serialize those variables without a particular value. I just need to be sure that those objects are deserialized in a right way. I'll try to update that PR))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is wrong with the framework
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants