Skip to content

Commit 9e328a9

Browse files
authored
Fix OpenAPI operation name plural appropriately (#8017)
1 parent ebde56b commit 9e328a9

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

requirements/requirements-optionals.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ coreapi==2.3.1
33
coreschema==0.0.4
44
django-filter>=2.4.0,<3.0
55
django-guardian>=2.4.0,<2.5
6+
inflection==0.5.1
67
markdown==3.3
78
psycopg2-binary>=2.9.5,<2.10
89
pygments==2.12

rest_framework/schemas/openapi.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
)
1212
from django.db import models
1313
from django.utils.encoding import force_str
14+
from inflection import pluralize
1415

1516
from rest_framework import (
1617
RemovedInDRF315Warning, exceptions, renderers, serializers
@@ -247,8 +248,8 @@ def get_operation_id_base(self, path, method, action):
247248
if name.endswith(action.title()): # ListView, UpdateAPIView, ThingDelete ...
248249
name = name[:-len(action)]
249250

250-
if action == 'list' and not name.endswith('s'): # listThings instead of listThing
251-
name += 's'
251+
if action == 'list':
252+
name = pluralize(name)
252253

253254
return name
254255

tests/schemas/test_openapi.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,21 @@ def test_operation_id_custom_name(self):
715715
operationId = inspector.get_operation_id(path, method)
716716
assert operationId == 'listUlysses'
717717

718+
def test_operation_id_plural(self):
719+
path = '/'
720+
method = 'GET'
721+
722+
view = create_view(
723+
views.ExampleGenericAPIView,
724+
method,
725+
create_request(path),
726+
)
727+
inspector = AutoSchema(operation_id_base='City')
728+
inspector.view = view
729+
730+
operationId = inspector.get_operation_id(path, method)
731+
assert operationId == 'listCities'
732+
718733
def test_operation_id_override_get(self):
719734
class CustomSchema(AutoSchema):
720735
def get_operation_id(self, path, method):

0 commit comments

Comments
 (0)