Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a7c1a00

Browse files
Omit include parameter from OpenAPI schema when left unused (#1157)
1 parent ac8ffe2 commit a7c1a00

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ any parts of the framework not mentioned in the documentation should generally b
2020
* Added support to overwrite serializer methods in customized schema class
2121
* Adjusted some still old formatted strings to f-strings.
2222
* Replaced `OrderedDict` with `dict` which is also ordered since Python 3.7.
23+
* Compound document "include" parameter is only included in the OpenAPI schema if serializer
24+
implements `included_serializers`.
2325

2426
### Fixed
2527

‎example/tests/test_openapi.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,22 @@ def test_schema_construction(snapshot):
110110
assert snapshot == json.dumps(schema, indent=2, sort_keys=True)
111111

112112

113+
def test_schema_parameters_include():
114+
"""Include paramater is only used when serializer defines included_serializers."""
115+
patterns = [
116+
re_path("^authors/?$", views.AuthorViewSet.as_view({"get": "list"})),
117+
re_path("^project-types/?$", views.ProjectTypeViewset.as_view({"get": "list"})),
118+
]
119+
generator = SchemaGenerator(patterns=patterns)
120+
121+
request = create_request("/")
122+
schema = generator.get_schema(request=request)
123+
124+
include_ref = {"$ref": "#/components/parameters/include"}
125+
assert include_ref in schema["paths"]["/authors/"]["get"]["parameters"]
126+
assert include_ref not in schema["paths"]["/project-types/"]["get"]["parameters"]
127+
128+
113129
def test_schema_related_serializers():
114130
"""
115131
Confirm that paths are generated for related fields. For example:

‎rest_framework_json_api/schemas/openapi.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,13 @@ def get_operation(self, path, method):
406406
operation["operationId"] = self.get_operation_id(path, method)
407407
operation["description"] = self.get_description(path, method)
408408

409+
serializer = self.get_response_serializer(path, method)
410+
409411
parameters = []
410412
parameters += self.get_path_parameters(path, method)
411413
# pagination, filters only apply to GET/HEAD of collections and items
412414
if method in ["GET", "HEAD"]:
413-
parameters += self._get_include_parameters(path, method)
415+
parameters += self._get_include_parameters(path, method, serializer)
414416
parameters += self._get_fields_parameters(path, method)
415417
parameters += self.get_pagination_parameters(path, method)
416418
parameters += self.get_filter_parameters(path, method)
@@ -448,11 +450,13 @@ def get_operation_id(self, path, method):
448450
action = self.method_mapping[method.lower()]
449451
return action + path
450452

451-
def _get_include_parameters(self, path, method):
453+
def _get_include_parameters(self, path, method, serializer):
452454
"""
453455
includes parameter: https://jsonapi.org/format/#fetching-includes
454456
"""
455-
return [{"$ref": "#/components/parameters/include"}]
457+
if getattr(serializer, "included_serializers", {}):
458+
return [{"$ref": "#/components/parameters/include"}]
459+
return []
456460

457461
def _get_fields_parameters(self, path, method):
458462
"""

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /