Skip to content

Commit 806b0b3

Browse files
committed
Include nested objects in metadata for OPTIONS. Closes #3156. Closes #2915.
1 parent 81709a2 commit 806b0b3

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

rest_framework/metadata.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ def get_field_info(self, field):
133133

134134
if getattr(field, 'child', None):
135135
field_info['child'] = self.get_field_info(field.child)
136+
elif getattr(field, 'fields', None):
137+
field_info['children'] = self.get_serializer_info(field)
136138

137139
if not field_info.get('read_only') and hasattr(field, 'choices'):
138140
field_info['choices'] = [

runtests.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pytest
99

1010
PYTEST_ARGS = {
11-
'default': ['tests', '--tb=short', '-s', '--cov', 'rest_framework'],
11+
'default': ['tests', '--tb=short', '-s'],
1212
'fast': ['tests', '--tb=short', '-q', '-s'],
1313
}
1414

@@ -87,6 +87,14 @@ def is_class(string):
8787
if len(sys.argv) > 1:
8888
pytest_args = sys.argv[1:]
8989
first_arg = pytest_args[0]
90+
91+
try:
92+
pytest_args.remove('--coverage')
93+
except ValueError:
94+
pass
95+
else:
96+
pytest_args = ['--cov', 'rest_framework'] + pytest_args
97+
9098
if first_arg.startswith('-'):
9199
# `runtests.py [flags]`
92100
pytest_args = ['tests'] + pytest_args

tests/test_metadata.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def test_actions(self):
5959
On generic views OPTIONS should return an 'actions' key with metadata
6060
on the fields that may be supplied to PUT and POST requests.
6161
"""
62+
class NestedField(serializers.Serializer):
63+
a = serializers.IntegerField()
64+
b = serializers.IntegerField()
65+
6266
class ExampleSerializer(serializers.Serializer):
6367
choice_field = serializers.ChoiceField(['red', 'green', 'blue'])
6468
integer_field = serializers.IntegerField(
@@ -72,6 +76,7 @@ class ExampleSerializer(serializers.Serializer):
7276
child=serializers.IntegerField()
7377
)
7478
)
79+
nested_field = NestedField()
7580

7681
class ExampleView(views.APIView):
7782
"""Example view."""
@@ -140,6 +145,26 @@ def get_serializer(self):
140145
'read_only': False
141146
}
142147
}
148+
},
149+
'nested_field': {
150+
'type': 'nested object',
151+
'required': True,
152+
'read_only': False,
153+
'label': 'Nested field',
154+
'children': {
155+
'a': {
156+
'type': 'integer',
157+
'required': True,
158+
'read_only': False,
159+
'label': 'A'
160+
},
161+
'b': {
162+
'type': 'integer',
163+
'required': True,
164+
'read_only': False,
165+
'label': 'B'
166+
}
167+
}
143168
}
144169
}
145170
}

0 commit comments

Comments
 (0)