Skip to content

only allow integer field to accept integers and strings, fixes #2835 #2836

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 1 commit into from
Apr 20, 2015
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
2 changes: 1 addition & 1 deletion rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ def to_internal_value(self, data):
self.fail('max_string_length')

try:
data = int(data)
data = int(re.compile(r'\.0*\s*$').sub('', str(data)))
Copy link
Member

Choose a reason for hiding this comment

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

  1. We should push this to self.re_decimal as per in Django so that it's only compiled once.
  2. We should comment that this regex substitution is so that we allow eg '1.0' as an int, but not '1.2'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

except (ValueError, TypeError):
self.fail('invalid')
return data
Expand Down
7 changes: 5 additions & 2 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,13 @@ class TestIntegerField(FieldValues):
1: 1,
0: 0,
1.0: 1,
0.0: 0
0.0: 0,
'1.0': 1
}
invalid_inputs = {
'abc': ['A valid integer is required.']
0.5: ['A valid integer is required.'],
'abc': ['A valid integer is required.'],
'0.5': ['A valid integer is required.']
}
outputs = {
'1': 1,
Expand Down