I'm trying to make my FastAPI docs pretty. I have 2 endpoints—both post requests. The first has a single required field, while the second has two required fields.
My end points:
#reset session
@app.post("/reset_session/", tags=["Recording"])
async def reset_session( session_id: str = Body(example=SESSION_ID_EXAMPLE, title="Session title bob", description="bob the builder")):
return database.reset_session(session_id)
#reset session/computer_id
@app.post("/reset_session_computer/", tags=["Recording"])
async def reset_session_computer(session_id : str = Body(example=SESSION_ID_EXAMPLE, embed=True, title="sessions are happy"),computer_id : str = Body(example=COMPUTER_ID_EXAMPLE, description="computer description", embed=True) ):
return database.reset_session_for_computer(session_id, computer_id)
I don't want to use a pydantic model.
- Is there a way to embed these two required fields into a starlette Request object and still communicate to the generated docs that these two are required fields in the request?
For example,
#reset session/computer_id
@app.post("/reset_session_computer/", tags=["Recording"])
async def reset_session_computer(request : Request = Body(...): #What goes in here?
session_id = request.json().get("session_id")
computer_id = request.json().get("computer_id")
return database.reset_session_for_computer(session_id, computer_id)
- How do I get the default values to appear in the docs? For the first request, it is working as intended. However, the second request with 2 params, currently, they are showing are showing as
{"session_id":"string", "computer_id":"string"}
rather that what I specified. What's the best way for me to document my function without a pydantic model? Ironically, the schema generates correctly, it's just the default values are not...
Here's a screenshot
working (example value is provided example value:
not working (example values both string):