From 8ab8d5aeb4faf04a4a72b6c0f1553f7de28316c0 Mon Sep 17 00:00:00 2001 From: Matthias Dellweg Date: Thu, 13 Jun 2024 13:01:10 +0200 Subject: [PATCH] Sanitize the operation ids in a postprocess hook --- docs/customization.rst | 1 + drf_spectacular/generators.py | 4 ++-- drf_spectacular/hooks.py | 6 +++++- drf_spectacular/settings.py | 3 ++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/customization.rst b/docs/customization.rst index 04373c45..e334bf1b 100644 --- a/docs/customization.rst +++ b/docs/customization.rst @@ -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 --------------------------- diff --git a/drf_spectacular/generators.py b/drf_spectacular/generators.py index 15f7ba9a..198e1ce6 100644 --- a/drf_spectacular/generators.py +++ b/drf_spectacular/generators.py @@ -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 @@ -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) diff --git a/drf_spectacular/hooks.py b/drf_spectacular/hooks.py index c4d2f8a7..f66ddbc2 100644 --- a/drf_spectacular/hooks.py +++ b/drf_spectacular/hooks.py @@ -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 @@ -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 diff --git a/drf_spectacular/settings.py b/drf_spectacular/settings.py index 9f46c6ef..f6e089f5 100644 --- a/drf_spectacular/settings.py +++ b/drf_spectacular/settings.py @@ -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.