Repository: tefra/xsdata

Issue:
Empty lists do not get serialized
Hi,

When I use the following schema

```xsd
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Wrapper" type="wrapper"/>

    <xsd:complexType name="wrapper">
        <xsd:sequence>
            <xsd:element name="ListOfNumbers" type="listOfNumbers" />
        </xsd:sequence>
    </xsd:complexType>
    
    <xsd:simpleType name="listOfNumbers">
        <xsd:list itemType="xsd:long" />
    </xsd:simpleType>
</xsd:schema>
```

to create a `Wrapper` object with an empty `ListOfNumbers` and I serialize it using the code below

```python
from xsdata.formats.dataclass.serializers import XmlSerializer
from xsdata.formats.dataclass.serializers.config import SerializerConfig

from generated.schema import Wrapper

obj = Wrapper()
obj.list_of_numbers = []

config = SerializerConfig(pretty_print=True)
serializer = XmlSerializer(config)

print(serializer.render(obj))
```

The `ListOfNumbers` element do not get serialized which results in the following XML file.

```xml
<?xml version="1.0" encoding="UTF-8"?>
<Wrapper/>
```

First of all, is this the expected behaviour ? I find it surprising to not have an empty element in the generated XML, which I expected to look somewhat like this

```xml
<?xml version="1.0" encoding="UTF-8"?>
<Wrapper>
    <ListOfNumbers/>
</Wrapper>
```

If this is the expected behaviour, would you be open to a pull request adding an option to serialize empty lists as empty elements rather than discard them?

Discussion:

Patch:
diff --git a/xsdata/formats/dataclass/serializers/xml.py b/xsdata/formats/dataclass/serializers/xml.py
index b3cda034..28fbaa69 100644
--- a/xsdata/formats/dataclass/serializers/xml.py
+++ b/xsdata/formats/dataclass/serializers/xml.py
@@ -160,7 +160,7 @@ class XmlSerializer(AbstractSerializer):
     def write_tokens(self, value: Any, var: XmlVar, namespace: NoneStr) -> Generator:
         """Produce an events stream for the given tokens list or list of tokens
         lists."""
-        if value or var.nillable:
+        if value is not None or var.nillable:
             if value and collections.is_array(value[0]):
                 for val in value:
                     yield from self.write_element(val, var, namespace)


Test patch:
diff --git a/tests/formats/dataclass/serializers/test_xml.py b/tests/formats/dataclass/serializers/test_xml.py
index 56c74c2c..30d01c58 100644
--- a/tests/formats/dataclass/serializers/test_xml.py
+++ b/tests/formats/dataclass/serializers/test_xml.py
@@ -135,9 +135,18 @@ class XmlSerializerTests(TestCase):
             xml_type=XmlType.ELEMENT, qname="a", tokens_factory=list
         )
 
+        result = self.serializer.write_value(None, var, "xsdata")
+        self.assertIsInstance(result, Generator)
+        self.assertListEqual([], list(result))
+
+        expected = [
+            (XmlWriterEvent.START, "a"),
+            (XmlWriterEvent.DATA, []),
+            (XmlWriterEvent.END, "a"),
+        ]
         result = self.serializer.write_value([], var, "xsdata")
         self.assertIsInstance(result, Generator)
-        self.assertEqual(0, len(list(result)))
+        self.assertListEqual(expected, list(result))
 
         expected = [
             (XmlWriterEvent.START, "a"),