fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

Consider warning about misuse of query parameters with BaseModel

Open nvgoldin opened this issue 4 years ago • 0 comments

I've noticed several times, especially for new users - a common mistake is to put for a in a GET request with query parameters, a BaseModel type annotation, causing the OpenAPI docs to miss the parameter entirely. Consider the following code:

from pydantic import BaseModel
from fastapi import FastAPI


class UserDetails(BaseModel):
    username: str
    full_name: str


api = FastAPI()


@api.get('/user-details')
def get_user_details(details: UserDetails):
    pass

The rendered OpenAPI documentation parameters is empty: image

Is this something that would be worth solving? I think it would ease the on-boarding of new users. I can think of two solutions:

  1. Printing a warning message in debug mode when that usage is detected.
  2. Throwing an exception? probably too intrusive.

What do yo think? would be happy to implement the solution.

nvgoldin avatar Jul 09 '20 18:07 nvgoldin