From c92ec4a2ef5d0e60b4657bfef19ba772746ac796 Mon Sep 17 00:00:00 2001 From: Jan Kowalleck Date: Wed, 4 Oct 2023 11:35:24 +0200 Subject: [PATCH] fixes Signed-off-by: Jan Kowalleck --- cyclonedx/output/__init__.py | 6 ++++-- cyclonedx/output/json.py | 3 ++- cyclonedx/output/xml.py | 12 +++++++++--- tests/_data/own/xml/1.4/indented_None.xml | 2 +- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/cyclonedx/output/__init__.py b/cyclonedx/output/__init__.py index 5a8bc087d..7e92cb1ed 100644 --- a/cyclonedx/output/__init__.py +++ b/cyclonedx/output/__init__.py @@ -73,11 +73,13 @@ def generate(self, force_regeneration: bool = False) -> None: @abstractmethod def output_as_string(self, *, - indent: Optional[Union[int, str]] = None) -> str: + indent: Optional[Union[int, str]] = None, + **kwargs) -> str: ... def output_to_file(self, filename: str, allow_overwrite: bool = False, *, - indent: Optional[Union[int, str]] = None) -> None: + indent: Optional[Union[int, str]] = None, + **kwargs) -> None: # Check directory writable output_filename = os.path.realpath(filename) output_directory = os.path.dirname(output_filename) diff --git a/cyclonedx/output/json.py b/cyclonedx/output/json.py index dc124e245..22e76caad 100644 --- a/cyclonedx/output/json.py +++ b/cyclonedx/output/json.py @@ -74,7 +74,8 @@ def generate(self, force_regeneration: bool = False) -> None: self.generated = True def output_as_string(self, *, - indent: Optional[Union[int, str]] = None) -> str: + indent: Optional[Union[int, str]] = None, + **kwargs) -> str: self.generate() return json_dumps(self._bom_json, indent=indent) diff --git a/cyclonedx/output/xml.py b/cyclonedx/output/xml.py index 8eb0fdc63..9a8942fc4 100644 --- a/cyclonedx/output/xml.py +++ b/cyclonedx/output/xml.py @@ -56,11 +56,15 @@ def generate(self, force_regeneration: bool = False) -> None: _view = SCHEMA_VERSIONS[self.schema_version_enum] self.get_bom().validate() xmlns = self.get_target_namespace() - self._bom_xml = xml_dumps( + self._bom_xml = '\n' + xml_dumps( self.get_bom().as_xml( # type:ignore[attr-defined] _view, as_string=False, xmlns=xmlns), method='xml', - encoding='unicode', xml_declaration=True, + encoding='unicode', + # `xml-declaration` is inconsistent/bugged in py38, especially on Windows it will print a non-UTF8 codepage. + # Furthermore, it might add an encoding of "utf-8" which is redundant default value of XML. + # -> so we write the declaration manually, as long as py38 is supported. + xml_declaration=False, default_namespace=xmlns) self.generated = True @@ -73,7 +77,9 @@ def __make_indent(v: Optional[Union[int, str]]) -> str: return v return '' - def output_as_string(self, *, indent: Optional[Union[int, str]] = None) -> str: + def output_as_string(self, *, + indent: Optional[Union[int, str]] = None, + **kwargs) -> str: self.generate() return self._bom_xml if indent is None else dom_parseString(self._bom_xml).toprettyxml( indent=self.__make_indent(indent) diff --git a/tests/_data/own/xml/1.4/indented_None.xml b/tests/_data/own/xml/1.4/indented_None.xml index f5e036270..52d425fb9 100644 --- a/tests/_data/own/xml/1.4/indented_None.xml +++ b/tests/_data/own/xml/1.4/indented_None.xml @@ -1,2 +1,2 @@ - + 2021-12-31T10:00:00+00:00CycloneDXcyclonedx-python-libTESTINGhttps://github.com/CycloneDX/cyclonedx-python-lib/actionshttps://pypi.org/project/cyclonedx-python-lib/https://cyclonedx.github.io/cyclonedx-python-lib/https://github.com/CycloneDX/cyclonedx-python-lib/issueshttps://github.com/CycloneDX/cyclonedx-python-lib/blob/main/LICENSEhttps://github.com/CycloneDX/cyclonedx-python-lib/blob/main/CHANGELOG.mdhttps://github.com/CycloneDX/cyclonedx-python-libhttps://cyclonedx.orgTest Authorsetuptools50.3.2MIT Licensepkg:pypi/setuptools@50.3.2?extension=tar.gz \ No newline at end of file