Issue 1:
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.

After submitting this, I commit to one of:

Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.

I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.

Implement a Pull Request for a confirmed bug.

Example

Here's a self-contained, minimal, reproducible, example with my use case:

import logging
from pydantic import BaseModel
from fastapi import FastAPI, Depends, Request

logger = logging.Logger(__name__)

class UserModel(BaseModel):
username: str
name: str = None

app = FastAPI()

@Depends
def get_user_model_data(model: UserModel):
print("Running function get_user_model_data")
return model.dict(exclude_unset=True)

@Depends
def log_new_users(request: Request, data: dict = get_user_model_data):
logger.info(f'Got request from {request.client.host} with username {model.username}')

@app.post('/users', dependencies=[log_new_users])
def create_account(data: dict = get_user_model_data):
return {
"status": "OK",
"data": data,
}

Description

Above is a simple application with a /users endpoint using a pydantic model which requires a username in the post body.

when the model validation passes all is fine. But when model validation fails, the model is validated for each depends separately resulting in duplicated errors.

I would expect that it should be cached even when there are errors so it is validated only once.

Run the app

execute curl -X POST "http://127.0.0.1:8000/api/users" -H "Content-Type: application/json" -d '{"username":null, "name":"string"}'

it will result in duplicate error as follows

{
"detail": [
{
"loc": [
"body",
"username"
],
"msg": "none is not an allowed value",
"type": "type_error.none.not_allowed"
},
{
"loc": [
"body",
"username"
],
"msg": "none is not an allowed value",
"type": "type_error.none.not_allowed"
}
]
}

Environment

OS: macOS:

FastAPI Version 0.63.0:

Python version: 3.9.1

Issue 2:
Really impressed with FastAPI so far... I have search docs github, tickets and googled the issue described below.

Description

How best to work with related tables and corresponding nested pydantic models whilst persisting data in a relational database in an async application?

Additional context

I have been attempting to extend the example in the docs

https://fastapi.tiangolo.com/advanced/async-sql-databases/

which relies on https://github.com/encode/databases

Using three test pydantic models as an example:

class UserModel(BaseModel):
id: int
title: str = Field(..., min_length=2, max_length=50)
firstname: str = Field(..., min_length=1, max_length=50)
firstname: str = Field(..., min_length=1, max_length=50)
username: str = Field(..., min_length=3, max_length=50)
email: str = Field(..., min_length=3, max_length=50)
favourite_book: int = Field(...)

class FavouriteBook(BaseModel):
id: int
title: str = Field(...)
author: str = Field(...)

class ExtendedUser(BaseModel):
id: int
title: str = Field(..., min_length=2, max_length=50)
firstname: str = Field(..., min_length=1, max_length=50)
firstname: str = Field(..., min_length=1, max_length=50)
username: str = Field(..., min_length=3, max_length=50)
email: str = Field(..., min_length=3, max_length=50)
favourite_book: FavouriteBook

the route would ideally be along the lines of...

@router.get("/extended", response_model=List[ExtendedUser])
async def list():
query = **sqlAlchemy/databases call that works**
return database.fetch_all(query=query)

How can a user create a route that returns the nested ExtendedUser from the database without resorting to performing two queries?

An SQL join is a standard way to do this with a single query. However, this does not work with SQLAlchemy core as the two tables contain 'id' and 'title' columns.

It is possible to work with SQLAlchemy orm - but not in an async way as far as I know. (async is my reason for using FastAPI ). I could rename the columns to something unique ( but to rename 'id' column seems like poor database design to me).