class A(BaseModel):
id: PydanticObjectId
raw = '{"id": "5eb7cf5a86d9755df3a6c593"}'
deserialized = A.model_validate_json(raw)
One would expect type(deserialized.id) == PydanticObjectId, but it's actually deserialized to str.
To Reproduce
See example above.
Expected behaviortype(deserialized.id) should be PydanticObjectId.
Additional context
Note that if I do A.model_validate(json.loads(raw)) the code works. This is potentially because in the __get_pydantic_core_schema__ override in PydanticObjectId, python_schema includes a transformation of the id from str to ObjectId, while the json_schema doesn't and directly uses the built-in str_schema.
Describe the bug Consider the following code:
One would expect
type(deserialized.id) == PydanticObjectId
, but it's actually deserialized tostr
.To Reproduce See example above.
Expected behavior
type(deserialized.id)
should bePydanticObjectId
.Additional context Note that if I do
A.model_validate(json.loads(raw))
the code works. This is potentially because in the__get_pydantic_core_schema__
override inPydanticObjectId
,python_schema
includes a transformation of the id fromstr
toObjectId
, while thejson_schema
doesn't and directly uses the built-instr_schema
.