diff --git a/CHANGES.rst b/CHANGES.rst index c7aaf66..57d74ec 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -27,6 +27,9 @@ Bug Fixes - Fix an issue where the ``flatten()`` method produces an invalid name (ex: "answer.0.") for the type "Sequence". See https://github.com/Pylons/colander/issues/179 +- Fixed issue with ``String`` not being properly encoded when non-string + values were passed into ``serialize()`` + See `#235 `_ 1.0 (2014-11-26) diff --git a/colander/__init__.py b/colander/__init__.py index 1bd8f87..a99deed 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -1185,6 +1185,8 @@ class String(SchemaType): result = text_type(appstruct) else: result = text_type(appstruct) + if self.encoding: + result = result.encode(self.encoding) return result except Exception as e: raise Invalid(node, diff --git a/colander/tests/test_colander.py b/colander/tests/test_colander.py index 6d05d71..0dd99aa 100644 --- a/colander/tests/test_colander.py +++ b/colander/tests/test_colander.py @@ -1557,6 +1557,13 @@ class TestString(unittest.TestCase): e = invalid_exc(typ.serialize, node, not_utf8) self.assertTrue('cannot be serialized' in e.msg) + def test_serialize_encoding_with_non_string_type(self): + utf8 = text_type('123').encode('utf-8') + node = DummySchemaNode(None) + typ = self._makeOne('utf-8') + result = typ.serialize(node, 123) + self.assertEqual(result, utf8) + class TestInteger(unittest.TestCase): def _makeOne(self): from colander import Integer