Skip to content

Commit 608db2d

Browse files
committed
[rest] use azure json encoder for json input bodies (#20361)
1 parent 91c9144 commit 608db2d

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

sdk/core/azure-core/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Features Added
66

7+
- We now use `azure.core.serialization.AzureJSONEncoder` to serialize `json` input to `azure.core.rest.HttpRequest`.
8+
79
### Breaking Changes in the Provisional `azure.core.rest` package
810

911
- The `text` property on `azure.core.rest.HttpResponse` and `azure.core.rest.AsyncHttpResponse` has changed to a method, which also takes

sdk/core/azure-core/azure/core/rest/_helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
from urlparse import urlparse # type: ignore
5151
except ImportError:
5252
from urllib.parse import urlparse
53+
from azure.core.serialization import AzureJSONEncoder
5354

5455
################################### TYPES SECTION #########################
5556

@@ -182,7 +183,7 @@ def set_content_body(content):
182183

183184
def set_json_body(json):
184185
# type: (Any) -> Tuple[Dict[str, str], Any]
185-
body = dumps(json)
186+
body = dumps(json, cls=AzureJSONEncoder)
186187
return {
187188
"Content-Type": "application/json",
188189
"Content-Length": str(len(body))

sdk/core/azure-core/tests/testserver_tests/test_rest_http_request.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,14 @@ def test_complicated_json(client):
277277
r = client.send_request(request)
278278
r.raise_for_status()
279279

280+
def test_use_custom_json_encoder():
281+
# this is to test we're using azure.core.serialization.AzureJSONEncoder
282+
# to serialize our JSON objects
283+
# since json can't serialize bytes by default but AzureJSONEncoder can,
284+
# we pass in bytes and check that they are serialized
285+
request = HttpRequest("GET", "/headers", json=bytearray("mybytes", "utf-8"))
286+
assert request.content == '"bXlieXRlcw=="'
287+
280288
# NOTE: For files, we don't allow list of tuples yet, just dict. Will uncomment when we add this capability
281289
# def test_multipart_multiple_files_single_input_content():
282290
# files = [

0 commit comments

Comments
 (0)