0

I have router in my application that dependency on some function. They both have separate pydantic model as an argument. Problem is when I look at generated documentation, request body only shows parameters from router function argument and ones from dependency function is not included.

So, my question is is there a way I can include those fields? or it was designed this way specifically for some reason?

Code example:

import uvicorn
from fastapi import FastAPI, APIRouter, Depends
from fastapi.responses import JSONResponse
from pydantic import BaseModel

app = FastAPI()


class TestModel(BaseModel):
    field_1: str
    field_2: float


class OtherTestModel(BaseModel):
    some_field_1: int
    some_field_2: bool


router = APIRouter()


def test_dependency(data: OtherTestModel) -> None:
    # do something here
    print(data.dict())
    if data.some_field_1 > 100:
        pass


@router.post(
    path='/',
    dependencies=[Depends(test_dependency)]
)
def test(data: TestModel) -> JSONResponse:

    return JSONResponse(
        content=data.dict()
    )


app.include_router(router)

if __name__ == '__main__':
    uvicorn.run(
        app='app:app',
        host='0.0.0.0',
        port=8000,
        log_level='info',
        debug=True,
    )


Generated documentation: enter image description here

Desired result would be to see some_field_1 and some_field_2 also included in this doc.

Sharmiko
  • 543
  • 4
  • 21
  • 1
    Both dependencies can't have the same name - they'll need to be `data: TestModel` and `other: TestModel`, since when you have two models, they will need to be under their own keys to be able to coordinate which fields belong to which model. See https://stackoverflow.com/questions/66323401/fastapi-having-a-dependency-through-depends-and-a-schema-refer-to-the-same-ro for an explanation. If you make the dependencies have their own names, you'll see that you get a request definition containing both models. – MatsLindh Sep 12 '22 at 13:47

0 Answers0