Skip to content

Commit

Permalink
Sanitize the operation ids in a postprocess hook
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellweg committed Jun 13, 2024
1 parent 2501bfd commit 8ab8d5a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ the choice ``Enum`` are consolidated into component objects. You can register ho
.. note:: Please note that setting ``POSTPROCESSING_HOOKS`` will override the default. If you intend to
keep the ``Enum`` hook, be sure to add ``'drf_spectacular.hooks.postprocess_schema_enums'`` back into the list.
The same applies to the ``Operation ID`` hook with ``'drf_spectacular.hooks.postprocess_sanitize_operation_id_uniqueness'``.

Step 7: Preprocessing hooks
---------------------------
Expand Down
4 changes: 2 additions & 2 deletions drf_spectacular/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from drf_spectacular.plumbing import (
ComponentRegistry, alpha_operation_sorter, build_root_object, camelize_operation, get_class,
is_versioning_supported, modify_for_versioning, normalize_result_object,
operation_matches_version, process_webhooks, sanitize_result_object,
operation_matches_version, process_webhooks,
)
from drf_spectacular.settings import spectacular_settings

Expand Down Expand Up @@ -290,4 +290,4 @@ def get_schema(self, request=None, public=False):
for hook in spectacular_settings.POSTPROCESSING_HOOKS:
result = hook(result=result, generator=self, request=request, public=public)

return sanitize_result_object(normalize_result_object(result))
return normalize_result_object(result)
6 changes: 5 additions & 1 deletion drf_spectacular/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from drf_spectacular.drainage import warn
from drf_spectacular.plumbing import (
ResolvedComponent, list_hash, load_enum_name_overrides, safe_ref,
ResolvedComponent, list_hash, load_enum_name_overrides, safe_ref, sanitize_result_object,
)
from drf_spectacular.settings import spectacular_settings

Expand Down Expand Up @@ -197,6 +197,10 @@ def clean(sub_result):
return result


def postprocess_sanitize_operation_id_uniqueness(result, **kwargs):
return sanitize_result_object(result)


def preprocess_exclude_path_format(endpoints, **kwargs):
"""
preprocessing hook that filters out {format} suffixed paths, in case
Expand Down
3 changes: 2 additions & 1 deletion drf_spectacular/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@
# Postprocessing functions that run at the end of schema generation.
# must satisfy interface result = hook(generator, request, public, result)
'POSTPROCESSING_HOOKS': [
'drf_spectacular.hooks.postprocess_schema_enums'
'drf_spectacular.hooks.postprocess_schema_enums',
'drf_spectacular.hooks.postprocess_sanitize_operation_id_uniqueness',
],

# Preprocessing functions that run before schema generation.
Expand Down

0 comments on commit 8ab8d5a

Please sign in to comment.