snok/drf-openapi-tester

Integration for drf-spectacular

Closed this issue Β· 11 comments

drf-yasg project is dead
But now a new project https://github.com/tfranzel/drf-spectacular with support for the openapi scheme is actively developing.
Is integration with this project possible?

What do you mean dead? Have they announced that it’s no longer maintained? Or do you mean that it only supports 2.0 OpenAPI spec?

The project author has stopped responding to community requests. It seems that the project will no longer develop.
I could be wrong..
drf_yasg supports only version 2.0 OpenAPI spec

If you want to support version 3.0 it will be very good πŸ‘‹

Regardless of whether the library is dead or not, we (or you) can very easily add support for testing responses against schemas generated from other libraries πŸŽ‰

The response tester logic written in the package for validating a response against a schema definition is generalized and just needs two variable inputs:

  • API response data
  • An OpenAPI schema (parsed as a dict) <-- This is where a new drf-spectacular-integration first has to be written

If you look at the drf_yasg-part of the package, it's really only a loader.py which holds some package-specific logic to be able to convert drf-yasg-generated documentation to a generalized dict.


All we need to integrate with drf-spectacular is to:

  • Create a new folder for drf-spectacular
  • Create a loader.py which holds a class with a get_response_schema method
  • Create an init.py which calls the response validator function with the new class

If this is unclear, have a look at how the response validation is executed here: https://github.com/snok/django-swagger-tester/blob/master/django_swagger_tester/response_validation/validation.py#L25

And what the drf_yasg __init__ looks like here: https://github.com/snok/django-swagger-tester/blob/master/django_swagger_tester/drf_yasg/__init__.py

The response/input validation functions receive a callable, which is specific to the library.


In terms of supporting different versions of the OpenAPI spec, I agree this is something we should strive to do - but only within the scope of this package.

We have, for example, added support for OpenAPI 3 nullable fields here. Do you have anything else in mind, specifically, that would need to change?

After looking at the package, an integrations seems very simple. I'm actually not even sure you would have to change more than the imports in the existing drf-yasg implementation to make it work for you. The majority of the time will instead be spent writing demo documentation to test with, and perhaps on adding some more OpenAPI 3 specific logic.

Since neither me or @JonasKs use drf-spectacular in our own projects, or are familiar with this library, I suggest you or someone in a similar position submit a PR.

Would you be willing to write an implementation?

Yes, I will try
Earlier in our large project, we also used the drf_yasg, but now we moved to drf-spectacular. Of the advantages of the package is convenient scheme versioning πŸ˜„

Cool, let me know if you need any help understanding the code πŸŽ‰πŸŽ‰

This will be a very useful feature. We are forced to manually generate schema for now to test the schema using this package. I looked into adding support as suggested but it doesnt seem to be as straightforward as the author assumes.

@Goldziher I am not planning to do the integration now. I don't have time for this

i see @MissiaL - I added a WIP PR - see above.

If anyone would be willing to implement drf-spectacular in the demo project, to test that the implementation actually works, the PR referenced looks more or less ready.

With the latest version just published, I think this issue is resolved. @MissiaL if you have the chance to test the new integration that would be great :) Thanks for the help on this @Goldziher