litestar-org / litestar

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

Bug: POSTs Result In HTTP 500 When Logging Middleware Enabled #849

Closed rgajason closed 1 year ago

rgajason commented 1 year ago

Describe the bug With the logging middleware enabled, POSTs are returning an HTTP 500.

To Reproduce

from starlite import Starlite, LoggingConfig, post
from starlite.middleware import LoggingMiddlewareConfig
from pydantic import BaseModel

logging_middleware_config = LoggingMiddlewareConfig()

class Widget(BaseModel):
    name: str
    description: str | None = None
    width_inches: float
    height_inches: float

@post("/")
async def my_handler(data: Widget) -> Widget:
    return data

app = Starlite(
    route_handlers=[my_handler],
    logging_config=LoggingConfig(),
    middleware=[logging_middleware_config.middleware],
)

Start up the app, POST to it:

curl --request POST --url http://localhost:8000/ --header 'Content-Type: application/json' --data '{
  "name": "foo",
  "description": "bar",
  "width_inches": 1.23,
  "height_inches": 4.56
}'
{"status_code":500,"detail":"RuntimeError()"}

If I remove the middleware argument from the app and restart, POST works as expected.

Additional context Python 3.10.8

anyio==3.6.2
appdirs==1.4.4
attrs==22.1.0
bcrypt==4.0.1
beanie==1.15.4
black==22.10.0
casbin==1.17.4
casbin-pymongo-adapter==1.0.0
certifi==2022.9.24
cffi==1.15.1
charset-normalizer==2.1.1
click==8.1.3
commonmark==0.9.1
coverage==6.5.0
cryptography==38.0.3
distlib==0.3.6
dnspython==2.2.1
email-validator==1.3.0
eradicate==2.1.0
exceptiongroup==1.0.4
Faker==15.3.2
filelock==3.8.0
flake8==5.0.4
flake8-black==0.3.4
flake8-builtins==2.0.1
flake8-comprehensions==3.10.1
flake8-eradicate==1.4.0
flake8-isort==5.0.0
flake8-plugin-utils==1.3.2
flake8-pytest-style==1.6.0
ggshield==1.14.1
graphql-core==3.2.3
h11==0.14.0
h2==4.1.0
hpack==4.0.0
httpcore==0.16.1
httpx==0.23.1
hyperframe==6.0.1
idna==3.4
iniconfig==1.1.1
isort==5.10.1
Jinja2==3.1.2
ldap3==2.9.1
MarkupSafe==2.1.1
marshmallow==3.18.0
marshmallow-dataclass==8.5.10
mccabe==0.7.0
mongomock==4.1.2
mongomock-motor==0.0.13
motor==3.1.1
multidict==6.0.2
mypy-extensions==0.4.3
oauthlib==3.2.2
orjson==3.8.2
packaging==21.3
passlib==1.7.4
pathspec==0.10.2
pep8-naming==0.13.2
picologging==0.9.1
platformdirs==2.5.4
pluggy==1.0.0
pyasn1==0.4.8
pycodestyle==2.9.1
pycparser==2.21
pydantic==1.10.2
pydantic-factories==1.15.0
pydantic-openapi-schema==1.3.0
pyflakes==2.5.0
pygitguardian==1.4.0
Pygments==2.13.0
pymongo==4.3.3
pyparsing==3.0.9
pytest==7.2.0
pytest-asyncio==0.20.2
pytest-cov==4.0.0
python-dateutil==2.8.2
python-dotenv==0.21.0
python-multipart==0.0.5
PyYAML==6.0
requests==2.28.1
rfc3986==1.5.0
rich==12.5.1
sentinels==1.0.0
sgqlc==16.0
simpleeval==0.9.12
six==1.16.0
sniffio==1.3.0
starlite==1.40.1
starlite-multipart==1.2.0
toml==0.10.2
tomli==2.0.1
typing-inspect==0.8.0
typing_extensions==4.4.0
urllib3==1.26.12
uvicorn==0.20.0
virtualenv==20.16.7
yarl==1.8.1
jtraub commented 1 year ago

I can reproduce the problem. Will take a look into it tonight

provinzkraut commented 1 year ago

@jtraub I hope you don't mind I went ahead with a fix in #860?

@rgajason Merging this PR should fix your problem. Can you confirm?

jtraub commented 1 year ago

@provinzkraut oops, I got buried with day job and forgot about the issue. Thanks for taking care of it.

rgajason commented 1 year ago

@provinzkraut confirmed working! Thank you so much!