From d2306e849e9f3031f521d38f9029ddf3eca5389c Mon Sep 17 00:00:00 2001 From: Laurent Mazuel Date: Fri, 5 Jul 2019 12:25:09 -0700 Subject: [PATCH 1/2] Support extension of JSON mimetype --- msrest/pipeline/universal.py | 10 +++++----- tests/test_universal_pipeline.py | 6 ++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/msrest/pipeline/universal.py b/msrest/pipeline/universal.py index f124ea781c..bbc344673a 100644 --- a/msrest/pipeline/universal.py +++ b/msrest/pipeline/universal.py @@ -32,6 +32,7 @@ import xml.etree.ElementTree as ET import platform import codecs +import re from typing import Mapping, Any, Optional, AnyStr, Union, IO, cast, TYPE_CHECKING # pylint: disable=unused-import @@ -129,10 +130,9 @@ def on_response(self, request, response, **kwargs): class RawDeserializer(SansIOHTTPPolicy): - 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)/([a-z+.]+\+)?json') + # Name used in context CONTEXT_NAME = "deserialized_data" @@ -165,7 +165,7 @@ def deserialize_from_text(cls, data, content_type=None): if content_type is None: return data - if content_type in cls.JSON_MIMETYPES: + if cls.JSON_REGEXP.fullmatch(content_type): try: return json.loads(data_as_str) except ValueError as err: diff --git a/tests/test_universal_pipeline.py b/tests/test_universal_pipeline.py index 2568e34b6f..cd92ca779a 100644 --- a/tests/test_universal_pipeline.py +++ b/tests/test_universal_pipeline.py @@ -151,6 +151,12 @@ 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+json") + raw_deserializer.on_response(None, response, stream=False) + result = response.context["deserialized_data"] + assert result["success"] is True + # JSON with UTF-8 BOM response = build_response(b'\xef\xbb\xbf{"success": true}', content_type="application/json; charset=utf-8") raw_deserializer.on_response(None, response, stream=False) From 852f412749089dd3c609718dbefb31f6fe52fb31 Mon Sep 17 00:00:00 2001 From: Laurent Mazuel Date: Wed, 24 Jul 2019 15:35:17 -0700 Subject: [PATCH 2/2] Py2.7 compat --- msrest/pipeline/universal.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/msrest/pipeline/universal.py b/msrest/pipeline/universal.py index bbc344673a..b8dc40c971 100644 --- a/msrest/pipeline/universal.py +++ b/msrest/pipeline/universal.py @@ -131,7 +131,7 @@ def on_response(self, request, response, **kwargs): class RawDeserializer(SansIOHTTPPolicy): # Accept "text" because we're open minded people... - JSON_REGEXP = re.compile(r'(application|text)/([a-z+.]+\+)?json') + JSON_REGEXP = re.compile(r'^(application|text)/([a-z+.]+\+)?json$') # Name used in context CONTEXT_NAME = "deserialized_data" @@ -165,7 +165,7 @@ def deserialize_from_text(cls, data, content_type=None): if content_type is None: return data - if cls.JSON_REGEXP.fullmatch(content_type): + if cls.JSON_REGEXP.match(content_type): try: return json.loads(data_as_str) except ValueError as err: