diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index 16165e58c6ad..811230155bbb 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 a2f8736dcc68..1a011689a238 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..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 @@ -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 = [