Skip to content

Expected a 422 response when model validation fails, but got a 500 instead. #772

@tazimmerman

Description

@tazimmerman

Describe the bug

When responding with a list of JSON objects, if any (or all) of the models created from that JSON fails, I get a 500 response without validation details.

To Reproduce

Steps to reproduce the behavior with a minimum self-contained file.

  1. Create a file with:
from datetime import date
from typing import List

from fastapi import FastAPI
from pydantic import BaseModel

class TheModel(BaseModel):
    from_date: date
    to_date: date

app = FastAPI()

@app.get(
    '/',
    response_model=List[TheModel],
)
def get():
    malformed = {'from_date': None, 'to_date': None}
    return [malformed] * 5
  1. Visit the endpoint / with a browser, wget, curl, etc.
$ wget http://localhost:8080/
HTTP request sent, awaiting response... 500 Internal Server Error

(The output from running uvicorn)

Traceback (most recent call last):
  File "/home/tzimmerman/envs/demo/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 376, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/home/tzimmerman/envs/demo/lib/python3.7/site-packages/fastapi/applications.py", line 140, in __call__
    await super().__call__(scope, receive, send)
  File "/home/tzimmerman/envs/demo/lib/python3.7/site-packages/starlette/applications.py", line 134, in __call__
    await self.error_middleware(scope, receive, send)
  File "/home/tzimmerman/envs/demo/lib/python3.7/site-packages/starlette/middleware/errors.py", line 178, in __call__
    raise exc from None
  File "/home/tzimmerman/envs/demo/lib/python3.7/site-packages/starlette/middleware/errors.py", line 156, in __call__
    await self.app(scope, receive, _send)
  File "/home/tzimmerman/envs/demo/lib/python3.7/site-packages/starlette/exceptions.py", line 73, in __call__
    raise exc from None
  File "/home/tzimmerman/envs/demo/lib/python3.7/site-packages/starlette/exceptions.py", line 62, in __call__
    await self.app(scope, receive, sender)
  File "/home/tzimmerman/envs/demo/lib/python3.7/site-packages/starlette/routing.py", line 590, in __call__
    await route(scope, receive, send)
  File "/home/tzimmerman/envs/demo/lib/python3.7/site-packages/starlette/routing.py", line 208, in __call__
    await self.app(scope, receive, send)
  File "/home/tzimmerman/envs/demo/lib/python3.7/site-packages/starlette/routing.py", line 41, in app
    response = await func(request)
  File "/home/tzimmerman/envs/demo/lib/python3.7/site-packages/fastapi/routing.py", line 140, in app
    exclude_unset=response_model_exclude_unset,
  File "/home/tzimmerman/envs/demo/lib/python3.7/site-packages/fastapi/routing.py", line 72, in serialize_response
    raise ValidationError(errors, field.type_)
pydantic.error_wrappers.ValidationError: 10 validation errors for TheModel
response -> 0 -> from_date
  none is not an allowed value (type=type_error.none.not_allowed)
response -> 0 -> to_date
  none is not an allowed value (type=type_error.none.not_allowed)
response -> 1 -> from_date
  none is not an allowed value (type=type_error.none.not_allowed)
response -> 1 -> to_date
  none is not an allowed value (type=type_error.none.not_allowed)
response -> 2 -> from_date
  none is not an allowed value (type=type_error.none.not_allowed)
response -> 2 -> to_date
  none is not an allowed value (type=type_error.none.not_allowed)
response -> 3 -> from_date
  none is not an allowed value (type=type_error.none.not_allowed)
response -> 3 -> to_date
  none is not an allowed value (type=type_error.none.not_allowed)
response -> 4 -> from_date
  none is not an allowed value (type=type_error.none.not_allowed)
response -> 4 -> to_date
  none is not an allowed value (type=type_error.none.not_allowed)
  1. It returns a 500 response
  2. But I expected it to return a 422 response

Expected behavior

I would expect 422 response with the validation error details.

Environment

  • OS: [Linux]
  • FastAPI Version [0.44.1]
  • Python [3.7.3]
  • Starlette [0.12.9]
  • Pydantic [1.2]
  • Uvicorn [0.9.1]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions