Don't show hidden fields in metadata (#5854)

HiddenField is meant to be hidden, but we discovered it showing up in OPTIONS requests
This commit is contained in:
Craig de Stigter 2018-03-24 11:47:10 +13:00 committed by Tom Christie
parent a7e2a7bfcd
commit a5072778e9
2 changed files with 22 additions and 0 deletions

View File

@ -111,6 +111,7 @@ class SimpleMetadata(BaseMetadata):
return OrderedDict([ return OrderedDict([
(field_name, self.get_field_info(field)) (field_name, self.get_field_info(field))
for field_name, field in serializer.fields.items() for field_name, field in serializer.fields.items()
if not isinstance(field, serializers.HiddenField)
]) ])
def get_field_info(self, field): def get_field_info(self, field):

View File

@ -275,6 +275,27 @@ class TestMetadata:
view = ExampleView.as_view(versioning_class=scheme) view = ExampleView.as_view(versioning_class=scheme)
view(request=request) view(request=request)
def test_dont_show_hidden_fields(self):
"""
HiddenField shouldn't show up in SimpleMetadata at all.
"""
class ExampleSerializer(serializers.Serializer):
integer_field = serializers.IntegerField(max_value=10)
hidden_field = serializers.HiddenField(default=1)
class ExampleView(views.APIView):
"""Example view."""
def post(self, request):
pass
def get_serializer(self):
return ExampleSerializer()
view = ExampleView.as_view()
response = view(request=request)
assert response.status_code == status.HTTP_200_OK
assert set(response.data['actions']['POST'].keys()) == {'integer_field'}
def test_list_serializer_metadata_returns_info_about_fields_of_child_serializer(self): def test_list_serializer_metadata_returns_info_about_fields_of_child_serializer(self):
class ExampleSerializer(serializers.Serializer): class ExampleSerializer(serializers.Serializer):
integer_field = serializers.IntegerField(max_value=10) integer_field = serializers.IntegerField(max_value=10)