drf-yasg: how to skip some operations during generate_swagger

25 views Asked by At

I use Django with drf-yasg. I have a REST endpoint similar to the following:

@swagger_auto_schema(
    method="POST",
    request_body=...
)
@api_view(["POST"])
def do_something(request: Request) -> HttpResponse:
    ...

When I run the server, I see the do_something operation in /swagger panel, which is fine.

Now, when I run ./manage.py generate_swagger I would like do_something operation to NOT show in the resulting OpenAPI yaml. I would like to be able to somehow annotate the operation to be conditionally included, like with some custom @include_conditionally decorator:

@include_conditionally
@swagger_auto_schema(...)
@api_view(["POST"])
def do_something(request: Request) -> HttpResponse:
    ...

I know that setting auto_schema=None prevents the endpoint from being included in Swagger, but it excludes the endpoint from both Swagger web panel and the yaml schema, which I do not want.

I tried to use my own generator in ./manage.py generate_swagger -g MyGenerator

class MyGenerator(OpenAPISchemaGenerator):
    def should_include_endpoint(self, path, method, view, public):
        ...

while should_include_endpoint seems to be the right way to go, I failed to detect if the operation was annotated with include_conditionally.

Background: I want to exclude some of the endpoints from being exposed with Google ESP while being available locally.

How can I exclude some endpoints conditionally from being included in Swagger schema yaml file?

0

There are 0 answers