From 0dac6fd4340c9529b52e6ebd8cbbc59d2670680f Mon Sep 17 00:00:00 2001 From: iscai-msft <43154838+iscai-msft@users.noreply.github.com> Date: Fri, 20 Aug 2021 19:16:58 -0400 Subject: [PATCH] [rest] use azure json encoder for json input bodies (#20361) --- 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 16165e58c6adf..811230155bbbd 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 in the Provisional `azure.core.rest` package - The `text` property on `azure.core.rest.HttpResponse` and `azure.core.rest.AsyncHttpResponse` has changed to a method, which also takes diff --git a/sdk/core/azure-core/azure/core/rest/_helpers.py b/sdk/core/azure-core/azure/core/rest/_helpers.py index a2f8736dcc68c..1a011689a2389 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 a41a911c3a6d4..a7fe2d30a8b83 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 = [