diff --git a/changes/4005-sergiosim.md b/changes/4005-sergiosim.md new file mode 100644 index 0000000000..abb526712d --- /dev/null +++ b/changes/4005-sergiosim.md @@ -0,0 +1 @@ +Fix Json strategy failure for the complex nested field diff --git a/pydantic/_hypothesis_plugin.py b/pydantic/_hypothesis_plugin.py index 890e192cca..e4f604def1 100644 --- a/pydantic/_hypothesis_plugin.py +++ b/pydantic/_hypothesis_plugin.py @@ -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. @@ -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), diff --git a/tests/test_hypothesis_plugin.py b/tests/test_hypothesis_plugin.py index ad3538826d..5ca99db4ea 100644 --- a/tests/test_hypothesis_plugin.py +++ b/tests/test_hypothesis_plugin.py @@ -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)