Skip to content

Fix custom json encoder in pydantic+fastapi

Pydantic v1's json module contain[s|ed] a dictionary called ENCODERS_BY_TYPE that was commonly used to overide default json encoders. Pydantic v2 is moving away from this to a "better" system. FastAPI attempted to stay compatible with both v1 and v2 by pulling ENCODERS_BY_TYPE into their own codebase, but this broke our custom type UTCDateTime, at least for anything using fastapi.

We thought we fixed this with a recent MR, but it turns out all we did was fix the validator. We did not fix the encoder itself. So, validation steps would pass, but we continued to server malformed json for UTCDateTimes. This commit reverts to the earlier version of pydantic_utcdatetime.py, and adds a small kluge that modifies ENCODERS_BY_TYPE in both pydantic and fastapi.

Merge request reports