Skip to content
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

remove usage of django's lazy function for error messages #6545

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 0 additions & 31 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import sys

from django.conf import settings
from django.core import validators
from django.utils import six
from django.views.generic import View

Expand Down Expand Up @@ -293,35 +292,5 @@ def md_filter_add_syntax_highlight(md):
LONG_SEPARATORS = (b', ', b': ')
INDENT_SEPARATORS = (b',', b': ')


class CustomValidatorMessage(object):
"""
We need to avoid evaluation of `lazy` translated `message` in `django.core.validators.BaseValidator.__init__`.
https://github.com/django/django/blob/75ed5900321d170debef4ac452b8b3cf8a1c2384/django/core/validators.py#L297

Ref: https://github.com/encode/django-rest-framework/pull/5452
"""

def __init__(self, *args, **kwargs):
self.message = kwargs.pop('message', self.message)
super(CustomValidatorMessage, self).__init__(*args, **kwargs)


class MinValueValidator(CustomValidatorMessage, validators.MinValueValidator):
pass


class MaxValueValidator(CustomValidatorMessage, validators.MaxValueValidator):
pass


class MinLengthValidator(CustomValidatorMessage, validators.MinLengthValidator):
pass


class MaxLengthValidator(CustomValidatorMessage, validators.MaxLengthValidator):
pass


# Version Constants.
PY36 = sys.version_info >= (3, 6)
123 changes: 70 additions & 53 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ValidationError as DjangoValidationError
from django.core.validators import (
EmailValidator, RegexValidator, URLValidator, ip_address_validators
EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator,
MinValueValidator, RegexValidator, URLValidator, ip_address_validators
)
from django.forms import FilePathField as DjangoFilePathField
from django.forms import ImageField as DjangoImageField
Expand All @@ -24,17 +25,14 @@
from django.utils.duration import duration_string
from django.utils.encoding import is_protected_type, smart_text
from django.utils.formats import localize_input, sanitize_separators
from django.utils.functional import lazy
from django.utils.ipv6 import clean_ipv6_address
from django.utils.timezone import utc
from django.utils.translation import ugettext_lazy as _
from pytz.exceptions import InvalidTimeError

from rest_framework import ISO_8601
from rest_framework.compat import (
Mapping, MaxLengthValidator, MaxValueValidator, MinLengthValidator,
MinValueValidator, ProhibitNullCharactersValidator, unicode_repr,
unicode_to_repr
Mapping, ProhibitNullCharactersValidator, unicode_repr, unicode_to_repr
)
from rest_framework.exceptions import ErrorDetail, ValidationError
from rest_framework.settings import api_settings
Expand Down Expand Up @@ -234,17 +232,25 @@ def get_error_detail(exc_info):
with the `code` populated.
"""
code = getattr(exc_info, 'code', None) or 'invalid'

try:
error_dict = exc_info.error_dict
except AttributeError:
for error in exc_info.error_list:
error.params = error.params or {}
error.params[error.code] = error.params.get('limit_value', '')
Copy link
Member

Choose a reason for hiding this comment

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

I've figured out what the rest of this is doing, but I can't figure out what this line is supposed to be doing.

It's not clear where this parameter was being previously injected, or if it was, and I also don't see anything explaining what this is supposed to be doing.

Copy link
Author

Choose a reason for hiding this comment

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

Django's BaseValidator takes a limit_value and that value is used for formatting messages. For example, this is the message template of MaxValueValidator.
message = _('Ensure this value is less than or equal to %(limit_value)s.')

But in Rest Framework, Serializers use code property of Validators as keyword for error message templates. This is IntegerField error message, for example:
'Ensure this value is less than or equal to {max_value}.

This block of code extracts needed keyword from it's Validator classes and adds them to params dict for error formatting.

Copy link
Member

Choose a reason for hiding this comment

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

So, just to confirm, this is an issue with the current DRF logic and it's just being fixed in this PR alongside the stated removal of lazy()?

Copy link
Author

Choose a reason for hiding this comment

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

Yes. Actually a performance issue.

Copy link
Member

Choose a reason for hiding this comment

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

Given that this is new logic, this is where I lean towards having new tests to cover this new logic. It's clear that there are no current tests that cover it (otherwise they would be failing), and I'd hate to see a regression in the future because they are not covered.

I'd also love to see it broken out into a separate PR, so it can be independently reviewed and not hold up the general refactor that is happening here, but I'm not stuck on that.

return [
ErrorDetail(error.message % (error.params or ()),
ErrorDetail(error.message.format(**error.params),
code=error.code if error.code else code)
for error in exc_info.error_list]

for k, errors in error_dict.items():
for error in errors:
error.params = error.params or {}
error.params[error.code] = error.params.get('limit_value', '')

return {
k: [
ErrorDetail(error.message % (error.params or ()),
ErrorDetail(error.message.format(**error.params),
code=error.code if error.code else code)
for error in errors
] for k, errors in error_dict.items()
Expand Down Expand Up @@ -766,17 +772,19 @@ def __init__(self, **kwargs):
self.min_length = kwargs.pop('min_length', None)
super(CharField, self).__init__(**kwargs)
if self.max_length is not None:
message = lazy(
self.error_messages['max_length'].format,
six.text_type)(max_length=self.max_length)
self.validators.append(
MaxLengthValidator(self.max_length, message=message))
MaxLengthValidator(
self.max_length,
message=self.error_messages['max_length']
)
)
if self.min_length is not None:
message = lazy(
self.error_messages['min_length'].format,
six.text_type)(min_length=self.min_length)
self.validators.append(
MinLengthValidator(self.min_length, message=message))
MinLengthValidator(
self.min_length,
message=self.error_messages['min_length']
)
)

# ProhibitNullCharactersValidator is None on Django < 2.0
if ProhibitNullCharactersValidator is not None:
Expand Down Expand Up @@ -935,17 +943,19 @@ def __init__(self, **kwargs):
self.min_value = kwargs.pop('min_value', None)
super(IntegerField, self).__init__(**kwargs)
if self.max_value is not None:
message = lazy(
self.error_messages['max_value'].format,
six.text_type)(max_value=self.max_value)
self.validators.append(
MaxValueValidator(self.max_value, message=message))
MaxValueValidator(
self.max_value,
message=self.error_messages['max_value']
)
)
if self.min_value is not None:
message = lazy(
self.error_messages['min_value'].format,
six.text_type)(min_value=self.min_value)
self.validators.append(
MinValueValidator(self.min_value, message=message))
MinValueValidator(
self.min_value,
message=self.error_messages['min_value']
)
)

def to_internal_value(self, data):
if isinstance(data, six.text_type) and len(data) > self.MAX_STRING_LENGTH:
Expand Down Expand Up @@ -975,20 +985,21 @@ def __init__(self, **kwargs):
self.min_value = kwargs.pop('min_value', None)
super(FloatField, self).__init__(**kwargs)
if self.max_value is not None:
message = lazy(
self.error_messages['max_value'].format,
six.text_type)(max_value=self.max_value)
self.validators.append(
MaxValueValidator(self.max_value, message=message))
MaxValueValidator(
self.max_value,
message=self.error_messages['max_value']
)
)
if self.min_value is not None:
message = lazy(
self.error_messages['min_value'].format,
six.text_type)(min_value=self.min_value)
self.validators.append(
MinValueValidator(self.min_value, message=message))
MinValueValidator(
self.min_value,
message=self.error_messages['min_value']
)
)

def to_internal_value(self, data):

if isinstance(data, six.text_type) and len(data) > self.MAX_STRING_LENGTH:
self.fail('max_string_length')

Expand Down Expand Up @@ -1034,17 +1045,20 @@ def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value=
super(DecimalField, self).__init__(**kwargs)

if self.max_value is not None:
message = lazy(
self.error_messages['max_value'].format,
six.text_type)(max_value=self.max_value)
self.validators.append(
MaxValueValidator(self.max_value, message=message))
MaxValueValidator(
self.max_value,
message=self.error_messages['max_value']
)
)

if self.min_value is not None:
message = lazy(
self.error_messages['min_value'].format,
six.text_type)(min_value=self.min_value)
self.validators.append(
MinValueValidator(self.min_value, message=message))
MinValueValidator(
self.min_value,
message=self.error_messages['min_value']
)
)

if rounding is not None:
valid_roundings = [v for k, v in vars(decimal).items() if k.startswith('ROUND_')]
Expand Down Expand Up @@ -1380,17 +1394,19 @@ def __init__(self, **kwargs):
self.min_value = kwargs.pop('min_value', None)
super(DurationField, self).__init__(**kwargs)
if self.max_value is not None:
message = lazy(
self.error_messages['max_value'].format,
six.text_type)(max_value=self.max_value)
self.validators.append(
MaxValueValidator(self.max_value, message=message))
MaxValueValidator(
self.max_value,
message=self.error_messages['max_value']
)
)
if self.min_value is not None:
message = lazy(
self.error_messages['min_value'].format,
six.text_type)(min_value=self.min_value)
self.validators.append(
MinValueValidator(self.min_value, message=message))
MinValueValidator(
self.min_value,
message=self.error_messages['min_value']
)
)

def to_internal_value(self, value):
if isinstance(value, datetime.timedelta):
Expand Down Expand Up @@ -1907,11 +1923,12 @@ def __init__(self, model_field, **kwargs):
max_length = kwargs.pop('max_length', None)
super(ModelField, self).__init__(**kwargs)
if max_length is not None:
message = lazy(
self.error_messages['max_length'].format,
six.text_type)(max_length=self.max_length)
self.validators.append(
MaxLengthValidator(self.max_length, message=message))
MaxLengthValidator(
self.max_value,
message=self.error_messages['max_length']
)
)

def to_internal_value(self, data):
rel = self.model_field.remote_field
Expand Down