Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback/webhook schema generation #665

Closed
Jusstas opened this issue Feb 21, 2022 · 11 comments
Closed

Callback/webhook schema generation #665

Jusstas opened this issue Feb 21, 2022 · 11 comments
Labels
enhancement New feature or request

Comments

@Jusstas
Copy link
Contributor

Jusstas commented Feb 21, 2022

I want to document my callbacks/webhooks as shown in the Petstore example. I am using serializers to serialize data and post it to some external URLs. Is there any way I can generate some of the schema required to achieve this? I know I can use postprocessing hooks to insert webhook callback operation, but I do not know how to get the schema from the serializer to use in it or if it is some better approach to this.

@tfranzel
Copy link
Owner

I see. This is in fact a missing feature and has not come up that often before. I'll have a look on how to integrate this.

In the meantime, postprocessing hooks can be a bit complicated but everything you need is indeed there. You have access to
def custom_postprocessing_hook(result, generator, request, public) where the complete schema is in result and all parsed serializer components are in generator.registry._components.

@tfranzel tfranzel added the enhancement New feature or request label Feb 22, 2022
tfranzel added a commit that referenced this issue Feb 25, 2022
tfranzel added a commit that referenced this issue Feb 25, 2022
tfranzel added a commit that referenced this issue Feb 25, 2022
@tfranzel
Copy link
Owner

callback interface proposal in PR #666

tfranzel added a commit that referenced this issue Feb 28, 2022
add OpenAPI callback operations #665
@tfranzel
Copy link
Owner

closed by #666

@Jusstas
Copy link
Contributor Author

Jusstas commented Feb 28, 2022

Thank you ❤️

@CodeWithOz
Copy link

@Jusstas how did you structure your code so that the webhooks got picked up and included in the generated yaml file? I can't figure out how to do it. ☹️

@tfranzel
Copy link
Owner

@CodeWithOz you might want to look at the tests that use that functionality. It is easier to understand by example than explanation:

https://github.com/tfranzel/drf-spectacular/blob/master/tests/test_callbacks.py

and resulting schema:

https://github.com/tfranzel/drf-spectacular/blob/master/tests/test_callbacks.yml

@CodeWithOz
Copy link

@tfranzel worked a treat! Amazing thank you!

@nitsujri
Copy link

nitsujri commented May 9, 2023

@CodeWithOz how did you do the webhook part? I copied the test_callbacks.py + yml and it looks good for callbacks but did you get ti like the webhooks?

I was hoping for this and maybe I'm doing something wrong?

ReDoc_Interactive_Demo

@CodeWithOz
Copy link

Screenshot 2023-05-09 at 10 41 34 AM

This is what I did and it worked. By the way I'm using readme.io to generate my API documentation so I can't comment on the tool you're using. @nitsujri

@tfranzel
Copy link
Owner

tfranzel commented May 9, 2023

Looks like webhook and callback are not the same thing. webhook was added in OpenAPI 3.1 and is not available in 3.0.3 (which we produce). So we only have callback at the moment.

excerpt from the 3.1 spec:

The incoming webhooks that MAY be received as part of this API and that the API consumer MAY choose to implement. Closely related to the callbacks feature, this section describes requests initiated other than by an API call, for example by an out of band registration. The key name is a unique string to refer to each webhook, while the (optionally referenced) Path Item Object describes a request that may be initiated by the API provider and the expected responses. An example is available.`

Redoc reference:
https://redocly.com/docs/openapi-visual-reference/webhooks/
https://redocly.com/docs/openapi-visual-reference/callbacks/

@nitsujri
Copy link

nitsujri commented May 9, 2023

@tfranzel thanks for the update! I thought I was going bonkers missing something, but this is much appreciated.

My current work around is to mimic Redoc's example:

SPECTACULAR_SETTINGS = {
  "EXTENSIONS_ROOT": {
     "x-webhooks": {...},
  }
}

# View.py
@extend_schema(tags=["webhook_payloads"], auth=[])
class WebhookPayloadView(viewsets.GenericViewSet):
    serializer_class = MyAwesomeWebhookSerializer

The empty view class to generate the serializer's reference, then manually get the ref for $ref. It's brittle, but it works great for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants