Skip to content

Add operationId on OpenAPI operations #6549

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions rest_framework/schemas/inspectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,19 @@ def __get__(self, instance, owner):
class OpenAPIAutoSchema(ViewInspector):

content_types = ['application/json']
method_mapping = {
'get': 'Retrieve',
'post': 'Create',
'put': 'Update',
'patch': 'PartialUpdate',
'delete': 'Destroy',
}

def get_operation(self, path, method):
operation = {}

operation['operationId'] = self._get_operation_id(path, method)

parameters = []
parameters += self._get_path_parameters(path, method)
parameters += self._get_pagination_parameters(path, method)
Expand All @@ -528,6 +537,45 @@ def get_operation(self, path, method):

return operation

def _get_operation_id(self, path, method):
"""
Compute an operation ID from the model, serializer or view name.
"""
# TODO: Allow an attribute/method on the view to change that ID?
# Avoid cyclic imports
from rest_framework.generics import GenericAPIView

if is_list_view(path, method, self.view):
action = 'List'
else:
action = self.method_mapping[method.lower()]

# Try to deduce the ID from the view's model
model = getattr(getattr(self.view, 'queryset', None), 'model', None)
if model is not None:
name = model.__name__

# Try with the serializer class name
elif isinstance(self.view, GenericAPIView):
name = self.view.get_serializer_class().__name__
if name.endswith('Serializer'):
name = name[:-10]

# Fallback to the view name
else:
name = self.view.__class__.__name__
if name.endswith('APIView'):
name = name[:-7]
elif name.endswith('View'):
name = name[:-4]
if name.endswith(action): # ListView, UpdateAPIView, ThingDelete ...
name = name[:-len(action)]

if action == 'List' and not name.endswith('s'): # ListThings instead of ListThing
name += 's'

return action + name

def _get_path_parameters(self, path, method):
"""
Return a list of parameters from templated path variables.
Expand Down
1 change: 1 addition & 0 deletions tests/schemas/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_path_without_parameters(self):

operation = inspector.get_operation(path, method)
assert operation == {
'operationId': 'ListExamples',
'parameters': [],
'responses': {'200': {'content': {'application/json': {}}}},
}
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy