Skip to content

Commit

Permalink
Fix sorting of endpoints with params
Browse files Browse the repository at this point in the history
Sort foo{arg} after foo/, but before foo/bar.

Related to tfranzel#110
  • Loading branch information
jayvdb committed Jul 14, 2020
1 parent c82d81a commit d1b3676
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
6 changes: 6 additions & 0 deletions drf_spectacular/plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ def alpha_operation_sorter(endpoint):
'PATCH': 3,
'DELETE': 4
}.get(method, 5)

# Sort foo{arg} after foo/, but before foo/bar
if path.endswith('/'):
path = path[:-1] + ' '
path = path.replace('{', '!')

return path, method_priority


Expand Down
38 changes: 19 additions & 19 deletions tests/test_extend_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ paths:
schema:
$ref: '#/components/schemas/ErrorDetail'
description: ''
/doesitall/{id}/subscribe/:
post:
operationId: doesitall_subscribe_create
description: ''
parameters:
- in: path
name: id
schema:
type: integer
required: true
tags:
- doesitall
security:
- cookieAuth: []
- basicAuth: []
- {}
responses:
'201':
description: No response body
/doesitall/callback/{ephemeral}/:
post:
operationId: doesitall_callback_create
Expand Down Expand Up @@ -217,25 +236,6 @@ paths:
schema:
$ref: '#/components/schemas/Alpha'
description: ''
/doesitall/{id}/subscribe/:
post:
operationId: doesitall_subscribe_create
description: ''
parameters:
- in: path
name: id
schema:
type: integer
required: true
tags:
- doesitall
security:
- cookieAuth: []
- basicAuth: []
- {}
responses:
'201':
description: No response body
components:
schemas:
Alpha:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,13 @@ def view_func(request, format=None):
assert list(schema['paths'].keys()) == [
'/pi',
'/pi/',
'/pi{format}',
'/pi/subpath',
'/pi/subpath{format}',
'/pick',
'/pick{format}',
'/pi{format}',
]

format_parameter = schema['paths']['/pi{format}']['get']['parameters'][0]
assert format_parameter['name'] == 'format'
assert format_parameter['required'] is True
Expand Down

0 comments on commit d1b3676

Please sign in to comment.