Skip to content

Commit

Permalink
Add documentation for OpenApiWebhook
Browse files Browse the repository at this point in the history
  • Loading branch information
federicobond committed Jan 14, 2024
1 parent 50c15e7 commit 0e628a3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion drf_spectacular/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@
'SERVERS': [],
# Tags defined in the global scope
'TAGS': [],
# Optional: List of OpenAPI 3.1 webhooks.
# Optional: List of OpenAPI 3.1 webhooks. Each entry should be an import path to an
# OpenApiWebhook instance.
'WEBHOOKS': None,
# Optional: MUST contain 'url', may contain "description"
'EXTERNAL_DOCS': {},
Expand Down
17 changes: 17 additions & 0 deletions drf_spectacular/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,23 @@ def __init__(


class OpenApiWebhook(OpenApiSchemaBase):
"""
Helper class to document webhook definitions. A webhook specifies a possible out-of-band
request initiated by the API provider and the expected responses from the consumer.
Please note that this particular :func:`@extend_schema <.extend_schema>` instance operates
from the perspective of the webhook origin, which means that ``request`` specifies the
outgoing request.
For convenience sake, we assume the API provider sends a POST request with a body of type
``application/json`` and the receiver responds with ``200`` if the event was successfully
received.
:param name: Name under which this webhook is listed in the schema.
:param decorator: :func:`@extend_schema <.extend_schema>` decorator that specifies the receiving
endpoint. In this special context the allowed parameters are ``requests``, ``responses``,
``summary``, ``description``, ``deprecated``.
"""
def __init__(
self,
name: _StrOrPromise,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class EventSerializer(serializers.Serializer):
name='SubscriptionEvent',
decorator=extend_schema(
summary="some summary",
description='pushes events to callbackUrl as "application/x-www-form-urlencoded"',
description='pushes events to a webhook url as "application/x-www-form-urlencoded"',
request={
'application/x-www-form-urlencoded': EventSerializer,
},
Expand Down
2 changes: 1 addition & 1 deletion tests/test_webhooks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ components:
webhooks:
SubscriptionEvent:
post:
description: pushes events to callbackUrl as "application/x-www-form-urlencoded"
description: pushes events to a webhook url as "application/x-www-form-urlencoded"
summary: some summary
requestBody:
content:
Expand Down

0 comments on commit 0e628a3

Please sign in to comment.