Skip to content

Commit

Permalink
Add possibility to ignore apps + namespace in the generated schema (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkjellid committed Aug 8, 2024
1 parent da72e4c commit 610c55a
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions 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,14 +20,33 @@ 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 "
"to a file."
"API_DECORATOR_SCHEMA_PATH must be set in settings in order to save the "
"api spec to a file."
)

path = Path(settings.API_DECORATOR_SCHEMA_PATH) # type: ignore[misc]
Expand Down

0 comments on commit 610c55a

Please sign in to comment.