Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add validation for fields & exclude type.
  • Loading branch information
BrickXu committed Dec 5, 2014
commit d68c61450440a522b08b64fdd21028cc739e6ead
6 changes: 6 additions & 0 deletions rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,12 @@ def get_fields(self):
depth = getattr(self.Meta, 'depth', 0)
extra_kwargs = getattr(self.Meta, 'extra_kwargs', {})

if fields and not isinstance(fields, (list, tuple)):
raise TypeError('`fields` must be a list or tuple')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do:

fields must be a list or tuple, got %s' % type(fields).name

But I don't mind too much - will accept the pull request either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry, I've not seen the message during the weekend.

should I create another pull request to adjust the error message?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BrickXu If it's yet not fixed on master, I think it's safe to open another PR with those tweaks and also maybe this one as well https://github.com/tomchristie/django-rest-framework/pull/2213/files#r21361897


if exclude and not isinstance(exclude, (list, tuple)):
raise TypeError('`exclude` must be a list or tuple')

assert not (fields and exclude), "Cannot set both 'fields' and 'exclude'."

extra_kwargs = self._include_additional_options(extra_kwargs)
Expand Down
62 changes: 62 additions & 0 deletions tests/test_serializer_metaclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from django.test import TestCase
from rest_framework import serializers
from .models import BasicModel


class TestSerializerMetaClass(TestCase):
def setUp(self):
class FieldsSerializer(serializers.ModelSerializer):
text = serializers.CharField()

class Meta:
model = BasicModel
fields = ('text')

class ExcludeSerializer(serializers.ModelSerializer):
text = serializers.CharField()

class Meta:
model = BasicModel
exclude = ('text')

class FieldsAndExcludeSerializer(serializers.ModelSerializer):
text = serializers.CharField()

class Meta:
model = BasicModel
fields = ('text',)
exclude = ('text',)

self.fields_serializer = FieldsSerializer
self.exclude_serializer = ExcludeSerializer
self.faeSerializer = FieldsAndExcludeSerializer

def test_meta_class_fields(self):
object = BasicModel(text="Hello World.")
serializer = self.fields_serializer(instance=object)

with self.assertRaises(TypeError) as result:
serializer.data

exception = result.exception
self.assertEqual(str(exception), "`fields` must be a list or tuple")

def test_meta_class_exclude(self):
object = BasicModel(text="Hello World.")
serializer = self.exclude_serializer(instance=object)

with self.assertRaises(TypeError) as result:
serializer.data

exception = result.exception
self.assertEqual(str(exception), "`exclude` must be a list or tuple")

def test_meta_class_fields_and_exclude(self):
object = BasicModel(text="Hello World.")
serializer = self.faeSerializer(instance=object)

with self.assertRaises(AssertionError) as result:
serializer.data

exception = result.exception
self.assertEqual(str(exception), "Cannot set both 'fields' and 'exclude'.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd rather just see these in test_model_serializer.py, but look good otherwise.