Understanding Sub-Dependency Injection Without Pydantic Model with Simple Example in FastAPI:

Problem Statement:

We have seen in the previous post how to add Dependency Injection function for your query parameter, but in this post, we will see how to create Sub-Dependency with multilevel sub dependency approach.

Solution

To include multilevel dependency, one need to put dependency injection in n-1 level of multi-level methods.

In this post we will see how to achieve below level of dependency injection:

sub_dependency_example(endpoint get method) –> subdependency_level2 –> subdependency_level1

Explanation with Example:

For example, if this is the endpoint method then first it will introduce dependency injection by declaring the endpoint query parameter to dependent method using en-circulating under Depends keyword. Below if you see the endpoint method “sub_dependency_example” declare method parameter sub_dep_result of type Depeds(subdependency_level2).

# Creating Endpoint using the subdependency concept
@app.get("/subdependency/example/")
async def sub_dependency_example(sub_dep_result: str = Depends(subdependency_level2)):
    return {"output": sub_dep_result}

Second level dependency, we will write the definition of “subdependency_level2” method which was defined under dependency injection in the get endpoint method. Below is the definition of method

def subdependency_level2(dependent_value: str = Depends(subdependency_level1)):
    return f"Subdependency using {dependent_value}"

if you see the parameter of the method, it again calls another dependency injection method i.e. “subdependency_level2” also it expect some value to be return from the dependency method which it will append to the return value f”Subdependency using {dependent_value}”

Now let’s look at the bottom most method, of query parameter

def subdependency_level1():
    return "root dependency"

Here it has the implementation, and it return some string value to the subdependency_level2 method then in return it subdependency_level2 will return some value to the endpoint method.

sub_dependency_example <– subdependency_level2 <– subdependency_level1

Here is the complete code:

from fastapi import FastAPI, Depends

app = FastAPI()


# Main Dependency
def subdependency_level1():
    return "root dependency"

# Subdependency
def subdependency_level2(dependent_value: str = Depends(subdependency_level1)):
    return f"Subdependency using {dependent_value}"


# Creating Endpoint using the subdependency concept
@app.get("/subdependency/example/")
async def sub_dependency_example(sub_dep_result: str = Depends(subdependency_level2)):
    return {"output": sub_dep_result}

Output on SwaggerUI:

Here if you see

The root method return string value, which was then appended to the second level of dependency then the appended result was sent to the endpoint get method which convert the appended string to json object output. Below is the sequence of result formation

“root dependency” à “Subdependency using root dependency” à {“output”: “Subdependency using root dependency”}

The same output we can see on swaggerUI below:

Thank you, in the next post we will try to use sub dependency for validation of endpoints parameters using pydantic model.

Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *