Created
February 17, 2021 21:35
-
-
Save Kludex/ef47c1fbbeb25d05e5e5133d23b6927d to your computer and use it in GitHub Desktop.
Camelize Body and Query parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import Body, FastAPI | |
from fastapi.routing import APIRoute, APIRouter | |
from humps import camelize | |
from pydantic import BaseModel | |
app = FastAPI() | |
router = APIRouter(prefix="/haha") | |
@app.post("/") | |
def home(my_potato: str = Body(..., embed=True)): | |
return my_potato | |
class MyBody(BaseModel): | |
first_var: str | |
second_var: int | |
@app.post("/another") | |
def another(my_body: MyBody): | |
... | |
@router.post("/another") | |
def router_another(my_body: MyBody): | |
... | |
@app.get("/query") | |
def query_endpoint(my_query: str): | |
... | |
app.include_router(router) | |
for route in app.routes: | |
if isinstance(route, APIRoute): | |
for param in route.dependant.query_params: | |
if param.alias == param.name: | |
param.alias = camelize(param.name) | |
for param in route.dependant.body_params: | |
if param.alias == param.name: | |
param.alias = camelize(param.name) | |
if route.body_field: | |
for field in route.body_field.type_.__fields__.values(): | |
field.alias = camelize(field.name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment