From d2c86942fe68f21a9612f65f8bbffde847fcda8f Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Fri, 29 Oct 2021 17:25:02 -0700 Subject: [PATCH 1/4] add json file descriptor tests --- .../azure-core/tests/test_rest_http_request.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/sdk/core/azure-core/tests/test_rest_http_request.py b/sdk/core/azure-core/tests/test_rest_http_request.py index 70c184b5cc3d..4d1d796a341d 100644 --- a/sdk/core/azure-core/tests/test_rest_http_request.py +++ b/sdk/core/azure-core/tests/test_rest_http_request.py @@ -417,6 +417,23 @@ def on_request(self, pipeline_request): # work assert "I entered the policies!" in str(ex.value) +def test_json_file_valid(): + json_bytes = bytearray('{"more": "cowbell"}', encoding='utf-8') + with io.BytesIO(json_bytes) as json_file: + request = HttpRequest("PUT", "/fake", json=json_file) + assert request.headers == {"Content-Type": "application/json"} + assert request.content == json_file + assert not request.content.closed + assert request.content.read() == b'{"more": "cowbell"}' + +def test_json_file_invalid(): + json_bytes = bytearray('{"more": "cowbell" i am not valid', encoding='utf-8') + with io.BytesIO(json_bytes) as json_file: + request = HttpRequest("PUT", "/fake", json=json_file) + assert request.headers == {"Content-Type": "application/json"} + assert request.content == json_file + assert not request.content.closed + assert request.content.read() == b'{"more": "cowbell" i am not valid' # NOTE: For files, we don't allow list of tuples yet, just dict. Will uncomment when we add this capability # def test_multipart_multiple_files_single_input_content(): From cdacf02486757e4e70fc9e65d7d96dc5683e25ed Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Fri, 29 Oct 2021 17:34:10 -0700 Subject: [PATCH 2/4] allow readable objects to go into json kwarg --- sdk/core/azure-core/azure/core/rest/_helpers.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/sdk/core/azure-core/azure/core/rest/_helpers.py b/sdk/core/azure-core/azure/core/rest/_helpers.py index 934bfa613c76..14969bab3647 100644 --- a/sdk/core/azure-core/azure/core/rest/_helpers.py +++ b/sdk/core/azure-core/azure/core/rest/_helpers.py @@ -157,11 +157,14 @@ def set_content_body(content): def set_json_body(json): # type: (Any) -> Tuple[Dict[str, str], Any] - body = dumps(json, cls=AzureJSONEncoder) - return { - "Content-Type": "application/json", - "Content-Length": str(len(body)) - }, body + headers = {"Content-Type": "application/json"} + if hasattr(json, "read"): + content_headers, body = set_content_body(json) + headers.update(content_headers) + else: + body = dumps(json, cls=AzureJSONEncoder) + headers.update({"Content-Length": str(len(body))}) + return headers, body def lookup_encoding(encoding): # type: (str) -> bool From d827191bf1945f7ff25b35cc4082e9b1a04dbf0b Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Fri, 29 Oct 2021 17:36:58 -0700 Subject: [PATCH 3/4] add don't override content type test --- sdk/core/azure-core/tests/test_rest_http_request.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sdk/core/azure-core/tests/test_rest_http_request.py b/sdk/core/azure-core/tests/test_rest_http_request.py index 4d1d796a341d..6aae3f3e4c16 100644 --- a/sdk/core/azure-core/tests/test_rest_http_request.py +++ b/sdk/core/azure-core/tests/test_rest_http_request.py @@ -435,6 +435,15 @@ def test_json_file_invalid(): assert not request.content.closed assert request.content.read() == b'{"more": "cowbell" i am not valid' +def test_json_file_content_type_input(): + json_bytes = bytearray('{"more": "cowbell"}', encoding='utf-8') + with io.BytesIO(json_bytes) as json_file: + request = HttpRequest("PUT", "/fake", json=json_file, headers={"Content-Type": "application/json-special"}) + assert request.headers == {"Content-Type": "application/json-special"} + assert request.content == json_file + assert not request.content.closed + assert request.content.read() == b'{"more": "cowbell"}' + # NOTE: For files, we don't allow list of tuples yet, just dict. Will uncomment when we add this capability # def test_multipart_multiple_files_single_input_content(): # files = [ From c223e37052cc722342a9123ee4f82c414f72d7a8 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 2 Nov 2021 13:34:25 -0700 Subject: [PATCH 4/4] update changelog --- sdk/core/azure-core/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index 6b1bbf9ba9a2..1299664c68f7 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -5,6 +5,7 @@ ### Features Added - add kwargs to the methods for `iter_raw` and `iter_bytes` #21529 +- no longer raise JSON errors if users pass in file descriptors of JSON to the `json` kwarg in `HttpRequest` #21504 ### Breaking Changes