Search code examples
fastapipydantic

ValidationError on endpoints /api/open-tasks and /api/closed-tasks from tasks_api service


Talking about the Serverless Apps with FastAPI, DynamoDB, and Vue course. I'm currently stuck on section "API" from Part 1, which seems to be pydantic related.

When running the tests with $ poetry run pytest tests.py a ValidationError rises with the following:

>   ???
E   pydantic.error_wrappers.ValidationError: 1 validation error for APITaskList
E   results -> 0
E     value is not a valid dict (type=type_error.dict)

pydantic/main.py:341: ValidationError
============================================= short test summary info ============================
FAILED tests.py::test_list_open_tasks - pydantic.error_wrappers.ValidationError: 1 validation error for APITaskList

I was expecting a succesful execution, since I've reproduced all of the course steps accordingly. But anyways, here are some extra info that might help:

  • Failing command (from tests.py): response = client.get("/api/open-tasks", headers={"Authorization": id_token}) - where client is a TestClient fixtture.

  • Api route ("api/open-tasks"):

@app.get("/api/open-tasks", response_model=APITaskList)
def open_tasks(
    user_email: str = Depends(get_user_email),
    task_store: TaskStore = Depends(get_task_store),
):
    return APITaskList(results=task_store.list_open(owner=user_email))
  • Pydatinc APITaskList schema:
class APITaskList(BaseModel):
    results: list[APITask]

    class Config:
        orm_mode = True
  • Pydantic APITask schema:
class APITask(BaseModel):
    id: UUID
    title: str
    status: TaskStatus
    owner: str

    class Config:
        orm_mode: True

Solution

  • As pointed out on @M.O.'s comment:

    ... you have orm_mode: True instead of orm_mode = True in APITask.Config.

    Test passed successfully after this fix.