Skip to content

Raise error if fields on serializer is not a list of strings. #2213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Member

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
Member

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
Member

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.