How to expose the drf_spectacular autogenerated documentation of a class-based view that has no direct reference in urls.py?

I have a Django app that based on specific condition, executes one or another class-based view.

def compatibility_whatever_view(request: Request, *args, **kwargs) -> Response:
    if is_legacy:
        return LegacyWhateverView.as_view()(request, *args, **kwargs)
    else:
        return WhateverView.as_view()(request, *args, **kwargs)

I have my WhateverView and LegacyWhateverView with fully compatible API.

urlpatterns = [
    path(
        "whatever/<uuid:whatever_id>/",
        compatibility_whatever_view,
        name="whatever_list_whatevers",
    ),
]

I want to expose the documentation of WhateverView using drf_spectacular. Unfortunately I am failing to do so, as it seems the swagger.yml file is generated only if the class-based view is directly exposed in the urlpatterns, otherwise it does not know how to build the URL endpoint. This totally makes sense, but I would like to be able to somehow expose the automatically generated of WhateverView to the public.

I have tried a bit of magic using:

@drf_spectacular.utils.extend_schema(operation_id="whatever_list_whatevers")
@rest_framework.decorators.api_view()
def compatibility_whatever_view(request, *args, **kwargs): ...

but the generated documentation was lacking the details that are otherwise present when I directly reference the class-based view.

Вернуться на верх