Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
625eb4c5
Commit
625eb4c5
authored
Feb 19, 2017
by
Xavier Ordoquy
Committed by
GitHub
Feb 19, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4911 from jisaacstone/feature/serializer-mappings
Check for collection.Mapping instead of dict
parents
300e46a9
289e1e44
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
5 deletions
+36
-5
rest_framework/serializers.py
+4
-4
tests/test_serializer.py
+32
-1
No files found.
rest_framework/serializers.py
View file @
625eb4c5
...
@@ -15,7 +15,7 @@ from __future__ import unicode_literals
...
@@ -15,7 +15,7 @@ from __future__ import unicode_literals
import
copy
import
copy
import
inspect
import
inspect
import
traceback
import
traceback
from
collections
import
OrderedDict
from
collections
import
Mapping
,
OrderedDict
from
django.core.exceptions
import
ValidationError
as
DjangoValidationError
from
django.core.exceptions
import
ValidationError
as
DjangoValidationError
from
django.core.exceptions
import
ImproperlyConfigured
from
django.core.exceptions
import
ImproperlyConfigured
...
@@ -326,11 +326,11 @@ def as_serializer_error(exc):
...
@@ -326,11 +326,11 @@ def as_serializer_error(exc):
else
:
else
:
detail
=
exc
.
detail
detail
=
exc
.
detail
if
isinstance
(
detail
,
dict
):
if
isinstance
(
detail
,
Mapping
):
# If errors may be a dict we use the standard {key: list of values}.
# If errors may be a dict we use the standard {key: list of values}.
# Here we ensure that all the values are *lists* of errors.
# Here we ensure that all the values are *lists* of errors.
return
{
return
{
key
:
value
if
isinstance
(
value
,
(
list
,
dict
))
else
[
value
]
key
:
value
if
isinstance
(
value
,
(
list
,
Mapping
))
else
[
value
]
for
key
,
value
in
detail
.
items
()
for
key
,
value
in
detail
.
items
()
}
}
elif
isinstance
(
detail
,
list
):
elif
isinstance
(
detail
,
list
):
...
@@ -442,7 +442,7 @@ class Serializer(BaseSerializer):
...
@@ -442,7 +442,7 @@ class Serializer(BaseSerializer):
"""
"""
Dict of native values <- Dict of primitive datatypes.
Dict of native values <- Dict of primitive datatypes.
"""
"""
if
not
isinstance
(
data
,
dict
):
if
not
isinstance
(
data
,
Mapping
):
message
=
self
.
error_messages
[
'invalid'
]
.
format
(
message
=
self
.
error_messages
[
'invalid'
]
.
format
(
datatype
=
type
(
data
)
.
__name__
datatype
=
type
(
data
)
.
__name__
)
)
...
...
tests/test_serializer.py
View file @
625eb4c5
...
@@ -4,9 +4,10 @@ from __future__ import unicode_literals
...
@@ -4,9 +4,10 @@ from __future__ import unicode_literals
import
inspect
import
inspect
import
pickle
import
pickle
import
re
import
re
import
unittest
from
collections
import
Mapping
import
pytest
import
pytest
from
django.db
import
models
from
django.db
import
models
from
rest_framework
import
fields
,
relations
,
serializers
from
rest_framework
import
fields
,
relations
,
serializers
...
@@ -15,6 +16,11 @@ from rest_framework.fields import Field
...
@@ -15,6 +16,11 @@ from rest_framework.fields import Field
from
.utils
import
MockObject
from
.utils
import
MockObject
try
:
from
collections
import
ChainMap
except
ImportError
:
ChainMap
=
False
# Test serializer fields imports.
# Test serializer fields imports.
# -------------------------------
# -------------------------------
...
@@ -113,6 +119,31 @@ class TestSerializer:
...
@@ -113,6 +119,31 @@ class TestSerializer:
assert
not
serializer
.
is_valid
()
assert
not
serializer
.
is_valid
()
assert
serializer
.
errors
==
{
'non_field_errors'
:
[
'No data provided'
]}
assert
serializer
.
errors
==
{
'non_field_errors'
:
[
'No data provided'
]}
@unittest.skipUnless
(
ChainMap
,
'requires python 3.3'
)
def
test_serialize_chainmap
(
self
):
data
=
ChainMap
({
'char'
:
'abc'
},
{
'integer'
:
123
})
serializer
=
self
.
Serializer
(
data
=
data
)
assert
serializer
.
is_valid
()
assert
serializer
.
validated_data
==
{
'char'
:
'abc'
,
'integer'
:
123
}
assert
serializer
.
errors
==
{}
def
test_serialize_custom_mapping
(
self
):
class
SinglePurposeMapping
(
Mapping
):
def
__getitem__
(
self
,
key
):
return
'abc'
if
key
==
'char'
else
123
def
__iter__
(
self
):
yield
'char'
yield
'integer'
def
__len__
(
self
):
return
2
serializer
=
self
.
Serializer
(
data
=
SinglePurposeMapping
())
assert
serializer
.
is_valid
()
assert
serializer
.
validated_data
==
{
'char'
:
'abc'
,
'integer'
:
123
}
assert
serializer
.
errors
==
{}
class
TestValidateMethod
:
class
TestValidateMethod
:
def
test_non_field_error_validate_method
(
self
):
def
test_non_field_error_validate_method
(
self
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment