Skip to content

Commit

Permalink
Add possibility to ignore apps + namespace in the generated schema
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkjellid committed Aug 8, 2024
1 parent 5fa6354 commit 9c56a18
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion django_api_decorator/schema_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Any

from django.conf import settings
from django.urls.resolvers import URLResolver

from .openapi import generate_api_spec

Expand All @@ -19,13 +20,29 @@ def get_api_spec() -> dict[str, Any]:

urlpatterns = import_module(settings.ROOT_URLCONF).urlpatterns

ignored_resolvers = getattr(settings, "API_DECORATOR_SCHEMA_IGNORED_RESOLVERS", [])
resolvers_to_ignore = []

# iterate through urlpatterns to find resolvers to remove.
for resolver_or_pattern in urlpatterns:
if not isinstance(resolver_or_pattern, URLResolver):
continue

app_name, namespace = resolver_or_pattern.app_name, resolver_or_pattern.namespace
if (app_name, namespace) in ignored_resolvers:
resolvers_to_ignore.append(resolver_or_pattern)

# Remove ignored resovlers from the urlpatterns sequence.
for resolver in resolvers_to_ignore:
urlpatterns.remove(resolver)

return generate_api_spec(urlpatterns=urlpatterns)


def get_path() -> Path:
if not hasattr(settings, "API_DECORATOR_SCHEMA_PATH"):
raise ValueError(
"ROOT_URLCONF must be set in settings in order to save the api spec "
"API_DECORATOR_SCHEMA_PATH must be set in settings in order to save the api spec "
"to a file."
)

Expand Down

0 comments on commit 9c56a18

Please sign in to comment.