You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems like generating Json fields in Pydantic (1.10) creates a bug. Trying the factory_use_construct=True does not help as it provides a JsonWrapperValue object which is not a valid Json.
URL to code causing the issue
No response
MCVE
fromtypingimportDictfrompydanticimportJson, BaseModelfrompolyfactory.factories.pydantic_factoryimportModelFactoryclassMyModel(BaseModel):
myfield: Json[Dict[str,str]]
classMyModelFactory(ModelFactory[MyModel]):
__model__=MyModelprint(type(MyModelFactory.build(factory_use_construct=True).myfield))
# >> HAS INVALID TYPE# <class '.JsonWrapperValue'>MyModelFactory.build()
# >> RAISES EXCEPTION:# E pydantic.error_wrappers.ValidationError: 1 validation error for MyModel# E myfield# E JSON object must be str, bytes or bytearray (type=type_error.json)
Aah Json fields aren't supported for v1. I don't exactly remember why that decision was made, even though I'm the one who did it 😅
I'll see if it's possible to add support for it. As a workaround until it's fixed, you can override the build method to convert the value into a JSON string before the kwargs are passed to the constructor of the model.
@Leobouloc I think it's going to be at least January mid or so before I'll get a chance to work on it. However, if someone else is willing to raise a PR for this, I'd be happy to review it!
Description
It seems like generating
Json
fields in Pydantic (1.10) creates a bug. Trying thefactory_use_construct=True
does not help as it provides aJsonWrapperValue
object which is not a validJson
.URL to code causing the issue
No response
MCVE
Release Version
2.12.0
Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh dashboard
The text was updated successfully, but these errors were encountered: