Skip to content

Commit f74d3bf

Browse files
Lucidiotcarltongibson
authored andcommitted
Fix nested serializer schema rendering
Add a nested serializer test case
1 parent c49bb59 commit f74d3bf

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

rest_framework/schemas/openapi.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,9 @@ def _map_field(self, field):
214214
'items': self._map_serializer(field.child)
215215
}
216216
if isinstance(field, serializers.Serializer):
217-
return {
218-
'type': 'object',
219-
'properties': self._map_serializer(field)
220-
}
217+
data = self._map_serializer(field)
218+
data['type'] = 'object'
219+
return data
221220

222221
# Related fields.
223222
if isinstance(field, serializers.ManyRelatedField):

tests/schemas/test_openapi.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,36 @@ class View(generics.GenericAPIView):
130130
assert responses['200']['content']['application/json']['schema']['required'] == ['text']
131131
assert list(responses['200']['content']['application/json']['schema']['properties'].keys()) == ['text']
132132

133+
def test_response_body_nested_serializer(self):
134+
path = '/'
135+
method = 'POST'
136+
137+
class NestedSerializer(serializers.Serializer):
138+
number = serializers.IntegerField()
139+
140+
class Serializer(serializers.Serializer):
141+
text = serializers.CharField()
142+
nested = NestedSerializer()
143+
144+
class View(generics.GenericAPIView):
145+
serializer_class = Serializer
146+
147+
view = create_view(
148+
View,
149+
method,
150+
create_request(path),
151+
)
152+
inspector = AutoSchema()
153+
inspector.view = view
154+
155+
responses = inspector._get_responses(path, method)
156+
schema = responses['200']['content']['application/json']['schema']
157+
assert sorted(schema['required']) == ['nested', 'text']
158+
assert sorted(list(schema['properties'].keys())) == ['nested', 'text']
159+
assert schema['properties']['nested']['type'] == 'object'
160+
assert list(schema['properties']['nested']['properties'].keys()) == ['number']
161+
assert schema['properties']['nested']['required'] == ['number']
162+
133163

134164
@pytest.mark.skipif(uritemplate is None, reason='uritemplate not installed.')
135165
@override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.openapi.AutoSchema'})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy