From d3d96df297e159f6273cc55fee920d8e099fb768 Mon Sep 17 00:00:00 2001 From: Laurent Mazuel Date: Wed, 31 Jul 2019 15:17:51 -0700 Subject: [PATCH] Accept extension of JSON content-type (#6583) * Accept extension of JSON content-type * Adding text/something+json test * Support digit --- .../azure/core/pipeline/policies/universal.py | 9 ++++----- sdk/core/azure-core/tests/test_universal_pipeline.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/universal.py b/sdk/core/azure-core/azure/core/pipeline/policies/universal.py index b9338950ff38..77ffd69c6a39 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/universal.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/universal.py @@ -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" @@ -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: diff --git a/sdk/core/azure-core/tests/test_universal_pipeline.py b/sdk/core/azure-core/tests/test_universal_pipeline.py index cc783b0d4c74..035a462d388a 100644 --- a/sdk/core/azure-core/tests/test_universal_pipeline.py +++ b/sdk/core/azure-core/tests/test_universal_pipeline.py @@ -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)