Skip to content

get_error_detail should not format without error.params #6622

Closed
@WolfgangFellger

Description

@WolfgangFellger

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I @michael-k has included a failing test as a pull request. (Thanks!)

get_error_detail should not attempt to format the message in DjangoValidationErrors if params aren't used. This is also in line with how Django behaves.

If f-strings are used to generate the error message, this means duplicate formatting, and fails if a % occurs in the message.

Steps to reproduce

Most basic example I could muster (this is actually supposed to be ModelValidator with the validator on a Django model)

from rest_framework import serializers
from django.core.exceptions import ValidationError


def validate(value):
    raise ValidationError(f'{value} is invalid')

class MySerializer(serializers.Serializer):
    field = serializers.CharField(validators=[validate])


s = MySerializer(data={'field': 'f%'})  
if not s.is_valid():  # throws TypeError: not enough arguments for format string
    print(s.errors)

Expected behavior

Should return the error message "f% is not valid"

Actual behavior

Throws a TypeError in

ErrorDetail(error.message % (error.params or ()),

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions