Skip to content

Commit 1d13798

Browse files
authored
feat(event_handler): support richer top level Tags (#3543)
1 parent cedb5c9 commit 1d13798

File tree

3 files changed

+62
-39
lines changed

3 files changed

+62
-39
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
License,
8181
OpenAPI,
8282
Server,
83+
Tag,
8384
)
8485
from aws_lambda_powertools.event_handler.openapi.params import Dependant
8586
from aws_lambda_powertools.event_handler.openapi.types import (
@@ -1360,7 +1361,7 @@ def get_openapi_schema(
13601361
openapi_version: str = DEFAULT_OPENAPI_VERSION,
13611362
summary: Optional[str] = None,
13621363
description: Optional[str] = None,
1363-
tags: Optional[List[str]] = None,
1364+
tags: Optional[List[Union["Tag", str]]] = None,
13641365
servers: Optional[List["Server"]] = None,
13651366
terms_of_service: Optional[str] = None,
13661367
contact: Optional["Contact"] = None,
@@ -1381,7 +1382,7 @@ def get_openapi_schema(
13811382
A short summary of what the application does.
13821383
description: str, optional
13831384
A verbose explanation of the application behavior.
1384-
tags: List[str], optional
1385+
tags: List[Tag | str], optional
13851386
A list of tags used by the specification with additional metadata.
13861387
servers: List[Server], optional
13871388
An array of Server Objects, which provide connectivity information to a target server.
@@ -1403,7 +1404,7 @@ def get_openapi_schema(
14031404
get_compat_model_name_map,
14041405
get_definitions,
14051406
)
1406-
from aws_lambda_powertools.event_handler.openapi.models import OpenAPI, PathItem, Server
1407+
from aws_lambda_powertools.event_handler.openapi.models import OpenAPI, PathItem, Server, Tag
14071408
from aws_lambda_powertools.event_handler.openapi.types import (
14081409
COMPONENT_REF_TEMPLATE,
14091410
)
@@ -1468,7 +1469,7 @@ def get_openapi_schema(
14681469
if components:
14691470
output["components"] = components
14701471
if tags:
1471-
output["tags"] = [{"name": tag} for tag in tags]
1472+
output["tags"] = [Tag(name=tag) if isinstance(tag, str) else tag for tag in tags]
14721473

14731474
output["paths"] = {k: PathItem(**v) for k, v in paths.items()}
14741475

@@ -1482,7 +1483,7 @@ def get_openapi_json_schema(
14821483
openapi_version: str = DEFAULT_OPENAPI_VERSION,
14831484
summary: Optional[str] = None,
14841485
description: Optional[str] = None,
1485-
tags: Optional[List[str]] = None,
1486+
tags: Optional[List[Union["Tag", str]]] = None,
14861487
servers: Optional[List["Server"]] = None,
14871488
terms_of_service: Optional[str] = None,
14881489
contact: Optional["Contact"] = None,
@@ -1503,7 +1504,7 @@ def get_openapi_json_schema(
15031504
A short summary of what the application does.
15041505
description: str, optional
15051506
A verbose explanation of the application behavior.
1506-
tags: List[str], optional
1507+
tags: List[Tag, str], optional
15071508
A list of tags used by the specification with additional metadata.
15081509
servers: List[Server], optional
15091510
An array of Server Objects, which provide connectivity information to a target server.
@@ -1548,7 +1549,7 @@ def enable_swagger(
15481549
openapi_version: str = DEFAULT_OPENAPI_VERSION,
15491550
summary: Optional[str] = None,
15501551
description: Optional[str] = None,
1551-
tags: Optional[List[str]] = None,
1552+
tags: Optional[List[Union["Tag", str]]] = None,
15521553
servers: Optional[List["Server"]] = None,
15531554
terms_of_service: Optional[str] = None,
15541555
contact: Optional["Contact"] = None,
@@ -1573,7 +1574,7 @@ def enable_swagger(
15731574
A short summary of what the application does.
15741575
description: str, optional
15751576
A verbose explanation of the application behavior.
1576-
tags: List[str], optional
1577+
tags: List[Tag, str], optional
15771578
A list of tags used by the specification with additional metadata.
15781579
servers: List[Server], optional
15791580
An array of Server Objects, which provide connectivity information to a target server.

tests/functional/event_handler/test_openapi_params.py

-31
Original file line numberDiff line numberDiff line change
@@ -349,37 +349,6 @@ def handler(user: Annotated[User, Body(embed=True)]):
349349
assert body_post_handler_schema.properties["user"].ref == "#/components/schemas/User"
350350

351351

352-
def test_openapi_with_tags():
353-
app = APIGatewayRestResolver()
354-
355-
@app.get("/users")
356-
def handler():
357-
raise NotImplementedError()
358-
359-
schema = app.get_openapi_schema(tags=["Orders"])
360-
assert len(schema.tags) == 1
361-
362-
tag = schema.tags[0]
363-
assert tag.name == "Orders"
364-
365-
366-
def test_openapi_operation_with_tags():
367-
app = APIGatewayRestResolver()
368-
369-
@app.get("/users", tags=["Users"])
370-
def handler():
371-
raise NotImplementedError()
372-
373-
schema = app.get_openapi_schema()
374-
assert len(schema.paths.keys()) == 1
375-
376-
get = schema.paths["/users"].get
377-
assert len(get.tags) == 1
378-
379-
tag = get.tags[0]
380-
assert tag == "Users"
381-
382-
383352
def test_openapi_with_excluded_operations():
384353
app = APIGatewayRestResolver()
385354

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
2+
from aws_lambda_powertools.event_handler.openapi.models import Tag
3+
4+
5+
def test_openapi_with_tags():
6+
app = APIGatewayRestResolver()
7+
8+
@app.get("/users")
9+
def handler():
10+
raise NotImplementedError()
11+
12+
schema = app.get_openapi_schema(tags=["Orders"])
13+
assert schema.tags is not None
14+
assert len(schema.tags) == 1
15+
16+
tag = schema.tags[0]
17+
assert tag.name == "Orders"
18+
19+
20+
def test_openapi_with_object_tags():
21+
app = APIGatewayRestResolver()
22+
23+
@app.get("/users")
24+
def handler():
25+
raise NotImplementedError()
26+
27+
schema = app.get_openapi_schema(tags=[Tag(name="Orders", description="Order description tag")])
28+
assert schema.tags is not None
29+
assert len(schema.tags) == 1
30+
31+
tag = schema.tags[0]
32+
assert tag.name == "Orders"
33+
assert tag.description == "Order description tag"
34+
35+
36+
def test_openapi_operation_with_tags():
37+
app = APIGatewayRestResolver()
38+
39+
@app.get("/users", tags=["Users"])
40+
def handler():
41+
raise NotImplementedError()
42+
43+
schema = app.get_openapi_schema()
44+
assert schema.paths is not None
45+
assert len(schema.paths.keys()) == 1
46+
47+
get = schema.paths["/users"].get
48+
assert get is not None
49+
assert get.tags is not None
50+
assert len(get.tags) == 1
51+
52+
tag = get.tags[0]
53+
assert tag == "Users"

0 commit comments

Comments
 (0)