Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Bug when generating Json fields in Pydantic model #456

Open
Leobouloc opened this issue Dec 13, 2023 · 3 comments
Open

Bug: Bug when generating Json fields in Pydantic model #456

Leobouloc opened this issue Dec 13, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@Leobouloc
Copy link

Leobouloc commented Dec 13, 2023

Description

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

from typing import Dict
from pydantic import Json, BaseModel

from polyfactory.factories.pydantic_factory import ModelFactory

class MyModel(BaseModel):
    myfield: Json[Dict[str,str]]

class MyModelFactory(ModelFactory[MyModel]):
    __model__ = MyModel

print(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)

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

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@Leobouloc Leobouloc added the bug Something isn't working label Dec 13, 2023
@guacs
Copy link
Member

guacs commented Dec 14, 2023

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
Copy link
Author

Thank you for your answer! I will look into the solution you propose. Do you have any idea when support could be added (if it is actually possible) ?

@guacs
Copy link
Member

guacs commented Dec 19, 2023

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants