From f4d0a1f9a015a041000ae20e02c5db8b9450cd69 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Thu, 19 Aug 2021 17:24:39 -0400 Subject: [PATCH 1/2] use azure json encoder for json input bodies --- sdk/core/azure-core/CHANGELOG.md | 2 ++ sdk/core/azure-core/azure/core/rest/_helpers.py | 3 ++- .../tests/testserver_tests/test_rest_http_request.py | 8 ++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index d9d8db2e1ada..ea9313de6dd7 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -4,6 +4,8 @@ ### Features Added +- We now use `azure.core.serialization.AzureJSONEncoder` to serialize `json` input to `azure.core.rest.HttpRequest`. + ### Breaking Changes ### Bugs Fixed diff --git a/sdk/core/azure-core/azure/core/rest/_helpers.py b/sdk/core/azure-core/azure/core/rest/_helpers.py index 4975d09d655c..968061d868c8 100644 --- a/sdk/core/azure-core/azure/core/rest/_helpers.py +++ b/sdk/core/azure-core/azure/core/rest/_helpers.py @@ -50,6 +50,7 @@ from urlparse import urlparse # type: ignore except ImportError: from urllib.parse import urlparse +from azure.core.serialization import AzureJSONEncoder ################################### TYPES SECTION ######################### @@ -182,7 +183,7 @@ def set_content_body(content): def set_json_body(json): # type: (Any) -> Tuple[Dict[str, str], Any] - body = dumps(json) + body = dumps(json, cls=AzureJSONEncoder) return { "Content-Type": "application/json", "Content-Length": str(len(body)) diff --git a/sdk/core/azure-core/tests/testserver_tests/test_rest_http_request.py b/sdk/core/azure-core/tests/testserver_tests/test_rest_http_request.py index a41a911c3a6d..d658d56f668b 100644 --- a/sdk/core/azure-core/tests/testserver_tests/test_rest_http_request.py +++ b/sdk/core/azure-core/tests/testserver_tests/test_rest_http_request.py @@ -277,6 +277,14 @@ def test_complicated_json(client): r = client.send_request(request) r.raise_for_status() +def test_use_custom_json_encoder(): + # this is to test we're using azure.core.serialization.AzureJSONEncoder + # to serialize our JSON objects + # since json can't serialize bytes by default but AzureJSONEncoder can, + # we pass in bytes and check that they are serialized + request = HttpRequest("GET", "/headers", json=bytearray("mybytes", "utf-8")) + assert request.content == "bXlieXRlcw==" + # 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 2552c19f289d6f9d65a92b14f8a9059a872e6050 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Fri, 20 Aug 2021 16:32:53 -0400 Subject: [PATCH 2/2] fix test --- .../azure-core/tests/testserver_tests/test_rest_http_request.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/core/azure-core/tests/testserver_tests/test_rest_http_request.py b/sdk/core/azure-core/tests/testserver_tests/test_rest_http_request.py index d658d56f668b..a7fe2d30a8b8 100644 --- a/sdk/core/azure-core/tests/testserver_tests/test_rest_http_request.py +++ b/sdk/core/azure-core/tests/testserver_tests/test_rest_http_request.py @@ -283,7 +283,7 @@ def test_use_custom_json_encoder(): # since json can't serialize bytes by default but AzureJSONEncoder can, # we pass in bytes and check that they are serialized request = HttpRequest("GET", "/headers", json=bytearray("mybytes", "utf-8")) - assert request.content == "bXlieXRlcw==" + assert request.content == '"bXlieXRlcw=="' # 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():