Skip to content

Commit

Permalink
add setting for operation parameter sorting #281
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Feb 10, 2021
1 parent 17577cf commit a6419bc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion drf_spectacular/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,12 @@ def dict_helper(parameters):
else:
parameters[key] = parameter

return sorted(parameters.values(), key=lambda p: p['name'])
if callable(spectacular_settings.SORT_OPERATION_PARAMETERS):
return sorted(parameters.values(), key=spectacular_settings.SORT_OPERATION_PARAMETERS)
elif spectacular_settings.SORT_OPERATION_PARAMETERS:
return sorted(parameters.values(), key=lambda p: p['name'])
else:
return list(parameters.values())

def get_description(self):
""" override this for custom behaviour """
Expand Down
5 changes: 5 additions & 0 deletions drf_spectacular/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
# allowed values are 'dict', 'bool', None
'GENERIC_ADDITIONAL_PROPERTIES': 'dict',

# Determines whether operation parameters should be sorted alphanumerically or just in
# the order they arrived. Accepts either True, False, or a callable for sort's key arg.
'SORT_OPERATION_PARAMETERS': True,

# General schema metadata. Refer to spec for valid inputs
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#openapi-object
'TITLE': '',
Expand Down Expand Up @@ -118,6 +122,7 @@
'PREPROCESSING_HOOKS',
'GET_LIB_DOC_EXCLUDES',
'GET_MOCK_REQUEST',
'SORT_OPERATION_PARAMETERS',
]

spectacular_settings = APISettings(
Expand Down
22 changes: 22 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1368,3 +1368,25 @@ class ReferencingModelViewset(viewsets.ModelViewSet):
assert properties['referenced_model_m2m']['items']['type'] == 'integer'
assert properties['referenced_model_m2m_ro']['items']['type'] == 'integer'
assert properties['indirect_referenced_model_ro']['type'] == 'integer'


@pytest.mark.parametrize(['sorting', 'result'], [
(True, ['a', 'b', 'c']),
(False, ['b', 'c', 'a']),
(lambda x: (x['in'], x['name']), ['b', 'a', 'c']),
])
def test_parameter_sorting_setting(no_warnings, sorting, result):
@extend_schema(
parameters=[OpenApiParameter('b', str, 'header'), OpenApiParameter('c'), OpenApiParameter('a')],
responses=OpenApiTypes.FLOAT
)
@api_view(['GET'])
def view_func(request, format=None):
pass # pragma: no cover

with mock.patch(
'drf_spectacular.settings.spectacular_settings.SORT_OPERATION_PARAMETERS', sorting
):
schema = generate_schema('/x/', view_function=view_func)
parameters = schema['paths']['/x/']['get']['parameters']
assert [p['name'] for p in parameters] == result

0 comments on commit a6419bc

Please sign in to comment.