Skip to content

RequestValidationError is raised when testing, instead of returing 422 response #5404

@jkazimierczak

Description

@jkazimierczak

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import uuid

from fastapi import FastAPI, APIRouter, Header, HTTPException
from fastapi.testclient import TestClient
from pydantic import BaseModel


# Models and CRUD
class Item(BaseModel):
    id: int
    content: str


def crud_get_items():
    # This function lies in a separate module
    return [
        Item(id=1, content="First item"),
        Item(id=2, content="Second item"),
    ]


# App
app = APIRouter()


@app.get("/")
async def get_items(user_id: str = Header()):
    data = crud_get_items()
    if not len(data):  # This is redundant in the example, but exist in "normal" codebase
        raise HTTPException(status_code=404, detail="No items found")

    return data


# Tests
client = TestClient(app)


def test_get_items():
    headers = {"user-id": str(uuid.uuid4())}
    res = client.get("/", headers=headers)
    assert res.status_code == 200


def test_get_items_without_userid():
    res = client.get("/")
    assert res.status_code == 422

Description

When testing my app I noticed, that sending request to router without header parameter set, I get RequestValidationError instead of 422 status code. This doesn't happen, when using FastAPI instead of APIRouter.

One could say, that an easy fix to this is to wrap the second test code in pytest.raises(), but this will only execute the client.get while skipping all asserts the test has.

from fastapi.exceptions import RequestValidationError

def test_get_items_without_userid():
    with pytest.raises(RequestValidationError):
        res = client.get("/")          # This line will execute
        assert res.status_code == 422  # This line won't (and any below) 

Operating System

Windows

Operating System Details

Windows 10, 21H2

FastAPI Version

0.85.0

Python Version

Python 3.10.4

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions