Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OpenAPI operation name plural appropriately #8017

Merged
merged 2 commits into from Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements/requirements-optionals.txt
Expand Up @@ -3,6 +3,7 @@ coreapi==2.3.1
coreschema==0.0.4
django-filter>=2.4.0,<3.0
django-guardian>=2.4.0,<2.5
inflection==0.5.1
markdown==3.3
psycopg2-binary>=2.8.5,<2.9
pygments==2.12
Expand Down
5 changes: 3 additions & 2 deletions rest_framework/schemas/openapi.py
Expand Up @@ -11,6 +11,7 @@
)
from django.db import models
from django.utils.encoding import force_str
from inflection import pluralize

from rest_framework import (
RemovedInDRF314Warning, exceptions, renderers, serializers
Expand Down Expand Up @@ -247,8 +248,8 @@ def get_operation_id_base(self, path, method, action):
if name.endswith(action.title()): # ListView, UpdateAPIView, ThingDelete ...
name = name[:-len(action)]

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

return name

Expand Down
15 changes: 15 additions & 0 deletions tests/schemas/test_openapi.py
Expand Up @@ -695,6 +695,21 @@ def test_operation_id_custom_name(self):
operationId = inspector.get_operation_id(path, method)
assert operationId == 'listUlysses'

def test_operation_id_plural(self):
path = '/'
method = 'GET'

view = create_view(
views.ExampleGenericAPIView,
method,
create_request(path),
)
inspector = AutoSchema(operation_id_base='City')
inspector.view = view

operationId = inspector.get_operation_id(path, method)
assert operationId == 'listCities'

def test_operation_id_override_get(self):
class CustomSchema(AutoSchema):
def get_operation_id(self, path, method):
Expand Down