litestar-org / litestar

Production-ready, Light, Flexible and Extensible ASGI API framework | Effortlessly Build Performant APIs
https://litestar.dev/
MIT License
5.66k stars 384 forks source link

Bug: Returning pathlib.WindowsPath causes "Unable to serialize response content" Error #1043

Closed patorf closed 1 year ago

patorf commented 1 year ago

Describe the bug When returning a pathlib.WindowsPath from an RouteHandler the object can not be serialized.

To Reproduce uvicorn==0.20.0 starlite==1.48.1 pydantic==1.10.0 msgspec==0.11.0

import pathlib
import uvicorn
from starlite import Starlite, get

@get('/path')
def get_path() -> pathlib.Path:
    return pathlib.Path()

app = Starlite(route_handlers=[get_path])

if __name__ == "__main__":
    uvicorn.run("rest:app", host="0.0.0.0", port=5010, log_level="debug")

When hitting "http://localhost:5010/path" the Response is 500 "Unable to serialize response content"

Traceback (most recent call last):
  File "C:\code\venv\lib\site-packages\starlite\response\base.py", line 287, in render
    return encode_json(content, self._enc_hook)
  File "C:\code\venv\lib\site-packages\starlite\utils\serialization.py", line 118, in encode_json
    return _msgspec_json_encoder.encode(obj)
  File "C:\code\venv\lib\site-packages\starlite\utils\serialization.py", line 83, in default_serializer
    raise TypeError(f"Unsupported type: {type(value)!r}")
TypeError: Unsupported type: <class 'pathlib.WindowsPath'>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\code\venv\lib\site-packages\starlite\middleware\exceptions\middleware.py", line 47, in __call__
    await self.app(scope, receive, send)
  File "C:\code\venv\lib\site-packages\starlite\routes\http.py", line 75, in handle
    response = await self._get_response_for_request(
  File "C:\code\venv\lib\site-packages\starlite\routes\http.py", line 127, in _get_response_for_request
    response = await self._call_handler_function(
  File "C:\code\venv\lib\site-packages\starlite\routes\http.py", line 163, in _call_handler_function
    else await route_handler.to_response(
  File "C:\code\venv\lib\site-packages\starlite\handlers\http.py", line 660, in to_response
    return await response_handler(app=app, data=data, plugins=plugins, request=request)  # type: ignore
  File "C:\code\venv\lib\site-packages\starlite\handlers\http.py", line 236, in handler
    return await create_response(data=data)
  File "C:\code\venv\lib\site-packages\starlite\handlers\http.py", line 206, in create_response
    response = response_class(
  File "C:\code\venv\lib\site-packages\starlite\response\base.py", line 120, in __init__
    self.body = content if isinstance(content, bytes) else self.render(content)
  File "C:\code\venv\lib\site-packages\starlite\response\base.py", line 289, in render
    raise ImproperlyConfiguredException("Unable to serialize response content") from e
starlite.exceptions.http_exceptions.ImproperlyConfiguredException: 500: Unable to serialize response content
provinzkraut commented 1 year ago

@patorf I put up a PR. Can you confirm that #1044 fixes the issue?

patorf commented 1 year ago

@provinzkraut Yes, the problem was solved! Thanks