Skip to content

Commit

Permalink
Fix README, shim openapi json 'where' to be compatible with @OpenAPIT…
Browse files Browse the repository at this point in the history
  • Loading branch information
mdconaway committed Oct 30, 2024
1 parent a9d7d70 commit 5b3c9b5
Show file tree
Hide file tree
Showing 5 changed files with 1,055 additions and 995 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ LEAVE_SOCKET_BY_ID
LEAVE_SOCKET_BY_CLIENT
CLIENT_MESSAGE_EVENT
DISCONNECT_EVENT
OPENAPI_WHERE_OVERRIDE
# TYPES / MODELS / SCHEMAS
T
UUID
Expand Down Expand Up @@ -624,20 +625,22 @@ The `Actions` class contains all the business logic for the <i>base</i> CRUD act
Available actions:

```python
async def create(data: create_model)
async def create(request: Request, data: create_model)

async def update(id: id_type = Path(..., alias="id"), *, data: update_model)
async def update(request: Request, id: id_type = Path(..., alias="id"), *, data: update_model)

async def delete(
id: id_type = Path(..., alias="id"),
request: Request, id: id_type = Path(..., alias="id")
)

async def get_by_id(
request: Request,
id: id_type = Path(..., alias="id"),
where: Json = Query(None, alias="where"),
)

async def get_all(
request: Request,
page: int = 1,
limit: int = 10,
columns: list[str] = Query(None, alias="columns"),
Expand Down
7 changes: 6 additions & 1 deletion fastapi_cruddy_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@
)
from .pubsub import PubSub
from .websocket_manager import WebsocketConnectionManager
from .controller import Actions, CruddyController, ControllerConfigurator
from .controller import (
Actions,
CruddyController,
ControllerConfigurator,
OPENAPI_WHERE_OVERRIDE,
)
from .repository import AbstractRepository
from .adapters import (
BaseAdapter,
Expand Down
38 changes: 31 additions & 7 deletions fastapi_cruddy_framework/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@
META_RELATED_RECORDS_KEY = "records"
META_FAILED_RECORDS_KEY = "invalid"
META_VALIDATION_MESSAGES_KEY = "messages"
OPENAPI_WHERE_OVERRIDE = {
"parameters": [
{
"name": "where",
"in": "query",
"description": "Filter Query (JSON Object or Array of Objects)",
"required": False,
"schema": {
"type": "string",
"title": "Where",
"contentSchema": {
"title": "Where",
"type": "object",
"additionalProperties": {"type": "string"},
},
},
}
]
}
# -------------------------------------------------------------------------------------------
# ACTION MAP (FOR REUSE IN CLIENT CODE)
# -------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -98,7 +117,7 @@ def __init__(
self.repository = repository
self.disable_nested_objects = disable_nested_objects

async def create(request: Request, data: create_model):
async def create(request: Request, data: create_model): # type: ignore
the_thing_with_rels: CruddyGenericModel = getattr(data, single_name)
context_data = {DATA_KEY: the_thing_with_rels, META_KEY: None}
# If there is a user space lifecycle hook, run it (allows context mutations)
Expand Down Expand Up @@ -175,7 +194,7 @@ async def create(request: Request, data: create_model):
return single_schema(**context_data)

async def update(
request: Request, id: id_type = Path(..., alias="id"), *, data: update_model
request: Request, id: id_type = Path(..., alias="id"), *, data: update_model # type: ignore
):
the_thing_with_rels: CruddyGenericModel = getattr(data, single_name)
context_data = {DATA_KEY: the_thing_with_rels, META_KEY: {META_ID_KEY: id}}
Expand Down Expand Up @@ -290,7 +309,7 @@ async def delete(
async def get_by_id(
request: Request,
id: id_type = Path(..., alias="id"),
where: Json = Query(None, alias="where"),
where: Json = Query(None, alias="where", include_in_schema=False),
):
context_data = {
DATA_KEY: {
Expand Down Expand Up @@ -324,7 +343,7 @@ async def get_all(
limit: int = self.default_limit,
columns: list[str] = Query(None, alias="columns"),
sort: list[str] = Query(None, alias="sort"),
where: Json = Query(None, alias="where"),
where: Json = Query(None, alias="where", include_in_schema=False),
):
context_data = {
DATA_KEY: {
Expand Down Expand Up @@ -696,12 +715,13 @@ def _ControllerConfigManyToOne(
policies_get_one,
config.foreign_resource.policies["get_one"],
),
openapi_extra=OPENAPI_WHERE_OVERRIDE,
)
async def get_many_to_one(
request: Request,
id: id_type = Path(..., alias="id"),
columns: list[str] = Query(None, alias="columns"),
where: Json = Query(None, alias="where"),
where: Json = Query(None, alias="where", include_in_schema=False),
):
origin_record: CruddyModel | None = await repository.get_by_id(
id=id, request=request
Expand Down Expand Up @@ -849,6 +869,7 @@ def _ControllerConfigOneToMany(
policies_get_one,
config.foreign_resource.policies["get_many"],
),
openapi_extra=OPENAPI_WHERE_OVERRIDE,
)
async def get_one_to_many(
request: Request,
Expand All @@ -857,7 +878,7 @@ async def get_one_to_many(
limit: int = default_limit,
columns: list[str] = Query(None, alias="columns"),
sort: list[str] = Query(None, alias="sort"),
where: Json = Query(None, alias="where"),
where: Json = Query(None, alias="where", include_in_schema=False),
):
origin_record: CruddyModel | None = await repository.get_by_id(
id=id, request=request
Expand Down Expand Up @@ -987,6 +1008,7 @@ def _ControllerConfigManyToMany(
policies_get_one,
config.foreign_resource.policies["get_many"],
),
openapi_extra=OPENAPI_WHERE_OVERRIDE,
)
async def get_many_to_many(
request: Request,
Expand All @@ -995,7 +1017,7 @@ async def get_many_to_many(
limit: int = default_limit,
columns: list[str] = Query(None, alias="columns"),
sort: list[str] = Query(None, alias="sort"),
where: Json = Query(None, alias="where"),
where: Json = Query(None, alias="where", include_in_schema=False),
):
# Consider raising 404 here and in get by ID
if await repository.get_by_id(id=id, request=request) == None:
Expand Down Expand Up @@ -1123,6 +1145,7 @@ def ControllerConfigurator(
response_model=single_schema,
response_model_exclude_none=True,
dependencies=assemble_policies(policies_universal, policies_get_one),
openapi_extra=OPENAPI_WHERE_OVERRIDE,
)(actions.get_by_id)

if not disable_get_many:
Expand All @@ -1132,6 +1155,7 @@ def ControllerConfigurator(
response_model=many_schema,
response_model_exclude_none=True,
dependencies=assemble_policies(policies_universal, policies_get_many),
openapi_extra=OPENAPI_WHERE_OVERRIDE,
)(actions.get_all)

# Add relationship link endpoints starting here...
Expand Down
Loading

0 comments on commit 5b3c9b5

Please sign in to comment.