Skip to content

Commit f0e6a38

Browse files
lmazuelrakshith91
authored andcommitted
Accept extension of JSON content-type (Azure#6583)
* Accept extension of JSON content-type * Adding text/something+json test * Support digit
1 parent 3457403 commit f0e6a38

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

sdk/core/azure-core/azure/core/pipeline/policies/universal.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,9 @@ def on_response(self, request, response):
266266
class ContentDecodePolicy(SansIOHTTPPolicy):
267267
"""Policy for decoding unstreamed response content.
268268
"""
269-
JSON_MIMETYPES = [
270-
'application/json',
271-
'text/json' # Because we're open minded people...
272-
]
269+
# Accept "text" because we're open minded people...
270+
JSON_REGEXP = re.compile(r'^(application|text)/([0-9a-z+.]+\+)?json$')
271+
273272
# Name used in context
274273
CONTEXT_NAME = "deserialized_data"
275274

@@ -301,7 +300,7 @@ def deserialize_from_text(cls, response, content_type=None):
301300
if content_type is None:
302301
return data
303302

304-
if content_type in cls.JSON_MIMETYPES:
303+
if cls.JSON_REGEXP.match(content_type):
305304
try:
306305
return json.loads(data_as_str)
307306
except ValueError as err:

sdk/core/azure-core/tests/test_universal_pipeline.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ def body(self):
160160
result = response.context["deserialized_data"]
161161
assert result["success"] is True
162162

163+
# Simple JSON with complex content_type
164+
response = build_response(b'{"success": true}', content_type="application/vnd.microsoft.appconfig.kv.v1+json")
165+
raw_deserializer.on_response(None, response)
166+
result = response.context["deserialized_data"]
167+
assert result["success"] is True
168+
169+
# Simple JSON with complex content_type, v2
170+
response = build_response(b'{"success": true}', content_type="text/vnd.microsoft.appconfig.kv.v1+json")
171+
raw_deserializer.on_response(None, response)
172+
result = response.context["deserialized_data"]
173+
assert result["success"] is True
174+
163175
# For compat, if no content-type, decode JSON
164176
response = build_response(b'"data"')
165177
raw_deserializer.on_response(None, response)

0 commit comments

Comments
 (0)