Repository: tefra/xsdata

Issue:
SerializerConfig introduced 2 issues
Hello,

1st issue is that the constructor interface for XmlSerializer changed.

```python
XmlSerializer(encoding=None)
```

no longer works. This is a smaller issue as I can change my code to support the new library (but it's still a change that isn't backwards compatible)

2nd issue is that the XmlWriter no longer supports encoding=None. This is a blocker, as I have a rest API that will not accept a payload with an encoding.

Comments:

Patch:
diff --git a/docs/xml.rst b/docs/xml.rst
index 47ba1e30..61e62dcd 100644
--- a/docs/xml.rst
+++ b/docs/xml.rst
@@ -167,6 +167,9 @@ context instance between them to save on memory and processing.
        * - xml_version
          - str
          - XML Version number, default: ``1.0``
+       * - xml_declaration
+         - bool
+         - Generate XML declaration, default ``True``
        * - pretty_print
          - bool
          - Enable pretty output, default ``False``
diff --git a/xsdata/formats/dataclass/serializers/config.py b/xsdata/formats/dataclass/serializers/config.py
index e7369247..4a408c4a 100644
--- a/xsdata/formats/dataclass/serializers/config.py
+++ b/xsdata/formats/dataclass/serializers/config.py
@@ -8,6 +8,7 @@ class SerializerConfig:
     """
     :param encoding: Text encoding
     :param xml_version: XML Version number (1.0|1.1)
+    :param xml_declaration: Generate XML declaration
     :param pretty_print: Enable pretty output
     :param schema_location: Specify the xsi:schemaLocation attribute value
     :param no_namespace_schema_location: Specify the xsi:noNamespaceSchemaLocation
@@ -16,6 +17,7 @@ class SerializerConfig:
 
     encoding: str = field(default="UTF-8")
     xml_version: str = field(default="1.0")
+    xml_declaration: bool = field(default=True)
     pretty_print: bool = field(default=False)
     schema_location: Optional[str] = field(default=None)
     no_namespace_schema_location: Optional[str] = field(default=None)
diff --git a/xsdata/formats/dataclass/serializers/mixins.py b/xsdata/formats/dataclass/serializers/mixins.py
index 76b8a597..c4b44989 100644
--- a/xsdata/formats/dataclass/serializers/mixins.py
+++ b/xsdata/formats/dataclass/serializers/mixins.py
@@ -91,8 +91,9 @@ class XmlWriter:
         self.handler.endDocument()
 
     def start_document(self):
-        self.output.write(f'<?xml version="{self.config.xml_version}"')
-        self.output.write(f' encoding="{self.config.encoding}"?>\n')
+        if self.config.xml_declaration:
+            self.output.write(f'<?xml version="{self.config.xml_version}"')
+            self.output.write(f' encoding="{self.config.encoding}"?>\n')
 
     def start_tag(self, qname: str):
         """


Test patch:
diff --git a/tests/formats/dataclass/serializers/writers/test_lxml.py b/tests/formats/dataclass/serializers/writers/test_lxml.py
index d8cb5ff9..2d92f070 100644
--- a/tests/formats/dataclass/serializers/writers/test_lxml.py
+++ b/tests/formats/dataclass/serializers/writers/test_lxml.py
@@ -41,6 +41,14 @@ class LxmlEventWriterTests(TestCase):
 
         self.assertEqual('<?xml version="1.1" encoding="US-ASCII"?>', xml_declaration)
 
+    def test_declaration_disabled(self):
+        self.serializer.config.xml_declaration = False
+        actual = self.serializer.render(books, {None: "urn:books"})
+        expected = fixtures_dir.joinpath("books/books_default_ns.xml").read_text()
+        xml_declaration, expected = expected.split("\n", 1)
+
+        self.assertEqual(expected, actual)
+
     def test_pretty_print_false(self):
         self.serializer.config.pretty_print = False
         actual = self.serializer.render(books)
diff --git a/tests/formats/dataclass/serializers/writers/test_native.py b/tests/formats/dataclass/serializers/writers/test_native.py
index 30185195..309a9a78 100644
--- a/tests/formats/dataclass/serializers/writers/test_native.py
+++ b/tests/formats/dataclass/serializers/writers/test_native.py
@@ -35,6 +35,14 @@ class XmlEventWriterTests(TestCase):
 
         self.assertEqual('<?xml version="1.1" encoding="US-ASCII"?>', xml_declaration)
 
+    def test_declaration_disabled(self):
+        self.serializer.config.xml_declaration = False
+        actual = self.serializer.render(books, {None: "urn:books"})
+        expected = fixtures_dir.joinpath("books/books_default_ns.xml").read_text()
+        xml_declaration, expected = expected.split("\n", 1)
+
+        self.assertEqual(expected, actual)
+
     def test_pretty_print_false(self):
         self.serializer.config.pretty_print = False
         actual = self.serializer.render(books)