From 24b20d52cbcd5b1ee7462800a251bbfe8604ef24 Mon Sep 17 00:00:00 2001 From: zrgt Date: Tue, 5 May 2026 16:09:07 +0200 Subject: [PATCH] fix: DictSupplementaryFileContainer increment refcount in _assign_unique_name --- sdk/basyx/aas/adapter/aasx.py | 2 ++ sdk/test/adapter/aasx/test_aasx.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/sdk/basyx/aas/adapter/aasx.py b/sdk/basyx/aas/adapter/aasx.py index 2c7fe0b4..82fe4b76 100644 --- a/sdk/basyx/aas/adapter/aasx.py +++ b/sdk/basyx/aas/adapter/aasx.py @@ -880,6 +880,7 @@ def rename_file(self, old_name: str, new_name: str) -> str: if new_name == old_name: return new_name file_hash, file_content_type = self._name_map[old_name] + self._store_refcount[file_hash] -= 1 del self._name_map[old_name] return self._assign_unique_name(new_name, file_hash, file_content_type) @@ -889,6 +890,7 @@ def _assign_unique_name(self, name: str, sha: bytes, content_type: str) -> str: while True: if new_name not in self._name_map: self._name_map[new_name] = (sha, content_type) + self._store_refcount[sha] += 1 return new_name elif self._name_map[new_name] == (sha, content_type): return new_name diff --git a/sdk/test/adapter/aasx/test_aasx.py b/sdk/test/adapter/aasx/test_aasx.py index d4b0ff24..2243a0ed 100644 --- a/sdk/test/adapter/aasx/test_aasx.py +++ b/sdk/test/adapter/aasx/test_aasx.py @@ -88,6 +88,24 @@ def test_supplementary_file_container(self) -> None: with self.assertRaises(KeyError): container.write_file(duplicate_file, file_content) + def test_supplementary_file_container_refcount(self) -> None: + container = aasx.DictSupplementaryFileContainer() + data = b"test content" + name1 = container.add_file("/file1.bin", io.BytesIO(data), "application/octet-stream") + name2 = container.add_file("/file2.bin", io.BytesIO(data), "application/octet-stream") + content_hash = container.get_sha256(name1) + + # Both names point to same content — backing store must be present + self.assertIn(content_hash, container._store) + + # Deleting one reference must NOT free the backing store + container.delete_file(name1) + self.assertIn(content_hash, container._store) + + # Deleting the last reference must free the backing store + container.delete_file(name2) + self.assertNotIn(content_hash, container._store) + class AASXWriterTest(unittest.TestCase): def test_writing_reading_example_aas(self) -> None: