Fixed #15197 -- Fixed yaml serialization into HttpResponse
Thanks fourga38 for the report and hirokiky at gmail.com for the initial patch.
This commit is contained in:
parent
4f53e77f07
commit
26cb227cfe
|
@ -52,9 +52,8 @@ class Serializer(PythonSerializer):
|
||||||
self._current = None
|
self._current = None
|
||||||
|
|
||||||
def getvalue(self):
|
def getvalue(self):
|
||||||
# overwrite PythonSerializer.getvalue() with base Serializer.getvalue()
|
# Grand-parent super
|
||||||
if callable(getattr(self.stream, 'getvalue', None)):
|
return super(PythonSerializer, self).getvalue()
|
||||||
return self.stream.getvalue()
|
|
||||||
|
|
||||||
|
|
||||||
def Deserializer(stream_or_string, **options):
|
def Deserializer(stream_or_string, **options):
|
||||||
|
|
|
@ -44,7 +44,8 @@ class Serializer(PythonSerializer):
|
||||||
yaml.dump(self.objects, self.stream, Dumper=DjangoSafeDumper, **self.options)
|
yaml.dump(self.objects, self.stream, Dumper=DjangoSafeDumper, **self.options)
|
||||||
|
|
||||||
def getvalue(self):
|
def getvalue(self):
|
||||||
return self.stream.getvalue()
|
# Grand-parent super
|
||||||
|
return super(PythonSerializer, self).getvalue()
|
||||||
|
|
||||||
def Deserializer(stream_or_string, **options):
|
def Deserializer(stream_or_string, **options):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -21,6 +21,7 @@ from django.core import serializers
|
||||||
from django.core.serializers import SerializerDoesNotExist
|
from django.core.serializers import SerializerDoesNotExist
|
||||||
from django.core.serializers.base import DeserializationError
|
from django.core.serializers.base import DeserializationError
|
||||||
from django.db import connection, models
|
from django.db import connection, models
|
||||||
|
from django.http import HttpResponse
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.utils.functional import curry
|
from django.utils.functional import curry
|
||||||
from django.utils.unittest import skipUnless
|
from django.utils.unittest import skipUnless
|
||||||
|
@ -501,15 +502,18 @@ def streamTest(format, self):
|
||||||
obj.save_base(raw=True)
|
obj.save_base(raw=True)
|
||||||
|
|
||||||
# Serialize the test database to a stream
|
# Serialize the test database to a stream
|
||||||
stream = BytesIO()
|
for stream in (BytesIO(), HttpResponse()):
|
||||||
serializers.serialize(format, [obj], indent=2, stream=stream)
|
serializers.serialize(format, [obj], indent=2, stream=stream)
|
||||||
|
|
||||||
# Serialize normally for a comparison
|
# Serialize normally for a comparison
|
||||||
string_data = serializers.serialize(format, [obj], indent=2)
|
string_data = serializers.serialize(format, [obj], indent=2)
|
||||||
|
|
||||||
# Check that the two are the same
|
# Check that the two are the same
|
||||||
self.assertEqual(string_data, stream.getvalue())
|
if isinstance(stream, BytesIO):
|
||||||
stream.close()
|
self.assertEqual(string_data, stream.getvalue())
|
||||||
|
else:
|
||||||
|
self.assertEqual(string_data, stream.content)
|
||||||
|
stream.close()
|
||||||
|
|
||||||
for format in serializers.get_serializer_formats():
|
for format in serializers.get_serializer_formats():
|
||||||
setattr(SerializerTests, 'test_' + format + '_serializer', curry(serializerTest, format))
|
setattr(SerializerTests, 'test_' + format + '_serializer', curry(serializerTest, format))
|
||||||
|
|
Loading…
Reference in New Issue