Skip to content

Commit

Permalink
Accept extension of JSON content-type (#6583)
Browse files Browse the repository at this point in the history
* Accept extension of JSON content-type

* Adding text/something+json test

* Support digit
  • Loading branch information
lmazuel authored Jul 31, 2019
1 parent c6ebc93 commit d3d96df
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
9 changes: 4 additions & 5 deletions sdk/core/azure-core/azure/core/pipeline/policies/universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,9 @@ def on_response(self, request, response):
class ContentDecodePolicy(SansIOHTTPPolicy):
"""Policy for decoding unstreamed response content.
"""
JSON_MIMETYPES = [
'application/json',
'text/json' # Because we're open minded people...
]
# Accept "text" because we're open minded people...
JSON_REGEXP = re.compile(r'^(application|text)/([0-9a-z+.]+\+)?json$')

# Name used in context
CONTEXT_NAME = "deserialized_data"

Expand Down Expand Up @@ -301,7 +300,7 @@ def deserialize_from_text(cls, response, content_type=None):
if content_type is None:
return data

if content_type in cls.JSON_MIMETYPES:
if cls.JSON_REGEXP.match(content_type):
try:
return json.loads(data_as_str)
except ValueError as err:
Expand Down
12 changes: 12 additions & 0 deletions sdk/core/azure-core/tests/test_universal_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ def body(self):
result = response.context["deserialized_data"]
assert result["success"] is True

# Simple JSON with complex content_type
response = build_response(b'{"success": true}', content_type="application/vnd.microsoft.appconfig.kv.v1+json")
raw_deserializer.on_response(None, response)
result = response.context["deserialized_data"]
assert result["success"] is True

# Simple JSON with complex content_type, v2
response = build_response(b'{"success": true}', content_type="text/vnd.microsoft.appconfig.kv.v1+json")
raw_deserializer.on_response(None, response)
result = response.context["deserialized_data"]
assert result["success"] is True

# For compat, if no content-type, decode JSON
response = build_response(b'"data"')
raw_deserializer.on_response(None, response)
Expand Down

0 comments on commit d3d96df

Please sign in to comment.