Skip to content

Fix: Better JSONification of pydantic models

Compare
Choose a tag to compare
@hxjo hxjo released this 31 May 12:53
· 7 commits to main since this release
b6e2186

Context

When building props, Pydantic models were converted to dict using Pydantic's model_dump method, which converts the model to a dict.
When an Inertia call was made, a simple JSONResponse (FastAPI's one) was returned, with the props.
However, JSONResponse does not handle some fields, such as datetime, and would require either:

  • Handling those prior to passing the models to the render method of inertia
  • Overriding the render method of inertia in order to use a custom JSONResponse which would handle those cases

This is very unoptimal.

Solution

  • Instead of using Pydantic's model_dump method, use its model_dump_json method which handles much more types, and use json.loads to convert it back to a dict.

  • Expose a _render_json method so it can be easily overridden, without having to override the whole render method.