From fbca9279b6eda53c45dee8fb9835965baf4eb803 Mon Sep 17 00:00:00 2001 From: Rainer Keller Date: Fri, 30 Jan 2026 17:07:04 +0100 Subject: [PATCH] Fix SPDX format writer loosing relationships The relationships were only added if a file information was not already written to the output writer. In all other cases, the relationship got lost. Moving the writing of the relationship outside of the if statement makes it write the relationship info in all cases. The test cases for tagvalue_write did also declare relationships, but the validation data was missing them. Signed-off-by: Rainer Keller --- src/spdx_tools/spdx/writer/tagvalue/tagvalue_writer.py | 8 +++----- tests/spdx/writer/tagvalue/test_tagvalue_writer.py | 3 +++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/spdx_tools/spdx/writer/tagvalue/tagvalue_writer.py b/src/spdx_tools/spdx/writer/tagvalue/tagvalue_writer.py index 6e8de2e94..51cd26504 100644 --- a/src/spdx_tools/spdx/writer/tagvalue/tagvalue_writer.py +++ b/src/spdx_tools/spdx/writer/tagvalue/tagvalue_writer.py @@ -73,11 +73,7 @@ def write_document(document: Document, text_output: TextIO): write_separator(text_output) if package.spdx_id in contained_files_by_package_id: for file in contained_files_by_package_id[package.spdx_id]: - if file.spdx_id in already_written_file_ids: - relationships_to_write.append( - Relationship(package.spdx_id, RelationshipType.CONTAINS, file.spdx_id) - ) - else: + if file.spdx_id not in already_written_file_ids: write_file(file, text_output) write_separator(text_output) if file.spdx_id in file_ids_with_contained_snippets: @@ -89,6 +85,8 @@ def write_document(document: Document, text_output: TextIO): ) already_written_file_ids.append(file.spdx_id) + relationships_to_write.append(Relationship(package.spdx_id, RelationshipType.CONTAINS, file.spdx_id)) + write_optional_heading(document.extracted_licensing_info, "## License Information\n", text_output) write_list_of_elements( document.extracted_licensing_info, write_extracted_licensing_info, text_output, with_separator=True diff --git a/tests/spdx/writer/tagvalue/test_tagvalue_writer.py b/tests/spdx/writer/tagvalue/test_tagvalue_writer.py index 487ee0dca..f6fc4f889 100644 --- a/tests/spdx/writer/tagvalue/test_tagvalue_writer.py +++ b/tests/spdx/writer/tagvalue/test_tagvalue_writer.py @@ -132,6 +132,8 @@ def test_correct_order_of_elements(): call("PackageDownloadLocation: \n"), call("FilesAnalyzed: true\n"), call("\n"), + call('## Relationships\n'), + call('Relationship: SPDXRef-Package-A CONTAINS SPDXRef-File-B\n'), call("\n"), ] ) @@ -215,6 +217,7 @@ def test_same_file_in_multiple_packages(): call("## Relationships\n"), call("Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-Package-A\n"), call("Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-Package-B\n"), + call("Relationship: SPDXRef-Package-A CONTAINS SPDXRef-File\n"), call("Relationship: SPDXRef-Package-B CONTAINS SPDXRef-File\n"), call("\n"), ]