Skip to content

Commit

Permalink
change operation sorting to alphanumeric with option (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Mar 12, 2020
1 parent 6f7800f commit a791ae7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions drf_spectacular/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'drf_spectacular.auth.TokenAuthenticationScheme',
],
'SCHEMA_PATH_PREFIX': r'',
'OPERATION_SORTER': 'alpha'
}

IMPORT_STRINGS = [
Expand Down
28 changes: 17 additions & 11 deletions drf_spectacular/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
from rest_framework.schemas.utils import get_pk_description, is_list_view

from drf_spectacular.app_settings import spectacular_settings
from drf_spectacular.plumbing import resolve_basic_type, warn, anyisinstance, force_instance, is_serializer, follow_field_source, is_field, is_basic_type
from drf_spectacular.plumbing import (
resolve_basic_type, warn, anyisinstance, force_instance, is_serializer,
follow_field_source, is_field, is_basic_type, alpha_operation_sorter,
)
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import PolymorphicProxySerializer

Expand Down Expand Up @@ -66,17 +69,22 @@ def create_view(self, callback, method, request=None):

return view

def parse(self, request=None):
result = {}
def get_endpoints(self, request):
""" sorted endpoints by operation """
self._initialise_endpoints()
_, endpoints = self._get_paths_and_endpoints(request)

paths, view_endpoints = self._get_paths_and_endpoints(request)
if spectacular_settings.OPERATION_SORTER == 'alpha':
return sorted(endpoints, key=alpha_operation_sorter)
else:
# default to DRF method sorting
return endpoints

# Only generate the path prefix for paths that will be included
if not paths:
return None
def parse(self, request=None):
""" Iterate endpoints generating per method path operations. """
result = {}

# Iterate endpoints generating per method path operations.
for path, method, view in view_endpoints:
for path, method, view in self.get_endpoints(request):
if not self.has_view_permissions(path, method, view):
continue
# keep reference to schema as every access yields a fresh object (descriptor pattern)
Expand All @@ -101,8 +109,6 @@ def get_schema(self, request=None, public=False):
"""
Generate a OpenAPI schema.
"""
self._initialise_endpoints()

schema = {
'openapi': '3.0.3',
'servers': [
Expand Down
13 changes: 13 additions & 0 deletions drf_spectacular/plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,16 @@ def dummy_property(obj) -> str:
pass

return dummy_property


def alpha_operation_sorter(endpoint):
""" sort endpoints first alphanumerically by path, then by method order """
path, method, callback = endpoint
method_priority = {
'GET': 0,
'POST': 1,
'PUT': 2,
'PATCH': 3,
'DELETE': 4
}.get(method, 5)
return (path, method_priority)

0 comments on commit a791ae7

Please sign in to comment.