Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

4 Commits

Repository files navigation

FastAPI - deepObject

An example of implementation for deep object encoding for url query parameters in FastAPI

Proposed usage

To allow deepObjects as defined in Swagger spec we can remap using alias's on the Query. For example in the below we have a User which has a role and a first_name, this would have the url string encoded as "/<endpoint>?user[role]=admin&user[first_name]=Alex", using the proposed solution it handles single level nesting of objects, optional objects and list of objects in the query string. This would then give you access to the User object without additional effort for parsing and validation.

fromtypingimportOptional, ListimportuvicornfromfastapiimportFastAPI, DeepQuery# NOTE: This is proposed usage, see custom.py for implementationfrommodelsimportUser, UserWithEnum# from custom import DeepQueryapp=FastAPI(version="0.1.0", docs_url="/")
@app.get("/optional-search")asyncdefsearch(user: Optional[User] =DeepQuery(Optional[User], name="bob")):
print(user)
@app.get("/enum-search")asyncdefsearch(user: UserWithEnum=DeepQuery(UserWithEnum, name="bob")):
print(user)
@app.get("/search")asyncdefsearch(user: User=DeepQuery(User, name="bob")):
print(user)
@app.get("/searchs")asyncdefsearchs(users: List[User] =DeepQuery(List[User])):
print(users)
@app.get("/unique-searchs")asyncdefsearchs(users: List[User] =DeepQuery(List[User], unique_on=["role"])):
print(users)
if__name__=="__main__":
uvicorn.run("app:app", port=5555, host="0.0.0.0", reload=True, log_config=None)

About

An example of implementation for deep object encoding for url query parameters in FastAPI

Resources

Stars

5 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages