diff --git a/packages/abstractions/kiota_abstractions/multipart_body.py b/packages/abstractions/kiota_abstractions/multipart_body.py index e3edbf27..5429ab61 100644 --- a/packages/abstractions/kiota_abstractions/multipart_body.py +++ b/packages/abstractions/kiota_abstractions/multipart_body.py @@ -58,7 +58,7 @@ def add_or_replace_part( raise ValueError("Part name cannot be null") if not content_type: raise ValueError("Content type cannot be null") - if not part_value: + if part_value is None: raise ValueError("Part value cannot be null") value: tuple[str, Any, Optional[str]] = (content_type, part_value, filename) self.parts[self._normalize_part_name(part_name)] = value @@ -100,10 +100,14 @@ def serialize(self, writer: SerializationWriter) -> None: """ if not writer: raise ValueError("Serialization writer cannot be null") - if not self.request_adapter or not self.request_adapter.get_serialization_writer_factory(): - raise ValueError("Request adapter or serialization writer factory cannot be null") if not self.parts: raise ValueError("No parts to serialize") + if any(isinstance(part[1], Parsable) for part in self.parts.values()): + if ( + not self.request_adapter + or not self.request_adapter.get_serialization_writer_factory() + ): + raise ValueError("Request adapter or serialization writer factory cannot be null") first = True for part_name, part_value in self.parts.items(): @@ -115,7 +119,7 @@ def serialize(self, writer: SerializationWriter) -> None: writer.write_str_value("", f"--{self.boundary}") writer.write_str_value("Content-Type", f"{part_value[0]}") writer.write_str_value( - "Content-Disposition", self._get_comtent_disposition(part_name, part_value) + "Content-Disposition", self._get_content_disposition(part_name, part_value) ) self._add_new_line(writer) @@ -139,13 +143,15 @@ def _normalize_part_name(self, original: str) -> str: def _add_new_line(self, writer: SerializationWriter) -> None: writer.write_str_value("", "") - def _get_comtent_disposition( + def _get_content_disposition( self, part_name: str, part_value: tuple[str, Any, Optional[str]] ) -> str: if len(part_value) >= 3 and part_value[2] is not None: return f'form-data; name="{part_name}"; filename="{part_value[2]}"' return f'form-data; name="{part_name}"' + _get_comtent_disposition = _get_content_disposition + def _write_parsable( self, writer: SerializationWriter, part_value: tuple[str, Any, Optional[str]] ) -> None: diff --git a/packages/abstractions/tests/test_multipart_body.py b/packages/abstractions/tests/test_multipart_body.py index d3f57fcc..8cfc5729 100644 --- a/packages/abstractions/tests/test_multipart_body.py +++ b/packages/abstractions/tests/test_multipart_body.py @@ -1,8 +1,13 @@ +import io +from unittest.mock import Mock, call + import pytest from kiota_abstractions.multipart_body import MultipartBody from kiota_abstractions.serialization import SerializationWriter +from .conftest import MockEntity + def test_defensive(): """Tests initialization of MultipartBody objects @@ -32,7 +37,8 @@ def test_defensive(): with pytest.raises(ValueError) as excinfo: multipart.serialize(None) assert "Serialization writer cannot be null" in str(excinfo.value) - + + def test_add_or_replace_part(): """Tests adding or replacing a part in the multipart body """ @@ -42,6 +48,7 @@ def test_add_or_replace_part(): multipart.add_or_replace_part("text", "text/plain", "Hello, World! 2") assert multipart.get_part_value("text") == "Hello, World! 2" + def test_remove_part(): """Tests removing a part from the multipart body """ @@ -50,4 +57,63 @@ def test_remove_part(): assert multipart.get_part_value("text") == "Hello, World!" assert multipart.remove_part("text") assert not multipart.get_part_value("text") - assert not multipart.remove_part("text") \ No newline at end of file + assert not multipart.remove_part("text") + + +def test_add_or_replace_part_empty_values(): + """Tests adding empty byte and string parts to the multipart body.""" + multipart = MultipartBody() + multipart.add_or_replace_part("empty_bytes", "application/octet-stream", b"") + assert multipart.get_part_value("empty_bytes") == b"" + + multipart.add_or_replace_part("empty_str", "text/plain", "") + assert multipart.get_part_value("empty_str") == "" + + multipart.add_or_replace_part("empty_stream", "application/octet-stream", io.BytesIO(b"")) + stream_val = multipart.get_part_value("empty_stream") + assert isinstance(stream_val, io.IOBase) + + mock_writer = Mock(spec=SerializationWriter) + multipart.serialize(mock_writer) + + # Verify that empty payloads reach the serialization writer + assert mock_writer.write_bytes_value.call_count == 3 + mock_writer.write_bytes_value.assert_has_calls([call("", b""), call("", b""), call("", b"")]) + + +def test_content_disposition(): + multipart = MultipartBody() + disp_with_file = multipart._get_content_disposition( + "doc", ("application/pdf", b"data", "doc.pdf") + ) + assert disp_with_file == 'form-data; name="doc"; filename="doc.pdf"' + + disp_without_file = multipart._get_content_disposition("text", ("text/plain", "hello", None)) + assert disp_without_file == 'form-data; name="text"' + + # Verify backward-compatible alias + assert multipart._get_comtent_disposition( + "doc", ("application/pdf", b"data", "doc.pdf") + ) == disp_with_file + + +def test_serialize_without_request_adapter(): + multipart = MultipartBody() + multipart.add_or_replace_part("text", "text/plain", "Hello") + multipart.add_or_replace_part("binary", "application/octet-stream", b"123") + multipart.add_or_replace_part("stream", "application/octet-stream", io.BytesIO(b"456")) + + mock_writer = Mock(spec=SerializationWriter) + multipart.serialize(mock_writer) + assert mock_writer.write_str_value.called + assert mock_writer.write_bytes_value.called + + +def test_serialize_parsable_requires_request_adapter(): + multipart = MultipartBody() + multipart.add_or_replace_part("entity", "application/json", MockEntity()) + + mock_writer = Mock(spec=SerializationWriter) + with pytest.raises(ValueError) as excinfo: + multipart.serialize(mock_writer) + assert "Request adapter or serialization writer factory cannot be null" in str(excinfo.value)