Skip to content

Commit

Permalink
Make inflection package truly optional (#9303)
Browse files Browse the repository at this point in the history
* Make inflection package truly optional

Fix #9291

* Make inflection compat layer consistent with the others

Co-authored-by: T. Franzel <13507857+tfranzel@users.noreply.github.com>

---------

Co-authored-by: T. Franzel <13507857+tfranzel@users.noreply.github.com>
  • Loading branch information
browniebroke and tfranzel authored Mar 18, 2024
1 parent 337ba21 commit 2f66c29
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/api-guide/schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def unicode_http_header(value):
except ImportError:
yaml = None

# inflection is optional
try:
import inflection
except ImportError:
inflection = None


# requests is optional
try:
Expand Down
7 changes: 3 additions & 4 deletions rest_framework/schemas/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from rest_framework import (
RemovedInDRF315Warning, exceptions, renderers, serializers
)
from rest_framework.compat import uritemplate
from rest_framework.compat import inflection, uritemplate
from rest_framework.fields import _UnvalidatedField, empty
from rest_framework.settings import api_settings

Expand Down Expand Up @@ -247,9 +247,8 @@ def get_operation_id_base(self, path, method, action):
name = name[:-len(action)]

if action == 'list':
from inflection import pluralize

name = pluralize(name)
assert inflection, '`inflection` must be installed for OpenAPI schema support.'
name = inflection.pluralize(name)

return name

Expand Down

0 comments on commit 2f66c29

Please sign in to comment.