Skip to content

Commit a90ad07

Browse files
committed
🐛 Flag for including resources in schema
1 parent e3452ae commit a90ad07

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

flama/resources/modules.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ def add_resource(
2828
self,
2929
path: str,
3030
resource: t.Union[Resource, type[Resource]],
31-
tags: t.Optional[dict[str, dict[str, t.Any]]] = None,
3231
*args,
32+
include_in_schema: bool = True,
33+
tags: t.Optional[dict[str, dict[str, t.Any]]] = None,
3334
**kwargs,
3435
) -> "Resource":
3536
"""Adds a resource to this application, setting its endpoints.
3637
3738
:param path: Resource base path.
38-
:param tags: Tags to add to the resource.
3939
:param resource: Resource class.
40+
:param include_in_schema: True if this route or endpoint should be declared as part of the API schema.
41+
:param tags: Tags to add to the resource.
4042
"""
4143
if inspect.isclass(resource) and issubclass(resource, Resource):
4244
resource_instance = resource(*args, **kwargs)
@@ -45,20 +47,28 @@ def add_resource(
4547
else:
4648
raise ValueError("Wrong resource")
4749

48-
self.app.mount(mount=ResourceRoute(path, resource_instance, tags))
50+
self.app.mount(mount=ResourceRoute(path, resource_instance, include_in_schema=include_in_schema, tags=tags))
4951

5052
return resource_instance
5153

52-
def resource(self, path: str, tags: t.Optional[dict[str, dict[str, t.Any]]] = None, *args, **kwargs) -> t.Callable:
54+
def resource(
55+
self,
56+
path: str,
57+
*args,
58+
include_in_schema: bool = True,
59+
tags: t.Optional[dict[str, dict[str, t.Any]]] = None,
60+
**kwargs,
61+
) -> t.Callable:
5362
"""Decorator for Resources classes for adding them to the application.
5463
5564
:param path: Resource base path.
65+
:param include_in_schema: True if this route or endpoint should be declared as part of the API schema.
5666
:param tags: Tags to add to the resource.
5767
:return: Decorated resource class.
5868
"""
5969

6070
def decorator(resource: type[Resource]) -> type[Resource]:
61-
self.add_resource(path, resource, tags, *args, **kwargs)
71+
self.add_resource(path, resource, *args, include_in_schema=include_in_schema, tags=tags, **kwargs)
6272
return resource
6373

6474
return decorator

flama/resources/routing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def __init__(
1818
self,
1919
path: str,
2020
resource: t.Union["Resource", type["Resource"]],
21+
*,
22+
include_in_schema: bool = True,
2123
tags: t.Optional[dict[str, t.Any]] = None,
2224
):
2325
tags = tags or {}
@@ -34,7 +36,7 @@ def __init__(
3436
endpoint=getattr(self.resource, name),
3537
methods=route._meta.methods,
3638
name=route._meta.name or route.__name__,
37-
include_in_schema=route._meta.include_in_schema,
39+
include_in_schema=include_in_schema and route._meta.include_in_schema,
3840
tags=tags.get(name, route._meta.tags),
3941
pagination=route._meta.pagination,
4042
)

tests/resources/test_routing.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,15 @@ async def list(self):
168168
assert response.status_code == 200
169169

170170
def test_method(self):
171-
@ResourceRoute.method(path="/", methods=["POST"], name="foo", tags={"additional": "bar"})
171+
@ResourceRoute.method(
172+
path="/", methods=["POST"], name="foo", include_in_schema=False, tags={"additional": "bar"}
173+
)
172174
def foo(x: int):
173175
return x
174176

175177
assert hasattr(foo, "_meta")
176178
assert foo._meta.path == "/"
177179
assert foo._meta.methods == {"POST"}
178180
assert foo._meta.name == "foo"
181+
assert foo._meta.include_in_schema is False
179182
assert foo._meta.tags == {"additional": "bar"}

0 commit comments

Comments
 (0)