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
4655501d
Commit
4655501d
authored
Sep 15, 2016
by
Tom Christie
Committed by
GitHub
Sep 15, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix regression of `RegexField`. (#4490)
* Don't deepcopy 'regex' arguments, instead treat as immutable.
parent
a68b37d8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
9 deletions
+41
-9
rest_framework/fields.py
+12
-9
tests/test_fields.py
+15
-0
tests/test_serializer.py
+14
-0
No files found.
rest_framework/fields.py
View file @
4655501d
...
...
@@ -252,6 +252,8 @@ class SkipField(Exception):
pass
REGEX_TYPE
=
type
(
re
.
compile
(
''
))
NOT_READ_ONLY_WRITE_ONLY
=
'May not set both `read_only` and `write_only`'
NOT_READ_ONLY_REQUIRED
=
'May not set both `read_only` and `required`'
NOT_REQUIRED_DEFAULT
=
'May not set both `required` and `default`'
...
...
@@ -581,16 +583,17 @@ class Field(object):
When cloning fields we instantiate using the arguments it was
originally created with, rather than copying the complete state.
"""
args
=
copy
.
deepcopy
(
self
.
_args
)
kwargs
=
dict
(
self
.
_kwargs
)
# Bit ugly, but we need to special case 'validators' as Django's
# RegexValidator does not support deepcopy.
# We treat validator callables as immutable objects.
# Treat regexes and validators as immutable.
# See https://github.com/tomchristie/django-rest-framework/issues/1954
validators
=
kwargs
.
pop
(
'validators'
,
None
)
kwargs
=
copy
.
deepcopy
(
kwargs
)
if
validators
is
not
None
:
kwargs
[
'validators'
]
=
validators
# and https://github.com/tomchristie/django-rest-framework/pull/4489
args
=
[
copy
.
deepcopy
(
item
)
if
not
isinstance
(
item
,
REGEX_TYPE
)
else
item
for
item
in
self
.
_args
]
kwargs
=
{
key
:
(
copy
.
deepcopy
(
value
)
if
(
key
not
in
(
'validators'
,
'regex'
))
else
value
)
for
key
,
value
in
self
.
_kwargs
.
items
()
}
return
self
.
__class__
(
*
args
,
**
kwargs
)
def
__repr__
(
self
):
...
...
tests/test_fields.py
View file @
4655501d
import
datetime
import
os
import
re
import
uuid
from
decimal
import
Decimal
...
...
@@ -590,6 +591,20 @@ class TestRegexField(FieldValues):
field
=
serializers
.
RegexField
(
regex
=
'[a-z][0-9]'
)
class
TestiCompiledRegexField
(
FieldValues
):
"""
Valid and invalid values for `RegexField`.
"""
valid_inputs
=
{
'a9'
:
'a9'
,
}
invalid_inputs
=
{
'A9'
:
[
"This value does not match the required pattern."
]
}
outputs
=
{}
field
=
serializers
.
RegexField
(
regex
=
re
.
compile
(
'[a-z][0-9]'
))
class
TestSlugField
(
FieldValues
):
"""
Valid and invalid values for `SlugField`.
...
...
tests/test_serializer.py
View file @
4655501d
...
...
@@ -2,6 +2,7 @@
from
__future__
import
unicode_literals
import
pickle
import
re
import
pytest
...
...
@@ -337,3 +338,16 @@ class TestDefaultInclusions:
assert
serializer
.
is_valid
()
assert
serializer
.
validated_data
==
{
'integer'
:
456
}
assert
serializer
.
errors
==
{}
class
TestSerializerValidationWithCompiledRegexField
:
def
setup
(
self
):
class
ExampleSerializer
(
serializers
.
Serializer
):
name
=
serializers
.
RegexField
(
re
.
compile
(
r'\d'
),
required
=
True
)
self
.
Serializer
=
ExampleSerializer
def
test_validation_success
(
self
):
serializer
=
self
.
Serializer
(
data
=
{
'name'
:
'2'
})
assert
serializer
.
is_valid
()
assert
serializer
.
validated_data
==
{
'name'
:
'2'
}
assert
serializer
.
errors
==
{}
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