title: FastAPI throws JSON serializable error on using Field and constr together
body: <p>FastAPI throws <code>TypeError: Object of type 'type' is not JSON serializable</code> error on creating <code>openapi.json</code> when using a <code>BaseModel</code> which includes <code>Field</code> and a <code>constr</code> object as default.</p>
  <p>A <a href="https://stackoverflow.com/help/minimal-reproducible-example">MRE</a>:</p>
  <pre><code>from fastapi import FastAPI
  from pydantic import BaseModel, Field, constr
  
  app = FastAPI()
  
  
  class CoolModel(BaseModel):
      this: str = Field(constr(strip_whitespace=True), min_length=10, max_length=20)
  
  
  @app.get(&quot;/&quot;, response_model=CoolModel)
  async def something(some_model: CoolModel):
      return some_model
  
  </code></pre>
  <p>Going to <code>http://localhost:8000/openapi.json</code> throws this:</p>
  <pre><code>INFO:     127.0.0.1:59046 - &quot;GET /docs HTTP/1.1&quot; 200 OK
  INFO:     127.0.0.1:59046 - &quot;GET /openapi.json HTTP/1.1&quot; 500 Internal Server Error
  ERROR:    Exception in ASGI application
  Traceback (most recent call last):
    File &quot;/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py&quot;, line 373, in run_asgi
      result = await app(self.scope, self.receive, self.send)
    File &quot;/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py&quot;, line 75, in __call__
      return await self.app(scope, receive, send)
    File &quot;/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/fastapi/applications.py&quot;, line 208, in __call__
      await super().__call__(scope, receive, send)
    File &quot;/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/applications.py&quot;, line 112, in __call__
      await self.middleware_stack(scope, receive, send)
    File &quot;/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/middleware/errors.py&quot;, line 181, in __call__
      raise exc from None
    File &quot;/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/middleware/errors.py&quot;, line 159, in __call__
      await self.app(scope, receive, _send)
    File &quot;/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/exceptions.py&quot;, line 82, in __call__
      raise exc from None
    File &quot;/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/exceptions.py&quot;, line 71, in __call__
      await self.app(scope, receive, sender)
    File &quot;/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/routing.py&quot;, line 580, in __call__
      await route.handle(scope, receive, send)
    File &quot;/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/routing.py&quot;, line 241, in handle
      await self.app(scope, receive, send)
    File &quot;/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/routing.py&quot;, line 52, in app
      response = await func(request)
    File &quot;/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/fastapi/applications.py&quot;, line 161, in openapi
      return JSONResponse(self.openapi())
    File &quot;/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/fastapi/applications.py&quot;, line 136, in openapi
      self.openapi_schema = get_openapi(
    File &quot;/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/fastapi/openapi/utils.py&quot;, line 387, in get_openapi
      definitions = get_model_definitions(
    File &quot;/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/fastapi/utils.py&quot;, line 24, in get_model_definitions
      m_schema, m_definitions, m_nested_models = model_process_schema(
    File &quot;pydantic/schema.py&quot;, line 548, in pydantic.schema.model_process_schema
    File &quot;pydantic/schema.py&quot;, line 589, in pydantic.schema.model_type_schema
    File &quot;pydantic/schema.py&quot;, line 234, in pydantic.schema.field_schema
    File &quot;pydantic/schema.py&quot;, line 202, in pydantic.schema.get_field_info_schema
    File &quot;pydantic/schema.py&quot;, line 899, in pydantic.schema.encode_default
    File &quot;pydantic/json.py&quot;, line 95, in pydantic.json.pydantic_encoder
  TypeError: Object of type 'type' is not JSON serializable
  </code></pre>