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 36c92a3

Browse files
authored
Avoided that an empty attributes dict is rendered (#1196)
1 parent 4d4dc37 commit 36c92a3

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ any parts of the framework not mentioned in the documentation should generally b
1919

2020
* Fixed OpenAPI schema generation for `Serializer` when used inside another `Serializer` or as a child of `ListField`.
2121
* `ModelSerializer` fields are now returned in the same order than DRF
22+
* Avoided that an empty attributes dict is rendered in case serializer does not
23+
provide any attribute fields.
2224

2325
### Removed
2426

‎example/tests/unit/test_renderers.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class WriteonlyTestSerializer(serializers.ModelSerializer):
126126

127127
class Meta:
128128
model = Entry
129-
fields = ("comments", "rating")
129+
fields = ("headline", "comments", "rating")
130130

131131
class WriteOnlyDummyTestViewSet(views.ReadOnlyModelViewSet):
132132
queryset = Entry.objects.all()
@@ -136,6 +136,7 @@ class WriteOnlyDummyTestViewSet(views.ReadOnlyModelViewSet):
136136
result = json.loads(rendered.decode())
137137

138138
assert "rating" not in result["data"]["attributes"]
139+
assert "headline" in result["data"]["attributes"]
139140
assert "relationships" not in result["data"]
140141

141142

@@ -153,6 +154,7 @@ class EmptyRelationshipViewSet(views.ReadOnlyModelViewSet):
153154

154155
rendered = render_dummy_test_serialized_view(EmptyRelationshipViewSet, Author())
155156
result = json.loads(rendered.decode())
157+
assert "attributes" not in result["data"]
156158
assert "relationships" in result["data"]
157159
assert "bio" in result["data"]["relationships"]
158160
assert result["data"]["relationships"]["bio"] == {"data": None}

‎rest_framework_json_api/renderers.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,10 @@ def build_json_resource_obj(
452452
resource_data = {
453453
"type": resource_name,
454454
"id": utils.get_resource_id(resource_instance, resource),
455-
"attributes": cls.extract_attributes(fields, resource),
456455
}
456+
attributes = cls.extract_attributes(fields, resource)
457+
if attributes:
458+
resource_data["attributes"] = attributes
457459
relationships = cls.extract_relationships(fields, resource, resource_instance)
458460
if relationships:
459461
resource_data["relationships"] = relationships

0 commit comments

Comments
(0)

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