Skip to content

Commit 735ce29

Browse files
authored
Avoided duplicated sort query parameter in schema generation (django-json-api#1124)
1 parent a4b1b25 commit 735ce29

File tree

6 files changed

+12
-25
lines changed

6 files changed

+12
-25
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Jonas Kiefer <https://github.com/jokiefer>
2121
Jonas Metzener <[email protected]>
2222
Jonathan Senecal <[email protected]>
2323
Joseba Mendivil <[email protected]>
24+
2425
Kevin Partington <[email protected]>
2526
Kieran Evans <[email protected]>
2627

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ any parts of the framework not mentioned in the documentation should generally b
1818

1919
* Added support to overwrite serializer methods in customized schema class
2020

21+
### Fixed
22+
23+
* Refactored handling of the `sort` query parameter to fix duplicate declaration in the generated schema definition
24+
2125
## [6.0.0] - 2022-09-24
2226

2327
### Fixed

example/tests/__snapshots__/test_openapi.ambr

+2-8
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,7 @@
273273
"$ref": "#/components/parameters/fields"
274274
},
275275
{
276-
"$ref": "#/components/parameters/sort"
277-
},
278-
{
279-
"description": "Which field to use when ordering the results.",
276+
"description": "[list of fields to sort by](https://jsonapi.org/format/#fetching-sorting)",
280277
"in": "query",
281278
"name": "sort",
282279
"required": false,
@@ -391,9 +388,6 @@
391388
{
392389
"$ref": "#/components/parameters/fields"
393390
},
394-
{
395-
"$ref": "#/components/parameters/sort"
396-
},
397391
{
398392
"description": "A page number within the paginated result set.",
399393
"in": "query",
@@ -413,7 +407,7 @@
413407
}
414408
},
415409
{
416-
"description": "Which field to use when ordering the results.",
410+
"description": "[list of fields to sort by](https://jsonapi.org/format/#fetching-sorting)",
417411
"in": "query",
418412
"name": "sort",
419413
"required": false,

example/tests/unit/test_filter_schema_params.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ def test_filters_get_schema_params():
7272
"name": "sort",
7373
"required": False,
7474
"in": "query",
75-
"description": "Which field to use when ordering the results.",
75+
"description": "[list of fields to sort by]"
76+
"(https://jsonapi.org/format/#fetching-sorting)",
7677
"schema": {"type": "string"},
7778
}
7879
],

rest_framework_json_api/filters.py

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class OrderingFilter(OrderingFilter):
2222
#: override :py:attr:`rest_framework.filters.OrderingFilter.ordering_param`
2323
#: with JSON:API-compliant query parameter name.
2424
ordering_param = "sort"
25+
ordering_description = (
26+
"[list of fields to sort by]" "(https://jsonapi.org/format/#fetching-sorting)"
27+
)
2528

2629
def remove_invalid_fields(self, queryset, fields, view, request):
2730
"""

rest_framework_json_api/schemas/openapi.py

-16
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,6 @@ class SchemaGenerator(drf_openapi.SchemaGenerator):
248248
},
249249
"explode": True,
250250
},
251-
"sort": {
252-
"name": "sort",
253-
"in": "query",
254-
"description": "[list of fields to sort by]"
255-
"(https://jsonapi.org/format/#fetching-sorting)",
256-
"required": False,
257-
"style": "form",
258-
"schema": {"type": "string"},
259-
},
260251
},
261252
}
262253

@@ -422,7 +413,6 @@ def get_operation(self, path, method):
422413
if method in ["GET", "HEAD"]:
423414
parameters += self._get_include_parameters(path, method)
424415
parameters += self._get_fields_parameters(path, method)
425-
parameters += self._get_sort_parameters(path, method)
426416
parameters += self.get_pagination_parameters(path, method)
427417
parameters += self.get_filter_parameters(path, method)
428418
operation["parameters"] = parameters
@@ -485,12 +475,6 @@ def _get_fields_parameters(self, path, method):
485475
# explode: true
486476
return [{"$ref": "#/components/parameters/fields"}]
487477

488-
def _get_sort_parameters(self, path, method):
489-
"""
490-
sort parameter: https://jsonapi.org/format/#fetching-sorting
491-
"""
492-
return [{"$ref": "#/components/parameters/sort"}]
493-
494478
def _add_get_collection_response(self, operation, path):
495479
"""
496480
Add GET 200 response for a collection to operation

0 commit comments

Comments
 (0)