Skip to content

fix BatchRequestItem now properly serializes the header and body fields #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/msgraph_core/requests/batch_request_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,19 +258,16 @@ def serialize(self, writer: SerializationWriter) -> None:
writer.write_str_value('method', self.method)
writer.write_str_value('url', self.url)
writer.write_collection_of_primitive_values('depends_on', self._depends_on)
writer.write_collection_of_object_values(
'headers',
self._headers # type: ignore # need method to serialize dicts
writer.write_additional_data_value(
{'headers': self._headers} # need proper method to serialize dicts
)
if self._body:
json_object = json.loads(self._body)
is_json_string = json_object and isinstance(json_object, dict)
# /$batch API expects JSON object or base 64 encoded value for the body
if is_json_string:
writer.write_collection_of_object_values( # type: ignore
# need method to serialize dicts
'body',
json_object
writer.write_additional_data_value(
{'body': json_object} # need proper method to serialize dicts
)
else:
writer.write_str_value('body', base64.b64encode(self._body).decode('utf-8'))
15 changes: 13 additions & 2 deletions tests/requests/test_batch_request_item.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import pytest
import json
from io import BytesIO
from unittest.mock import Mock
from urllib.request import Request
from kiota_abstractions.request_information import RequestInformation
from kiota_abstractions.serialization.serialization_writer import SerializationWriter
from kiota_abstractions.method import Method
from kiota_abstractions.headers_collection import HeadersCollection as RequestHeaders
from msgraph_core.requests.batch_request_item import BatchRequestItem
Expand All @@ -15,7 +18,7 @@ def request_info():
request_info.http_method = "GET"
request_info.url = "f{base_url}/me"
request_info.headers = RequestHeaders()
request_info.content = BytesIO(b'{"key": "value"}')
request_info.content = b'{"key": "value"}'
return request_info


Expand All @@ -27,7 +30,7 @@ def batch_request_item(request_info):
def test_initialization(batch_request_item, request_info):
assert batch_request_item.method == "GET"
assert batch_request_item.url == "f{base_url}/me"
assert batch_request_item.body.read() == b'{"key": "value"}'
assert batch_request_item.body == b'{"key": "value"}'


def test_create_with_urllib_request():
Expand Down Expand Up @@ -123,3 +126,11 @@ def test_batch_request_item_method_enum():
def test_depends_on_property(batch_request_item):
batch_request_item.set_depends_on(["request1", "request2"])
assert batch_request_item.depends_on == ["request1", "request2"]


def test_serialize(batch_request_item):
writer = Mock(spec=SerializationWriter)
batch_request_item.serialize(writer)
writer.write_additional_data_value.assert_any_call({'headers': batch_request_item._headers})
json_object = json.loads(batch_request_item._body)
writer.write_additional_data_value.assert_called_with({'body': json_object})