Repository: tefra/xsdata

Issue:
Serializer render particularities
I have noticed recently that the rendering of some elements at this moment in time looks different than I have remembered before, but I might mixed up the JAXB output.

For some elements I noticed that empty lists are rendered as `<Blabla/>`. When explicitly marking blabla=None, the elements are removed from the output.

Discussion:
sonarcloud[bot]: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=tefra_xsdata&pullRequest=902) **Quality Gate passed**  
Kudos, no new issues were introduced!

[0 New issues](https://sonarcloud.io/project/issues?id=tefra_xsdata&pullRequest=902&resolved=false&inNewCodePeriod=true)  
[0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=tefra_xsdata&pullRequest=902&resolved=false&inNewCodePeriod=true)  
No data about Coverage  
[0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=tefra_xsdata&pullRequest=902&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=tefra_xsdata&pullRequest=902)



Patch:
diff --git a/xsdata/formats/dataclass/serializers/xml.py b/xsdata/formats/dataclass/serializers/xml.py
index 688870dc..7e256c71 100644
--- a/xsdata/formats/dataclass/serializers/xml.py
+++ b/xsdata/formats/dataclass/serializers/xml.py
@@ -174,7 +174,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 is not None or var.nillable:
+        if value or var.nillable or var.required:
             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 4bd1a377..a8904730 100644
--- a/tests/formats/dataclass/serializers/test_xml.py
+++ b/tests/formats/dataclass/serializers/test_xml.py
@@ -186,11 +186,16 @@ class XmlSerializerTests(TestCase):
         self.assertIsInstance(result, Generator)
         self.assertListEqual([], list(result))
 
+        result = self.serializer.write_value([], var, "xsdata")
+        self.assertIsInstance(result, Generator)
+        self.assertListEqual([], list(result))
+
         expected = [
             (XmlWriterEvent.START, "a"),
             (XmlWriterEvent.DATA, []),
             (XmlWriterEvent.END, "a"),
         ]
+        var.required = True
         result = self.serializer.write_value([], var, "xsdata")
         self.assertIsInstance(result, Generator)
         self.assertListEqual(expected, list(result))