Fix OpenAPI operation name plural appropriately (#8017)

This commit is contained in:
Shinya Ohyanagi 2022-11-24 16:48:05 +09:00 committed by GitHub
parent ebde56b932
commit 9e328a9549
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 2 deletions

View File

@ -3,6 +3,7 @@ coreapi==2.3.1
coreschema==0.0.4 coreschema==0.0.4
django-filter>=2.4.0,<3.0 django-filter>=2.4.0,<3.0
django-guardian>=2.4.0,<2.5 django-guardian>=2.4.0,<2.5
inflection==0.5.1
markdown==3.3 markdown==3.3
psycopg2-binary>=2.9.5,<2.10 psycopg2-binary>=2.9.5,<2.10
pygments==2.12 pygments==2.12

View File

@ -11,6 +11,7 @@ from django.core.validators import (
) )
from django.db import models from django.db import models
from django.utils.encoding import force_str from django.utils.encoding import force_str
from inflection import pluralize
from rest_framework import ( from rest_framework import (
RemovedInDRF315Warning, exceptions, renderers, serializers RemovedInDRF315Warning, exceptions, renderers, serializers
@ -247,8 +248,8 @@ class AutoSchema(ViewInspector):
if name.endswith(action.title()): # ListView, UpdateAPIView, ThingDelete ... if name.endswith(action.title()): # ListView, UpdateAPIView, ThingDelete ...
name = name[:-len(action)] name = name[:-len(action)]
if action == 'list' and not name.endswith('s'): # listThings instead of listThing if action == 'list':
name += 's' name = pluralize(name)
return name return name

View File

@ -715,6 +715,21 @@ class TestOperationIntrospection(TestCase):
operationId = inspector.get_operation_id(path, method) operationId = inspector.get_operation_id(path, method)
assert operationId == 'listUlysses' 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): def test_operation_id_override_get(self):
class CustomSchema(AutoSchema): class CustomSchema(AutoSchema):
def get_operation_id(self, path, method): def get_operation_id(self, path, method):