Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rest] use azure json encoder for json input bodies #20361

Merged
merged 3 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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