Django drf-spectacular - Can you exclude specific paths?

Viewed 1262

We have a bunch of api's with different versions in urls.py, eg

  • api/v1
  • api/v2
  • api/v3

. We want to implement swagger with drf-spectacular, but we only want to expose api/v3 endpoints.

Is there a way to do this? I cannot make sense of the documentation.

Thanks

2 Answers

This works for me:

def preprocessing_filter_spec(endpoints):
    filtered = []
    for (path, path_regex, method, callback) in endpoints:
        # Remove all but DRF API endpoints
        if path.startswith("/api/"):
            filtered.append((path, path_regex, method, callback))
    return filtered

In settings:

"PREPROCESSING_HOOKS": ["common.openapi.preprocessing_filter_spec"],

Worked it out. Copied the custom preprocessing hook function, changed the pass statement to filter what I did not require in the endpoints, then mapped the location of the file and function in my spectacular settings for preprocessed hooks.

Related