Skip to content

Commit 480f486

Browse files
committed
Fix OpenAPI operation name plural appropriately
1 parent 9d149f2 commit 480f486

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.3.0,<2.4
6+
inflection==0.5.1
67
markdown==3.3;python_version>="3.6"
78
markdown==3.2.2;python_version=="3.5"
89
psycopg2-binary>=2.8.5,<2.9

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
RemovedInDRF314Warning, 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
@@ -676,6 +676,21 @@ def test_operation_id_custom_name(self):
676676
operationId = inspector.get_operation_id(path, method)
677677
assert operationId == 'listUlysses'
678678

679+
def test_operation_id_plural(self):
680+
path = '/'
681+
method = 'GET'
682+
683+
view = create_view(
684+
views.ExampleGenericAPIView,
685+
method,
686+
create_request(path),
687+
)
688+
inspector = AutoSchema(operation_id_base='City')
689+
inspector.view = view
690+
691+
operationId = inspector.get_operation_id(path, method)
692+
assert operationId == 'listCities'
693+
679694
def test_operation_id_override_get(self):
680695
class CustomSchema(AutoSchema):
681696
def get_operation_id(self, path, method):

0 commit comments

Comments
 (0)