Open orf opened 2 months ago
Describe the bug All calls to BaseModel.model_dump use the default mode argument, which is python and not json. For example:
BaseModel.model_dump
python
json
This means models that use custom types (such as any of these built-in Pydantic types) do not work:
import pydantic, ninja api = NinjaAPI() class SomeModel(pydantic.BaseModel): url: pydantic.AnyUrl @api.get("/test") def example(request) -> SomeModel: return SomeModel.model_validate({ "url": "custom-protocol://foo/bar" })
Fails with:
TypeError: Type is not JSON serializable: pydantic_core._pydantic_core.Url
It seems like we should default to using .model_dump(mode='json')?
.model_dump(mode='json')
You could change example to this:
example
@api.get("/test", response=SomeModel) def example(request): return SomeModel.model_validate({ "url": "custom-protocol://foo/bar" }).model_dump(mode='json')
But this is a shame: we end up calling model_dump and model_validate multiple times (which can be expensive with a lot of data), and we loose type hints on the function.
model_dump
model_validate
Versions (please complete the following information):
Describe the bug All calls to
BaseModel.model_dump
use the default mode argument, which ispython
and notjson
. For example:This means models that use custom types (such as any of these built-in Pydantic types) do not work:
Fails with:
It seems like we should default to using
.model_dump(mode='json')
?You could change
example
to this:But this is a shame: we end up calling
model_dump
andmodel_validate
multiple times (which can be expensive with a lot of data), and we loose type hints on the function.Versions (please complete the following information):