From e327f270940b189c3a3be0153856c2b1a3390920 Mon Sep 17 00:00:00 2001 From: Joris Beckers Date: Mon, 20 Mar 2017 15:55:52 +0100 Subject: [PATCH 1/2] Fix for #4 where an ErrorDetail string subclass raises an error. --- rest_framework_yaml/encoders.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rest_framework_yaml/encoders.py b/rest_framework_yaml/encoders.py index a69dc70..be6640a 100644 --- a/rest_framework_yaml/encoders.py +++ b/rest_framework_yaml/encoders.py @@ -2,10 +2,12 @@ Helper classes for parsers. """ from __future__ import unicode_literals + import decimal import types from django.utils import six +from rest_framework import exceptions from .compat import ( yaml, yaml_represent_text, Hyperlink, OrderedDict, ReturnDict, ReturnList @@ -63,6 +65,11 @@ def represent_mapping(self, tag, mapping, flow_style=None): yaml.representer.SafeRepresenter.represent_list ) +SafeDumper.add_representer( + exceptions.ErrorDetail, + yaml_represent_text +) + if Hyperlink: SafeDumper.add_representer( Hyperlink, From cd21880a12eaf060dc295411204f886cef9fb52e Mon Sep 17 00:00:00 2001 From: Joris Beckers Date: Mon, 20 Mar 2017 16:18:34 +0100 Subject: [PATCH 2/2] Fix for pre DRF 3.5.0 versions not having ErrorDetail class --- rest_framework_yaml/compat.py | 6 ++++++ rest_framework_yaml/encoders.py | 14 +++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/rest_framework_yaml/compat.py b/rest_framework_yaml/compat.py index 9f6c147..2e5efc0 100644 --- a/rest_framework_yaml/compat.py +++ b/rest_framework_yaml/compat.py @@ -36,3 +36,9 @@ except ImportError: ReturnDict = None ReturnList = None + +try: + # Note: ErrorDetail was introduced in DRF 3.5.0 + from rest_framework.exceptions import ErrorDetail +except ImportError: + ErrorDetail = None diff --git a/rest_framework_yaml/encoders.py b/rest_framework_yaml/encoders.py index be6640a..848ae19 100644 --- a/rest_framework_yaml/encoders.py +++ b/rest_framework_yaml/encoders.py @@ -7,10 +7,9 @@ import types from django.utils import six -from rest_framework import exceptions from .compat import ( - yaml, yaml_represent_text, Hyperlink, OrderedDict, ReturnDict, ReturnList + yaml, yaml_represent_text, Hyperlink, OrderedDict, ReturnDict, ReturnList, ErrorDetail ) @@ -65,11 +64,6 @@ def represent_mapping(self, tag, mapping, flow_style=None): yaml.representer.SafeRepresenter.represent_list ) -SafeDumper.add_representer( - exceptions.ErrorDetail, - yaml_represent_text -) - if Hyperlink: SafeDumper.add_representer( Hyperlink, @@ -87,3 +81,9 @@ def represent_mapping(self, tag, mapping, flow_style=None): ReturnList, yaml.representer.SafeRepresenter.represent_list ) + +if ErrorDetail: + SafeDumper.add_representer( + ErrorDetail, + yaml_represent_text + )