Skip to content

Commit

Permalink
fix: handle empty XML elements during deserialization
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Horton <paul.horton@owasp.org>
  • Loading branch information
madpah committed Mar 2, 2023
1 parent bc8300d commit f806f35
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions serializable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,9 @@ def _from_xml(cls: Type[_T], data: Union[TextIOWrapper, ElementTree.Element],

logging.debug(f'Creating {cls} from {_data}')

if len(_data) == 0:
return None

return cls(**_data)


Expand Down
33 changes: 33 additions & 0 deletions tests/fixtures/the-phoenix-project-camel-case-1-v1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<book isbnNumber="978-1942788294">
<id>f3758bf0-0ff7-4366-a5e5-c209d4352b2d</id>
<title>The Phoenix Project</title>
<edition number="5">5th Anniversary Limited Edition</edition>
<publishDate>2018-04-16</publishDate>
<author>Kevin Behr</author>
<author>Gene Kim</author>
<author>George Spafford</author>
<type>fiction</type>
<publisher>
<name>IT Revolution Press LLC</name>
</publisher>
<chapters>
<chapter>
<number>1</number>
<title>Tuesday, September 2</title>
</chapter>
<chapter>
<number>2</number>
<title>Tuesday, September 2</title>
</chapter>
<chapter>
<number>3</number>
<title>Tuesday, September 2</title>
</chapter>
<chapter>
<number>4</number>
<title>Wednesday, September 3</title>
</chapter>
</chapters>
<references />
</book>
2 changes: 1 addition & 1 deletion tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_serialize_tfp_sc1(self) -> None:

def test_deserialize_tfp_cc1(self) -> None:
CurrentFormatter.formatter = CamelCasePropertyNameFormatter
with open(os.path.join(FIXTURES_DIRECTORY, 'the-phoenix-project-camel-case-1.xml')) as input_xml:
with open(os.path.join(FIXTURES_DIRECTORY, 'the-phoenix-project-camel-case-1-v1.xml')) as input_xml:
book: Book = Book.from_xml(data=ElementTree.fromstring(input_xml.read()))
self.assertEqual(ThePhoenixProject_v1.title, book.title)
self.assertEqual(ThePhoenixProject_v1.isbn, book.isbn)
Expand Down

0 comments on commit f806f35

Please sign in to comment.