Skip to content

Commit 66d2de4

Browse files
authored
Omit id field from OpenAPI schema attributes (#1156)
1 parent a7c1a00 commit 66d2de4

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ any parts of the framework not mentioned in the documentation should generally b
2727

2828
* Refactored handling of the `sort` query parameter to fix duplicate declaration in the generated schema definition
2929
* Non-field serializer errors are given a source.pointer value of "/data".
30+
* Fixed "id" field being added to /data/attributes in the OpenAPI schema when it is not rendered there.
3031

3132
## [6.0.0] - 2022-09-24
3233

example/tests/test_openapi.py

+15
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ def test_schema_construction(snapshot):
110110
assert snapshot == json.dumps(schema, indent=2, sort_keys=True)
111111

112112

113+
def test_schema_id_field():
114+
"""ID field is only included in the root, not the attributes."""
115+
patterns = [
116+
re_path("^companies/?$", views.CompanyViewset.as_view({"get": "list"})),
117+
]
118+
generator = SchemaGenerator(patterns=patterns)
119+
120+
request = create_request("/")
121+
schema = generator.get_schema(request=request)
122+
123+
company_properties = schema["components"]["schemas"]["Company"]["properties"]
124+
assert company_properties["id"] == {"$ref": "#/components/schemas/id"}
125+
assert "id" not in company_properties["attributes"]["properties"]
126+
127+
113128
def test_schema_parameters_include():
114129
"""Include paramater is only used when serializer defines included_serializers."""
115130
patterns = [

rest_framework_json_api/schemas/openapi.py

+4
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,10 @@ def map_serializer(self, serializer):
670670
"$ref": "#/components/schemas/reltomany"
671671
}
672672
continue
673+
if field.field_name == "id":
674+
# ID is always provided in the root of JSON:API and removed from the
675+
# attributes in JSONRenderer.
676+
continue
673677

674678
if field.required:
675679
required.append(format_field_name(field.field_name))

0 commit comments

Comments
 (0)