Skip to content

JSON of model_dump_json of aiogram types are not BotAPI compatible #1277

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

Closed
ghost opened this issue Aug 20, 2023 · 1 comment · Fixed by #1285
Closed

JSON of model_dump_json of aiogram types are not BotAPI compatible #1277

ghost opened this issue Aug 20, 2023 · 1 comment · Fixed by #1285
Assignees
Labels
3.x Issue or PR for stable 3.x version enhancement Make it better!
Milestone

Comments

@ghost
Copy link

ghost commented Aug 20, 2023

aiogram version

3.x

Problem

In case we want to dump current event/message/etc using model_dump_json method we get something like this:

{
  "update_id": 909208484,
  "message": {
    "message_id": 3171,
    "date": "2023-08-20T18:12:29Z",
    "chat": {
      "id": 483808054,
      "type": "private",
      "username": "MrYacha",
      "first_name": "Yacha"
    },
    "from": {
      "id": 483808054,
      "is_bot": false,
      "first_name": "Yacha",
      "username": "MrYacha",
      "language_code": "de",
      "is_premium": true
    },
    "text": "/notes",
    "entities": [
      {
        "type": "bot_command",
        "offset": 0,
        "length": 6
      }
    ]
  }
}

The message.date field has its own type which is not BotAPI compatible.

Possible solution

Make aiogram types serialize to BotAPI standard.

Alternatives

No response

Code example

message.model_dump_json(exclude_none=True, by_alias=True)

Additional information

No response

@ghost ghost added the enhancement Make it better! label Aug 20, 2023
@JrooTJunior JrooTJunior added the 3.x Issue or PR for stable 3.x version label Aug 20, 2023
JrooTJunior added a commit that referenced this issue Aug 20, 2023
This commit adds a new file `custom_types.py` in the aiogram directory.
The inclusion of `DateTime` type ensures better compatibility with the Telegram Bot API. `DateTime` type is further enhanced to work with unixtime.
@JrooTJunior
Copy link
Member

This behaviour was disabled since pydantic 2.0 in due to json_serializers was removed from models settings, so, now it can be resolved via Annotated custom types with custom serializers like:

DateTime = Annotated[
    datetime.datetime,
    PlainSerializer(lambda dt: int(dt.timestamp()), return_type=int, when_used="json"),
]

I think in v3.1-3.3 i can add this alias to codegeneration.

@JrooTJunior JrooTJunior added this to the 3.x future milestone Aug 20, 2023
@JrooTJunior JrooTJunior moved this to 🆕 New in 3.x Development Aug 20, 2023
@JrooTJunior JrooTJunior self-assigned this Aug 20, 2023
JrooTJunior added a commit that referenced this issue Aug 27, 2023
* #1277  Replace datetime.datetime with DateTime across codebase

Replaced all instances of standard library 'datetime.datetime' with a new 'DateTime' type from `.custom` module. This change is necessary to make all date-time values compatible with the Telegram Bot API (it uses Unix time). This will simplify the conversion process and eliminate potential errors related to date-time format mismatches. Changed codebase, butcher files, and modified 'pyproject.toml' to shift the typing-extensions dependency. The 'aiogram/custom_types.py' file was renamed to 'aiogram/types/custom.py' to better reflect its nature as a location for custom types used in the aiogram library.
@github-project-automation github-project-automation bot moved this from 🆕 New to ✅ Done in 3.x Development Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.x Issue or PR for stable 3.x version enhancement Make it better!
Projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

1 participant