Skip to content

Commit

Permalink
Support extension of JSON mimetype (#167)
Browse files Browse the repository at this point in the history
* Support extension of JSON mimetype

* Py2.7 compat
  • Loading branch information
lmazuel authored Jul 24, 2019
1 parent c4086bf commit 4cc8bc8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 5 additions & 5 deletions msrest/pipeline/universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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.match(content_type):
try:
return json.loads(data_as_str)
except ValueError as err:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_universal_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4cc8bc8

Please sign in to comment.