Skip to content

Commit 5b3c9b5

Browse files
author
mdconaway
committed
Fix README, shim openapi json 'where' to be compatible with @OpenAPITools
1 parent a9d7d70 commit 5b3c9b5

File tree

5 files changed

+1055
-995
lines changed

5 files changed

+1055
-995
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ LEAVE_SOCKET_BY_ID
118118
LEAVE_SOCKET_BY_CLIENT
119119
CLIENT_MESSAGE_EVENT
120120
DISCONNECT_EVENT
121+
OPENAPI_WHERE_OVERRIDE
121122
# TYPES / MODELS / SCHEMAS
122123
T
123124
UUID
@@ -624,20 +625,22 @@ The `Actions` class contains all the business logic for the <i>base</i> CRUD act
624625
Available actions:
625626

626627
```python
627-
async def create(data: create_model)
628+
async def create(request: Request, data: create_model)
628629

629-
async def update(id: id_type = Path(..., alias="id"), *, data: update_model)
630+
async def update(request: Request, id: id_type = Path(..., alias="id"), *, data: update_model)
630631

631632
async def delete(
632-
id: id_type = Path(..., alias="id"),
633+
request: Request, id: id_type = Path(..., alias="id")
633634
)
634635

635636
async def get_by_id(
637+
request: Request,
636638
id: id_type = Path(..., alias="id"),
637639
where: Json = Query(None, alias="where"),
638640
)
639641

640642
async def get_all(
643+
request: Request,
641644
page: int = 1,
642645
limit: int = 10,
643646
columns: list[str] = Query(None, alias="columns"),

fastapi_cruddy_framework/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@
5252
)
5353
from .pubsub import PubSub
5454
from .websocket_manager import WebsocketConnectionManager
55-
from .controller import Actions, CruddyController, ControllerConfigurator
55+
from .controller import (
56+
Actions,
57+
CruddyController,
58+
ControllerConfigurator,
59+
OPENAPI_WHERE_OVERRIDE,
60+
)
5661
from .repository import AbstractRepository
5762
from .adapters import (
5863
BaseAdapter,

fastapi_cruddy_framework/controller.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,25 @@
5050
META_RELATED_RECORDS_KEY = "records"
5151
META_FAILED_RECORDS_KEY = "invalid"
5252
META_VALIDATION_MESSAGES_KEY = "messages"
53+
OPENAPI_WHERE_OVERRIDE = {
54+
"parameters": [
55+
{
56+
"name": "where",
57+
"in": "query",
58+
"description": "Filter Query (JSON Object or Array of Objects)",
59+
"required": False,
60+
"schema": {
61+
"type": "string",
62+
"title": "Where",
63+
"contentSchema": {
64+
"title": "Where",
65+
"type": "object",
66+
"additionalProperties": {"type": "string"},
67+
},
68+
},
69+
}
70+
]
71+
}
5372
# -------------------------------------------------------------------------------------------
5473
# ACTION MAP (FOR REUSE IN CLIENT CODE)
5574
# -------------------------------------------------------------------------------------------
@@ -98,7 +117,7 @@ def __init__(
98117
self.repository = repository
99118
self.disable_nested_objects = disable_nested_objects
100119

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

177196
async def update(
178-
request: Request, id: id_type = Path(..., alias="id"), *, data: update_model
197+
request: Request, id: id_type = Path(..., alias="id"), *, data: update_model # type: ignore
179198
):
180199
the_thing_with_rels: CruddyGenericModel = getattr(data, single_name)
181200
context_data = {DATA_KEY: the_thing_with_rels, META_KEY: {META_ID_KEY: id}}
@@ -290,7 +309,7 @@ async def delete(
290309
async def get_by_id(
291310
request: Request,
292311
id: id_type = Path(..., alias="id"),
293-
where: Json = Query(None, alias="where"),
312+
where: Json = Query(None, alias="where", include_in_schema=False),
294313
):
295314
context_data = {
296315
DATA_KEY: {
@@ -324,7 +343,7 @@ async def get_all(
324343
limit: int = self.default_limit,
325344
columns: list[str] = Query(None, alias="columns"),
326345
sort: list[str] = Query(None, alias="sort"),
327-
where: Json = Query(None, alias="where"),
346+
where: Json = Query(None, alias="where", include_in_schema=False),
328347
):
329348
context_data = {
330349
DATA_KEY: {
@@ -696,12 +715,13 @@ def _ControllerConfigManyToOne(
696715
policies_get_one,
697716
config.foreign_resource.policies["get_one"],
698717
),
718+
openapi_extra=OPENAPI_WHERE_OVERRIDE,
699719
)
700720
async def get_many_to_one(
701721
request: Request,
702722
id: id_type = Path(..., alias="id"),
703723
columns: list[str] = Query(None, alias="columns"),
704-
where: Json = Query(None, alias="where"),
724+
where: Json = Query(None, alias="where", include_in_schema=False),
705725
):
706726
origin_record: CruddyModel | None = await repository.get_by_id(
707727
id=id, request=request
@@ -849,6 +869,7 @@ def _ControllerConfigOneToMany(
849869
policies_get_one,
850870
config.foreign_resource.policies["get_many"],
851871
),
872+
openapi_extra=OPENAPI_WHERE_OVERRIDE,
852873
)
853874
async def get_one_to_many(
854875
request: Request,
@@ -857,7 +878,7 @@ async def get_one_to_many(
857878
limit: int = default_limit,
858879
columns: list[str] = Query(None, alias="columns"),
859880
sort: list[str] = Query(None, alias="sort"),
860-
where: Json = Query(None, alias="where"),
881+
where: Json = Query(None, alias="where", include_in_schema=False),
861882
):
862883
origin_record: CruddyModel | None = await repository.get_by_id(
863884
id=id, request=request
@@ -987,6 +1008,7 @@ def _ControllerConfigManyToMany(
9871008
policies_get_one,
9881009
config.foreign_resource.policies["get_many"],
9891010
),
1011+
openapi_extra=OPENAPI_WHERE_OVERRIDE,
9901012
)
9911013
async def get_many_to_many(
9921014
request: Request,
@@ -995,7 +1017,7 @@ async def get_many_to_many(
9951017
limit: int = default_limit,
9961018
columns: list[str] = Query(None, alias="columns"),
9971019
sort: list[str] = Query(None, alias="sort"),
998-
where: Json = Query(None, alias="where"),
1020+
where: Json = Query(None, alias="where", include_in_schema=False),
9991021
):
10001022
# Consider raising 404 here and in get by ID
10011023
if await repository.get_by_id(id=id, request=request) == None:
@@ -1123,6 +1145,7 @@ def ControllerConfigurator(
11231145
response_model=single_schema,
11241146
response_model_exclude_none=True,
11251147
dependencies=assemble_policies(policies_universal, policies_get_one),
1148+
openapi_extra=OPENAPI_WHERE_OVERRIDE,
11261149
)(actions.get_by_id)
11271150

11281151
if not disable_get_many:
@@ -1132,6 +1155,7 @@ def ControllerConfigurator(
11321155
response_model=many_schema,
11331156
response_model_exclude_none=True,
11341157
dependencies=assemble_policies(policies_universal, policies_get_many),
1158+
openapi_extra=OPENAPI_WHERE_OVERRIDE,
11351159
)(actions.get_all)
11361160

11371161
# Add relationship link endpoints starting here...

0 commit comments

Comments
 (0)