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

Fix Json strategy serialization failure #4005

Merged
merged 1 commit into from Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/4005-sergiosim.md
@@ -0,0 +1 @@
Fix Json strategy failure for the complex nested field
4 changes: 3 additions & 1 deletion pydantic/_hypothesis_plugin.py
Expand Up @@ -34,6 +34,7 @@
import pydantic
import pydantic.color
import pydantic.types
from pydantic.utils import lenient_issubclass

# FilePath and DirectoryPath are explicitly unsupported, as we'd have to create
# them on-disk, and that's unsafe in general without being told *where* to do so.
Expand Down Expand Up @@ -222,8 +223,9 @@ def resolve_json(cls): # type: ignore[no-untyped-def]
base=st.one_of(st.none(), st.booleans(), st.integers(), finite, st.text()),
extend=lambda x: st.lists(x) | st.dictionaries(st.text(), x), # type: ignore
)
inner_type = getattr(cls, 'inner_type', None)
return st.builds(
json.dumps,
cls.inner_type.json if lenient_issubclass(inner_type, pydantic.BaseModel) else json.dumps,
inner,
ensure_ascii=st.booleans(),
indent=st.none() | st.integers(0, 16),
Expand Down
1 change: 1 addition & 0 deletions tests/test_hypothesis_plugin.py
Expand Up @@ -67,6 +67,7 @@ class JsonModel(pydantic.BaseModel):
json_str: pydantic.Json[str]
json_int_or_str: pydantic.Json[typing.Union[int, str]]
json_list_of_float: pydantic.Json[typing.List[float]]
json_pydantic_model: pydantic.Json[pydantic.BaseModel]

class ConstrainedNumbersModel(pydantic.BaseModel):
conintt: pydantic.conint(gt=10, lt=100)
Expand Down