1

With FastAPI I am trying to inject two dependent functions, each of which have their own pydantic request bodies and form part of separate endpoints into path operation of another endpoint. Please see the example below just for illustration:

from fastapi import FastAPI, Depends
from pydantic import BaseModel

app = FastAPI()

class RequestBodyChild1(BaseModel):
    item1: float
    item2: float

class RequestBody1(BaseModel):
    parent1: RequestBodyChild1

@app.post("/function_1")
def function_1(request: RequestBody1):
    return request

class RequestBodyChild2(BaseModel):
    itemA: float
    itemB: float

class RequestBody2(BaseModel):
    parent2: RequestBodyChild2


@app.post("/function_2")
def function_2(request: RequestBody2):
    return request


@app.post("/function_3")
def function_3(request_1: dict = Depends(function_1), request_2: dict = Depends(function_2)):
    merged_dict = dict(request_1.items() + request_2.items())
    return merged_dict

The issue is that for the function_3 endpoint OpenAPI/Swagger only shows the request body/example schema for request_1: dict = Depends(function_1):

{
  "parent1": {
    "item1": 0,
    "item2": 0
  }
}

How can I make sure that both request bodies are included? Along the lines of:

{
  "parent1": {
    "item1": 0,
    "item2": 0
  },
  "parent2": {
    "itemA": 0,
    "itemB": 0
  }
}

1 Answer 1

1

The reason for the result you're seeing as that you're using the same parameter name in both functions - when you're using Depends in that way, the parameter name in the function is used (the function being registered as a view isn't relevant; only that it is a function); since it's the same in both cases, you only get a single argument name filled. You can see this change if you rename the parameter in function_2 to request2:

{
  "request": {
    "parent1": {
      "item1": 0,
      "item2": 0
    }
  },
  "request2": {
    "parent2": {
      "itemA": 0,
      "itemB": 0
    }
  }
}

To achieve the request format you've described I think you'd have to define the parent1: RequestBodyChild1, parent2: RequestBodyChild2 directly on function_3, and then call the dependent functions manually.

def function_3(parent1: RequestBodyChild1, parent2: RequestBodyChild2):
  ...

This resolves to:

{
  "parent1": {
    "item1": 0,
    "item2": 0
  },
  "parent2": {
    "itemA": 0,
    "itemB": 0
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks for this advice, I wasn't aware the identical parameter names in function_1 and function_2 was the underlying cause of this. Good to know. It's a pity that calling two dependencies doesn't result in the request body I was looking for. A single dependency as a single parameter in function_3 does result in the omission of the "parent1" level. I've gone with the manual solution in the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.