Skip to content

Commit

Permalink
Fix PycodeSerializer to escape unicode characters in string values
Browse files Browse the repository at this point in the history
  • Loading branch information
SrirachaHorse committed Dec 8, 2023
1 parent 212ca4f commit 81b05d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions tests/formats/dataclass/serializers/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ def test_write_class_with_default_values(self):
)
self.assertEqual(expected, result)

def test_write_string_with_unicode_characters(self):
books = Books(book=[BookForm(author="Backslashes \\ One Two \x12 Three")])
result = self.serializer.render(books, var_name="books")
expected = (
"from tests.fixtures.books.books import BookForm\n"
"from tests.fixtures.books.books import Books\n"
"\n"
"\n"
"books = Books(\n"
" book=[\n"
" BookForm(\n"
' author="Backslashes \\\\ One Two \\x12 Three"\n'
" ),\n"
" ]\n"
")\n"
)
self.assertEqual(expected, result)

def test_write_object_with_empty_array(self):
iterator = self.serializer.write_object([], 0, set())
self.assertEqual("[]", "".join(iterator))
Expand Down
2 changes: 1 addition & 1 deletion xsdata/utils/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def attrsetter(obj: Any, attr: str, value: Any):

def literal_value(value: Any) -> str:
if isinstance(value, str):
return quoteattr(value)
return quoteattr(value.encode('unicode_escape').decode('ASCII'))

if isinstance(value, float):
return str(value) if math.isfinite(value) else f'float("{value}")'
Expand Down

0 comments on commit 81b05d3

Please sign in to comment.