I try to create a simple FastAPI app:
from pydantic import BaseModel
class RephrasingRequest(BaseModel):
content: str
"""The content to rephrase"""
n_outputs: int = 1
"""How many examples to generate?"""
app_debug = FastAPI(
title="debug", description="View project's README.md for details", docs_url="/"
)
@app_debug.post("/rephrase")
async def rephrase_question(request: RephrasingRequest) -> str:
msg = "got the request " + str(request)
return msg
When I go to the app SwaggerUI page, I see the endpoint documentation, but it says "No parameters", as if the function does not accept parameters.
screenshot of the original doc page
Also, and I think this is related, the UI doesn't format the documentation properly. For example, if I add the following docstring:
@app_debug.post("/rephrase")
async def rephrase_question(request: RephrasingRequest) -> str:
"""
This is a sample doc.
:param request:
The request
:return:
What we return
"""
msg = "got the request " + str(request)
return msg
the result is malformatted screenshot of the modififed doc page
(note that the formatting strings are there)
I expected to see propely formatted documentation, including a proper list of parameters
You're confusing arguments to your HTTP endpoint and arguments to your python function.
You have a Pydantic model as the argument, this is mapped as a request body to HTTP endpoint (which you can see in your screenshot below the part about parameters).
When you're talking about parameters to the endpoint, those would be what you find in a query string (?foo=bar
). The model you've told your function to expect has to be mapped to a JSON body, and not URL parameters (since it can be a complex model).
If you want query parameters, use:
def foo(bar: int):
# or
def foo(bar: int = Query(...)):
The comment format is Markdown, and markdown does not consider a new line as anything other than plain text (i.e. not a break) if you've just indented the next line. If you want to have a newline, you can usually do that by using the expected Markdown format (end the line with two spaces)