Skip to content

Writing any datetime field raises TypeError: model_to_payload dumps in python mode #22

Description

@baraline

Symptom

Writing any datetime field raises TypeError before the request leaves the process.

from glpi_python_client.models.api_schema.assistance.timeline import PostTicketTask

await client.create_ticket_task(1, PostTicketTask(planned_begin=datetime(2024, 1, 1, 12, 0)))
# TypeError: Object of type datetime is not JSON serializable

Root cause

model_to_payload (_async/clients/commons/_payloads.py:26) calls model.model_dump(...) in python
mode
, so datetime fields stay as datetime objects in the body dict. _execute_request
(_async/clients/commons/_transport.py:233) hands that dict to httpx as json=, whose encoder is
json.dumps.

Reproduced directly:

>>> httpx.Request("POST", "http://x/", json={"planned_begin": datetime.datetime(2024, 1, 1)})
TypeError: Object of type datetime is not JSON serializable

Naive and aware values fail identically.

Why the suite is green

TransportRecorder stubs at client._session.request, i.e. above the httpx JSON encoder, so no unit
test ever encodes a payload. The bug is only reachable against a real socket.

Affected surface

Every model with a declared, writable datetime field: PostTicketTask/PatchTicketTask
(planned_begin, planned_end, date), PostFollowup, PostSolution, PostUser/PatchUser
(begin_date, end_date, substitution_*), and the KB post models.

Fix direction

model_dump(mode="json", exclude_none=True, exclude={"extra_payload"}).

This decides the outbound wire format, so it must land before the aware-datetime work: once inbound
values are aware, mode="json" starts emitting +01:00/Z offsets into GLPI's MySQL DATETIME columns,
which is a separate decision.

Needs a test that asserts the payload is JSON-encodable, not merely that the right dict was produced.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions