1

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.

  1. 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) 
  1. 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: working not working (example values both string): not working

Chris
  • 18,724
  • 6
  • 46
  • 80
mattsap
  • 3,790
  • 1
  • 15
  • 36
  • See related answers [here](https://stackoverflow.com/a/73091485/17865804), as well as [here](https://stackoverflow.com/a/73761724/17865804) and [here](https://stackoverflow.com/a/70636163/17865804). – Chris Nov 27 '22 at 06:27
  • Yeah, these are definitely related but don’t hit the head on the nail. The issue is the documentation is not auto generating as expected rather than being able to call it and have the data flow through. – mattsap Nov 27 '22 at 15:39

0 Answers0