Skip to content

Commit

Permalink
[rest] use azure json encoder for json input bodies (#20361)
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft committed Sep 29, 2021
1 parent 91c9144 commit 608db2d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion sdk/core/azure-core/azure/core/rest/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 #########################

Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 608db2d

Please sign in to comment.