From 4dcfc8c6478a30764295a32c1d760a99bc94dbf9 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Mon, 18 Mar 2024 18:57:13 +0000 Subject: [PATCH 1/2] Make inflection package truly optional Fix #9291 --- docs/api-guide/schemas.md | 3 ++- rest_framework/compat.py | 9 +++++++++ rest_framework/schemas/openapi.py | 4 +--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/api-guide/schemas.md b/docs/api-guide/schemas.md index 7af98dbf5e..c387af9727 100644 --- a/docs/api-guide/schemas.md +++ b/docs/api-guide/schemas.md @@ -56,10 +56,11 @@ The following sections explain more. ### Install dependencies - pip install pyyaml uritemplate + pip install pyyaml uritemplate inflection * `pyyaml` is used to generate schema into YAML-based OpenAPI format. * `uritemplate` is used internally to get parameters in path. +* `inflection` is used to pluralize operations more appropriately in the list endpoints. ### Generating a static schema with the `generateschema` management command diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 472b8ad244..96349aca05 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -46,6 +46,15 @@ def unicode_http_header(value): except ImportError: yaml = None +# inflection is optional +try: + from inflection import pluralize +except ImportError: + def pluralize(text): + if not text.endswith('s'): + text += "s" + return text + # requests is optional try: diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index c154494e2e..5a1f64b891 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -14,7 +14,7 @@ from rest_framework import ( RemovedInDRF315Warning, exceptions, renderers, serializers ) -from rest_framework.compat import uritemplate +from rest_framework.compat import pluralize, uritemplate from rest_framework.fields import _UnvalidatedField, empty from rest_framework.settings import api_settings @@ -247,8 +247,6 @@ def get_operation_id_base(self, path, method, action): name = name[:-len(action)] if action == 'list': - from inflection import pluralize - name = pluralize(name) return name From 492529006d531b828ae1ca9232e95a5fbb050b32 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Mon, 18 Mar 2024 22:14:05 +0000 Subject: [PATCH 2/2] Make inflection compat layer consistent with the others Co-authored-by: T. Franzel <13507857+tfranzel@users.noreply.github.com> --- rest_framework/compat.py | 7 ++----- rest_framework/schemas/openapi.py | 5 +++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 96349aca05..afc06b6cb7 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -48,12 +48,9 @@ def unicode_http_header(value): # inflection is optional try: - from inflection import pluralize + import inflection except ImportError: - def pluralize(text): - if not text.endswith('s'): - text += "s" - return text + inflection = None # requests is optional diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index 5a1f64b891..38031e646d 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -14,7 +14,7 @@ from rest_framework import ( RemovedInDRF315Warning, exceptions, renderers, serializers ) -from rest_framework.compat import pluralize, uritemplate +from rest_framework.compat import inflection, uritemplate from rest_framework.fields import _UnvalidatedField, empty from rest_framework.settings import api_settings @@ -247,7 +247,8 @@ def get_operation_id_base(self, path, method, action): name = name[:-len(action)] if action == 'list': - name = pluralize(name) + assert inflection, '`inflection` must be installed for OpenAPI schema support.' + name = inflection.pluralize(name) return name