Skip to content

Commit

Permalink
Implement rudimentary "dynamic"/"generic" fallback route for GET /ent…
Browse files Browse the repository at this point in the history
…ity/{schema_slug}

Manually tested to return the same results as the old GET /entity/product dynamic route (when called with the right parameters).
  • Loading branch information
der-gabe committed Dec 8, 2023
1 parent 5874722 commit 2e74d81
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
15 changes: 10 additions & 5 deletions backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .config import settings, VERSION
from .database import SessionLocal
from .enum import FilterEnum
from .dynamic_routes import create_dynamic_router
from .dynamic_routes import create_dynamic_router, router as dynamic_router
from .models import Schema, AttributeDefinition, AttrType
from .general_routes import router

Expand Down Expand Up @@ -83,7 +83,11 @@ def fix_openapi_schema(db, app):
operation_ids = set()
for route in app.routes:
if route.path in paths_to_remove:
# ...
# Create OpenAPI path for route by doing the following:
# * check if `route.include_in_schema` is `True`, otherwise path is just an ampty dict
# * Iterate over `route.methods` (asserted to be a list earlier) and for each method
# - call `get_openapi_operation_metadata` with the route, method and operation_ids to get operation metadata
# - call `...` ... you know what? `get_openapi_path is an incredibly, stupidly, mind-boggingly long function that does way to mayn things and I don't think writing that all down here will get me anywhere.
result = get_openapi_path(
route=route,
operation_ids=operation_ids,
Expand Down Expand Up @@ -141,10 +145,11 @@ def create_app(session: Optional[Session] = None) -> FastAPI:
load_dynamic_routes(db=db, app=app)

app.include_router(router)
app.include_router(dynamic_router)

# Need to use a closure here to be able to pass `app` to `fix_openapi_schema`
def get_custom_openapi():
return fix_openapi_schema(db, app)
app.openapi = get_custom_openapi
# def get_custom_openapi():
# return fix_openapi_schema(db, app)
# app.openapi = get_custom_openapi

return app
35 changes: 35 additions & 0 deletions backend/dynamic_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,38 @@ def create_dynamic_router(schema: Schema, app: FastAPI, old_slug: str = None):

app.include_router(router, prefix='/entity')
app.openapi_schema = None



router = APIRouter()

@router.get(
'/entity/{schema_slug}',
# response_model=Page[factory(schema=schema, variant=ModelVariant.GET)],
# tags=[schema.name],
# summary=f'List {schema.name} entities',
# description=description,
# response_model_exclude_unset=True
)
def get_entities(
schema_slug: str,
# all: bool = Query(False, description='If true, returns both deleted and not deleted entities'),
# deleted_only: bool = Query(False, description='If true, returns only deleted entities. *Note:* if `all` is true `deleted_only` is not checked'),
# all_fields: bool = Query(False, description='If true, returns data for all entity fields, not just key ones'),
# filters: filter_model = Depends(),
# order_by: str = Query('name', description='Ordering field'),
# ascending: bool = Query(True, description='Direction of ordering'),
db: Session = Depends(get_db),
# params: Params = Depends()
):
return crud.get_entities(
db=db,
schema=schema,
# params=params,
# all=all,
# deleted_only=deleted_only,
# all_fields=all_fields,
# filters=new_filters,
# order_by=order_by,
# ascending=ascending
)

0 comments on commit 2e74d81

Please sign in to comment.