mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-18 12:30:58 +03:00
[fields] Format error message only if params exist (#6624)
This prevents exceptions when the error message contains `%`, but is
not intended for formatting. Django itself does the same:
6866c91b63/django/core/exceptions.py (L168-L169)
Fixes encode/django-rest-framework#6622
This commit is contained in:
parent
7179ea9984
commit
79b2350b54
|
@ -231,12 +231,12 @@ def get_error_detail(exc_info):
|
||||||
error_dict = exc_info.error_dict
|
error_dict = exc_info.error_dict
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return [
|
return [
|
||||||
ErrorDetail(error.message % (error.params or ()),
|
ErrorDetail((error.message % error.params) if error.params else error.message,
|
||||||
code=error.code if error.code else code)
|
code=error.code if error.code else code)
|
||||||
for error in exc_info.error_list]
|
for error in exc_info.error_list]
|
||||||
return {
|
return {
|
||||||
k: [
|
k: [
|
||||||
ErrorDetail(error.message % (error.params or ()),
|
ErrorDetail((error.message % error.params) if error.params else error.message,
|
||||||
code=error.code if error.code else code)
|
code=error.code if error.code else code)
|
||||||
for error in errors
|
for error in errors
|
||||||
] for k, errors in error_dict.items()
|
] for k, errors in error_dict.items()
|
||||||
|
|
|
@ -2255,14 +2255,33 @@ class TestValidationErrorCode:
|
||||||
password = serializers.CharField()
|
password = serializers.CharField()
|
||||||
|
|
||||||
def validate_password(self, obj):
|
def validate_password(self, obj):
|
||||||
err = DjangoValidationError('exc_msg', code='exc_code')
|
err = DjangoValidationError(
|
||||||
|
'exc_msg %s', code='exc_code', params=('exc_param',),
|
||||||
|
)
|
||||||
if use_list:
|
if use_list:
|
||||||
err = DjangoValidationError([err])
|
err = DjangoValidationError([err])
|
||||||
raise err
|
raise err
|
||||||
|
|
||||||
serializer = ExampleSerializer(data={'password': 123})
|
serializer = ExampleSerializer(data={'password': 123})
|
||||||
serializer.is_valid()
|
serializer.is_valid()
|
||||||
assert serializer.errors == {'password': ['exc_msg']}
|
assert serializer.errors == {'password': ['exc_msg exc_param']}
|
||||||
|
assert serializer.errors['password'][0].code == 'exc_code'
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('use_list', (False, True))
|
||||||
|
def test_validationerror_code_with_msg_including_percent(self, use_list):
|
||||||
|
|
||||||
|
class ExampleSerializer(serializers.Serializer):
|
||||||
|
password = serializers.CharField()
|
||||||
|
|
||||||
|
def validate_password(self, obj):
|
||||||
|
err = DjangoValidationError('exc_msg with %', code='exc_code')
|
||||||
|
if use_list:
|
||||||
|
err = DjangoValidationError([err])
|
||||||
|
raise err
|
||||||
|
|
||||||
|
serializer = ExampleSerializer(data={'password': 123})
|
||||||
|
serializer.is_valid()
|
||||||
|
assert serializer.errors == {'password': ['exc_msg with %']}
|
||||||
assert serializer.errors['password'][0].code == 'exc_code'
|
assert serializer.errors['password'][0].code == 'exc_code'
|
||||||
|
|
||||||
@pytest.mark.parametrize('code', (None, 'exc_code',))
|
@pytest.mark.parametrize('code', (None, 'exc_code',))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user