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 11, 2020
1 parent 789a062 commit a4dfe49
Show file tree
Hide file tree
Showing 3 changed files with 28 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
4 changes: 3 additions & 1 deletion tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,18 @@ def similar(request, format=None):
generator = SchemaGenerator(patterns=urlpatterns)
schema = generator.get_schema(request=None, public=True)
validate_schema(schema)

assert len(schema['paths'].keys()) == 7
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 a4dfe49

Please sign in to comment.