Skip to content

Commit

Permalink
Load webhooks from settings as import paths
Browse files Browse the repository at this point in the history
  • Loading branch information
federicobond committed Jan 11, 2024
1 parent 1a5c039 commit 79c3432
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
10 changes: 7 additions & 3 deletions drf_spectacular/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re

from django.urls import URLPattern, URLResolver
from django.utils.module_loading import import_string
from rest_framework import views, viewsets
from rest_framework.schemas.generators import BaseSchemaGenerator
from rest_framework.schemas.generators import EndpointEnumerator as BaseEndpointEnumerator
Expand Down Expand Up @@ -185,9 +186,12 @@ def _initialise_endpoints(self):
self.endpoints = self.inspector.get_api_endpoints()

def _initialise_webhooks(self):
if self.webhooks is None:
if spectacular_settings.OAS_VERSION.startswith('3.1'):
self.webhooks = spectacular_settings.WEBHOOKS
if self.webhooks:
return
if not spectacular_settings.OAS_VERSION.startswith('3.1'):
return
webhooks = spectacular_settings.WEBHOOKS or []
self.webhooks = [import_string(s) for s in webhooks]

def _get_paths_and_endpoints(self):
"""
Expand Down
35 changes: 17 additions & 18 deletions tests/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,34 @@ class EventSerializer(serializers.Serializer):


urlpatterns = [] # type: ignore
openapi_webhooks = [
OpenApiWebhook(
name='SubscriptionEvent',
decorator=extend_schema(
summary="some summary",
description='pushes events to callbackUrl as "application/x-www-form-urlencoded"',
request={
'application/x-www-form-urlencoded': EventSerializer,
},
responses={
200: OpenApiResponse(description='event was successfully received'),
'4XX': OpenApiResponse(description='event will be retried shortly'),
},
),
)
]

subscription_event = OpenApiWebhook(
name='SubscriptionEvent',
decorator=extend_schema(
summary="some summary",
description='pushes events to callbackUrl as "application/x-www-form-urlencoded"',
request={
'application/x-www-form-urlencoded': EventSerializer,
},
responses={
200: OpenApiResponse(description='event was successfully received'),
'4XX': OpenApiResponse(description='event will be retried shortly'),
},
),
)


@mock.patch('drf_spectacular.settings.spectacular_settings.OAS_VERSION', '3.1.0')
def test_webhooks(no_warnings):
assert_schema(
generate_schema(None, patterns=[], webhooks=openapi_webhooks),
generate_schema(None, patterns=[], webhooks=[subscription_event]),
'tests/test_webhooks.yml'
)


@pytest.mark.urls(__name__)
@mock.patch('drf_spectacular.settings.spectacular_settings.OAS_VERSION', '3.1.0')
@mock.patch('drf_spectacular.settings.spectacular_settings.WEBHOOKS', openapi_webhooks)
@mock.patch('drf_spectacular.settings.spectacular_settings.WEBHOOKS', ["tests.test_webhooks.subscription_event"])
def test_webhooks_settings(no_warnings):
assert_schema(
SchemaGenerator().get_schema(request=None, public=True),
Expand Down

0 comments on commit 79c3432

Please sign in to comment.