Repository: pydantic/pydantic

Issue:
Json strategy fails serializing nested pydantic Models
### Checks

[x] I added a descriptive title to this issue
[x] I have searched (google, github) for similar issues and couldn't find anything
[x] I have read and followed [the docs](https://pydantic-docs.helpmanual.io/) and still think this is a bug

# Bug

Output of `python -c "import pydantic.utils; print(pydantic.utils.version_info())"`:
```
             pydantic version: 1.9.0
            pydantic compiled: True
                 install path: /home/bender/.local/lib/python3.9/site-packages/pydantic
               python version: 3.9.7 (default, Sep 10 2021, 14:59:43)  [GCC 11.2.0]
                     platform: Linux-5.13.0-39-generic-x86_64-with-glibc2.34
     optional deps. installed: ['dotenv', 'email-validator', 'typing-extensions']
```

First, thank you for this amazing library! Really appreciate it.

Regarding the bug:

When generating pydantic models having nested `Json` fields with `hypothesis`, a JSON serialization exception is raised.

Example:
```py
from hypothesis import strategies as st
from pydantic import BaseModel, Json


class Nested(BaseModel):
    id: str


class JsonModel(BaseModel):
    nested: Json[Nested]


st.builds(JsonModel).example()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/bender/.local/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py", line 334, in example
    example_generating_inner_function()
  File "/home/bender/.local/lib/python3.9/site-packages/hypothesis/strategies/_internal/strategies.py", line 323, in example_generating_inner_function
    @settings(
  File "/home/bender/.local/lib/python3.9/site-packages/hypothesis/core.py", line 1190, in wrapped_test
    raise the_error_hypothesis_found
  File "/usr/lib/python3.9/json/__init__.py", line 234, in dumps
    return cls(
  File "/usr/lib/python3.9/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.9/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.9/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Nested is not JSON serializable
...
```

It seems this is caused by the [_hypothesis_plugin](https://github.com/samuelcolvin/pydantic/blob/master/pydantic/_hypothesis_plugin.py#L226) which tries to call json.dumps on the inner_type of the `Json` field.


Discussion:

Resolving patch:
diff --git a/pydantic/_hypothesis_plugin.py b/pydantic/_hypothesis_plugin.py
--- 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),


Test patch:
diff --git a/tests/test_hypothesis_plugin.py b/tests/test_hypothesis_plugin.py
--- 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)