Skip to content

Commit

Permalink
feat(event-handler): add description to request body in OpenAPI schema (
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenfonseca authored Dec 21, 2023
1 parent fa9c44c commit 4f7fadb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,9 @@ def _openapi_operation_request_body(
if required:
request_body_oai["required"] = required

if field_info.description:
request_body_oai["description"] = field_info.description

# Generate the request body media type
request_media_content: Dict[str, Any] = {"schema": body_schema}
request_body_oai["content"] = {request_media_type: request_media_content}
Expand Down
24 changes: 24 additions & 0 deletions tests/functional/event_handler/test_openapi_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,30 @@ def handler(user: Annotated[User, Body(embed=True)]):
assert body_post_handler_schema.properties["user"].ref == "#/components/schemas/User"


def test_openapi_with_body_description():
app = APIGatewayRestResolver()

class User(BaseModel):
name: str

@app.post("/users")
def handler(user: Annotated[User, Body(description="This is a user")]):
print(user)

schema = app.get_openapi_schema()
assert len(schema.paths.keys()) == 1

post = schema.paths["/users"].post
assert post.parameters is None
assert post.requestBody is not None

request_body = post.requestBody

# Description should appear in two places: on the request body and on the schema
assert request_body.description == "This is a user"
assert request_body.content[JSON_CONTENT_TYPE].schema_.description == "This is a user"


def test_openapi_with_excluded_operations():
app = APIGatewayRestResolver()

Expand Down

0 comments on commit 4f7fadb

Please sign in to comment.