- Notifications
You must be signed in to change notification settings - Fork0
PierroD/fastapi-query-parameter-model
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
QueryParameterModel is a Python library designed to simplify the parsing of query parameters in FastAPI applications. Utilizing Pydantic for validation, this package offers a streamlined approach to handling query parameters, including automatic conversion of camelCase query parameters from the URL to Python's snake_case convention.
- CamelCase to SnakeCase Conversion: Automatically convert camelCase query parameters from the URL to Python's snake_case convention.
- Flexible Parsing: Parse query parameters into a full Pydantic model, which is particularly useful when handling multiple parameters in FastAPI routes.
- List Support: Easily handle query parameters that are lists.
- FastAPI Integration: Seamlessly integrate with FastAPI’s dependency injection system.
You can installQueryParameterModel via pip:
pip install fastapi-query-parameter-model
Create a model by inheriting fromQueryParameterModel and define your query parameters. CamelCase query parameters from the URL will be converted to snake_case in your model.
fromfastapi_query_parameter_modelimportQueryParameterModelfromfastapiimportQueryclassSmallTestQueryParamsModels(QueryParameterModel):title:strage:int=0is_major:bool=Query(alias="isMajor",default=False)array:list[str]= []
Use FastAPI’s dependency injection to parse query parameters into the model:
fromfastapiimportFastAPI,Dependsfromfastapi_query_parameter_modelimportQueryParameterModelapp=FastAPI()@app.get("/small-test")asyncdefsmall_test(query_params:SmallTestQueryParamsModels=Depends(SmallTestQueryParamsModels.parser)):returnquery_params.model_dump()
Here are two example endpoints demonstrating how to use the models with FastAPI:
fromfastapiimportFastAPI,Depends,Queryfromfastapi_query_parameter_modelimportQueryParameterModelapp=FastAPI()classSmallTestQueryParamsModels(QueryParameterModel):title:strage:int=0is_major:bool=Query(alias="isMajor",default=False)array:list[str]= []classFullTestQueryParamsModels(QueryParameterModel):title:strage:int=0is_major:bool=Query(alias="isMajor",default=False)str_array:list[str]=Query(alias="strArray",default=[])int_array:list[int]=Query(alias="intArray",default=[])@app.get("/small-test")asyncdefsmall_test(query_params:SmallTestQueryParamsModels=Depends(SmallTestQueryParamsModels.parser)):returnquery_params.model_dump()@app.get("/full-test")asyncdeffull_test(query_params:FullTestQueryParamsModels=Depends(FullTestQueryParamsModels.parser)):returnquery_params.model_dump()
Using the following query :small-test?title=thisATitle&isMajor=true&array[]=a&array[]=b&array[]=c
For the/small-test route, a sample response might look like this:
{"title":"thisATitle","age":0,"is_major":true,"array": ["a","b","c"]}This response shows how the query parameters are parsed and returned as a Pydantic model instance with snake_case attributes.
parser(request: Request): Parses the query parameters from a FastAPIRequestobject and returns an instance of the model with converted snake_case attributes.model_dump(): This is a native pydantic function, only used in this example, to show the result of the parsed query parameters model
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Commit your changes.
- Push your branch and open a pull request.
Please ensure that your code adheres to the existing style and includes tests where applicable.
QueryParameterModel is licensed under the MIT License. See theLICENSE file for details.
For questions or feedback, feel free to open an issue on theGitHub repository.
This revised README should more accurately reflect the functionality of yourQueryParameterModel and help users understand its features and usage. Let me know if there are any other adjustments or additional details you’d like to include!
About
Transform your camelCase query parameters into a model using snake_case fields
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.