Skip to content

Commit 4f7fadb

Browse files
authored
feat(event-handler): add description to request body in OpenAPI schema (#3548)
1 parent fa9c44c commit 4f7fadb

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+3
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,9 @@ def _openapi_operation_request_body(
617617
if required:
618618
request_body_oai["required"] = required
619619

620+
if field_info.description:
621+
request_body_oai["description"] = field_info.description
622+
620623
# Generate the request body media type
621624
request_media_content: Dict[str, Any] = {"schema": body_schema}
622625
request_body_oai["content"] = {request_media_type: request_media_content}

tests/functional/event_handler/test_openapi_params.py

+24
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,30 @@ 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_body_description():
353+
app = APIGatewayRestResolver()
354+
355+
class User(BaseModel):
356+
name: str
357+
358+
@app.post("/users")
359+
def handler(user: Annotated[User, Body(description="This is a user")]):
360+
print(user)
361+
362+
schema = app.get_openapi_schema()
363+
assert len(schema.paths.keys()) == 1
364+
365+
post = schema.paths["/users"].post
366+
assert post.parameters is None
367+
assert post.requestBody is not None
368+
369+
request_body = post.requestBody
370+
371+
# Description should appear in two places: on the request body and on the schema
372+
assert request_body.description == "This is a user"
373+
assert request_body.content[JSON_CONTENT_TYPE].schema_.description == "This is a user"
374+
375+
352376
def test_openapi_with_excluded_operations():
353377
app = APIGatewayRestResolver()
354378

0 commit comments

Comments
 (0)