diff --git a/CHANGES b/CHANGES index 48dcac90f..c97bb56e5 100644 --- a/CHANGES +++ b/CHANGES @@ -8,19 +8,25 @@ Development Version 0.8 ~~~~~~~~~~~ +- New: ``JSON_REQUEST_CONTENT_TYPES`` or supported JSON content types. Useful + when you need support for vendor-specific json types. Please note: responses + will still carry the standard ``application/json`` type. Defaults to + ``['application/json']``. Closes #1024. - New: ``ALLOW_CUSTOM_FIELDS_IN_GEOJSON`` allows custom fields in GeoJSON (Martin Fous). - New: Support for ``Feature`` and ``FeatureCollection`` GeoJSON objects. Closes #769 (Martin Fous). - Config options ``MONGO_AUTH_MECHANISM`` and ``MONGO_AUTH_MECHANISM_PROPERTIES`` added. -- Change: Support for Cerberus 1.0+. Closes #776 (Dominik Kellner, Brad P. Crochet). +- Change: Support for Cerberus 1.0+. Closes #776 (Dominik Kellner, Brad P. + Crochet). - Change: Drop Flask-PyMongo dependency. Closes #855 (Artem Kolesnikov). - Docs: code snippets are now Python 3 compatibile (Pahaz Blinov). - Dev: after the latest update (May 4th) travis-ci would not run tests on Python 2.6. - Dev: all branches are now tested on travis-ci. Previously, only 'master' was being tested. +- Dev: fix insidious bug in ``tests.methods.post.TestPost`` class. - Update: Upgrade Events dependency to v0.3. Breaking Changes diff --git a/docs/config.rst b/docs/config.rst index dda24d0e2..068a51ab8 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -487,6 +487,12 @@ uppercase. ``JSON_SORT_KEYS`` ``True`` to enable JSON key sorting, ``False`` otherwise. Defaults to ``False``. +``JSON_REQUEST_CONTENT_TYPES`` Supported JSON content types. Useful when + you need support for vendor-specific json + types. Please note: responses will still + carry the standard ``application/json`` + type. Defaults to ``['application/json']``. + ``VALIDATION_ERROR_STATUS`` The HTTP status code to use for validation errors. Defaults to ``422``. diff --git a/eve/default_settings.py b/eve/default_settings.py index 732fc638b..4e4cb68d7 100644 --- a/eve/default_settings.py +++ b/eve/default_settings.py @@ -207,6 +207,7 @@ MULTIPART_FORM_FIELDS_AS_JSON = False AUTO_COLLAPSE_MULTI_KEYS = False AUTO_CREATE_LISTS = False +JSON_REQUEST_CONTENT_TYPES = ['application/json'] SCHEMA_ENDPOINT = None diff --git a/eve/methods/common.py b/eve/methods/common.py index d816a5b6c..43006600d 100644 --- a/eve/methods/common.py +++ b/eve/methods/common.py @@ -161,8 +161,8 @@ def payload(): """ content_type = request.headers.get('Content-Type', '').split(';')[0] - if content_type == 'application/json': - return request.get_json() + if content_type in config.JSON_REQUEST_CONTENT_TYPES: + return request.get_json(force=True) elif content_type == 'application/x-www-form-urlencoded': return multidict_to_dict(request.form) if len(request.form) else \ abort(400, description='No form-urlencoded data supplied') diff --git a/eve/render.py b/eve/render.py index 3e3b2f8d6..aea46317e 100644 --- a/eve/render.py +++ b/eve/render.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*-) +# -*- coding: utf-8 -*- """ eve.render diff --git a/eve/tests/config.py b/eve/tests/config.py index d02a41991..181269632 100644 --- a/eve/tests/config.py +++ b/eve/tests/config.py @@ -87,6 +87,8 @@ def test_default_settings(self): self.assertEqual(self.app.config['STANDARD_ERRORS'], [400, 401, 404, 405, 406, 409, 410, 412, 422, 428]) self.assertEqual(self.app.config['UPSERT_ON_PUT'], True) + self.assertEqual(self.app.config['JSON_REQUEST_CONTENT_TYPES'], + ['application/json']) def test_settings_as_dict(self): my_settings = {'API_VERSION': 'override!', 'DOMAIN': {'contacts': {}}} diff --git a/eve/tests/methods/post.py b/eve/tests/methods/post.py index a05fb3dc3..feb02a0ef 100644 --- a/eve/tests/methods/post.py +++ b/eve/tests/methods/post.py @@ -875,6 +875,18 @@ def test_post_location_header_hateoas_off(self): self.assertTrue('Location' in r.headers) self.assertTrue(self.known_resource_url in r.headers['Location']) + def test_post_custom_json_content_type(self): + data = {'ref': '1234567890123456789054321'} + r, status = self.post(self.known_resource_url, data, + content_type='application/csp-report') + self.assert400(status) + + self.app.config['JSON_REQUEST_CONTENT_TYPES'] += \ + ['application/csp-report'] + r, status = self.post(self.known_resource_url, data, + content_type='application/csp-report') + self.assert201(status) + def perform_post(self, data, valid_items=[0]): r, status = self.post(self.known_resource_url, data=data) self.assert201(status) @@ -924,7 +936,9 @@ def compare_post_with_get(self, item_id, fields): else: return item[fields] - def post(self, url, data, headers=[], content_type='application/json'): + def post(self, url, data, headers=None, content_type='application/json'): + if not headers: + headers=[] headers.append(('Content-Type', content_type)) r = self.test_client.post(url, data=json.dumps(data), headers=headers) return self.parse_response(r)