Skip to content

Commit

Permalink
Fix handling of Hyperlink.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfischer-ch committed Sep 1, 2015
1 parent 8d430e9 commit 2dd00ef
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
15 changes: 13 additions & 2 deletions rest_framework_yaml/compat.py
Expand Up @@ -4,11 +4,16 @@
"""
# flake8: noqa


from django.utils import six
try:
import yaml
except ImportError:
yaml = None
yaml = represent_text = None
else:
if six.PY3:
yaml_represent_text = yaml.representer.SafeRepresenter.represent_str
else:
yaml_represent_text = yaml.representer.SafeRepresenter.represent_unicode

# OrderedDict only available in Python 2.7.
# This will always be the case in Django 1.7 and above, as these versions
Expand All @@ -19,6 +24,12 @@
except:
from django.utils.datastructures import SortedDict as OrderedDict

try:
# Note: Hyperlink is private(?) API from DRF 3
from rest_framework.relations import Hyperlink
except ImportError:
Hyperlink = None

try:
# Note: ReturnDict and ReturnList are private API from DRF 3
from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList
Expand Down
10 changes: 9 additions & 1 deletion rest_framework_yaml/encoders.py
Expand Up @@ -7,7 +7,9 @@

from django.utils import six

from .compat import yaml, OrderedDict, ReturnDict, ReturnList
from .compat import (
yaml, yaml_represent_text, Hyperlink, OrderedDict, ReturnDict, ReturnList
)


class SafeDumper(yaml.SafeDumper):
Expand Down Expand Up @@ -61,6 +63,12 @@ def represent_mapping(self, tag, mapping, flow_style=None):
yaml.representer.SafeRepresenter.represent_list
)

if Hyperlink:
SafeDumper.add_representer(
Hyperlink,
yaml_represent_text
)

if ReturnDict:
SafeDumper.add_representer(
ReturnDict,
Expand Down
12 changes: 12 additions & 0 deletions tests/test_renderers.py
@@ -1,9 +1,12 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import unittest
from io import BytesIO
from decimal import Decimal

from django.test import TestCase
from rest_framework_yaml.compat import Hyperlink
from rest_framework_yaml.renderers import YAMLRenderer
from rest_framework_yaml.parsers import YAMLParser

Expand Down Expand Up @@ -48,6 +51,15 @@ def test_render_decimal(self):
content = renderer.render({'field': Decimal('111.2')}, 'application/yaml')
self.assertYAMLContains(content.decode('utf-8'), "field: '111.2'")

@unittest.skipUnless(Hyperlink, 'Hyperlink is undefined')
def test_render_hyperlink(self):
"""
Test YAML Hyperlink rendering.
"""
renderer = YAMLRenderer()
content = renderer.render({'field': Hyperlink('http://pépé.com?great-answer=42', 'test')}, 'application/yaml')
self.assertYAMLContains(content.decode('utf-8'), "field: http://pépé.com?great-answer=42")

def assertYAMLContains(self, content, string):
self.assertTrue(string in content, '%r not in %r' % (string, content))

Expand Down

0 comments on commit 2dd00ef

Please sign in to comment.