-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1135 from federicobond/webhooks
Add OpenAPI 3.1 webhook support
- Loading branch information
Showing
6 changed files
with
177 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
from rest_framework import serializers | ||
|
||
from drf_spectacular.generators import SchemaGenerator | ||
from drf_spectacular.utils import OpenApiResponse, OpenApiWebhook, extend_schema | ||
from tests import assert_schema | ||
|
||
|
||
class EventSerializer(serializers.Serializer): | ||
id = serializers.CharField(read_only=True) | ||
change = serializers.CharField() | ||
external_id = serializers.CharField(write_only=True) | ||
|
||
|
||
urlpatterns = [] # type: ignore | ||
|
||
subscription_event = OpenApiWebhook( | ||
name='SubscriptionEvent', | ||
decorator=extend_schema( | ||
summary="some summary", | ||
description='pushes events to a webhook url 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'), | ||
}, | ||
), | ||
) | ||
|
||
|
||
@pytest.mark.urls(__name__) | ||
@mock.patch('drf_spectacular.settings.spectacular_settings.OAS_VERSION', '3.1.0') | ||
@mock.patch('drf_spectacular.settings.spectacular_settings.WEBHOOKS', [subscription_event]) | ||
def test_webhooks_settings(no_warnings): | ||
assert_schema( | ||
SchemaGenerator().get_schema(request=None, public=True), | ||
'tests/test_webhooks.yml' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
openapi: 3.1.0 | ||
info: | ||
title: '' | ||
version: 0.0.0 | ||
paths: {} | ||
components: | ||
schemas: | ||
Event: | ||
type: object | ||
properties: | ||
id: | ||
type: string | ||
readOnly: true | ||
change: | ||
type: string | ||
external_id: | ||
type: string | ||
writeOnly: true | ||
required: | ||
- change | ||
- external_id | ||
- id | ||
webhooks: | ||
SubscriptionEvent: | ||
post: | ||
description: pushes events to a webhook url as "application/x-www-form-urlencoded" | ||
summary: some summary | ||
requestBody: | ||
content: | ||
application/x-www-form-urlencoded: | ||
schema: | ||
$ref: '#/components/schemas/Event' | ||
required: true | ||
responses: | ||
'200': | ||
description: event was successfully received | ||
4XX: | ||
description: event will be retried shortly |