From 6afffdba5f7c7dacd3bf3e9c6045a94fdd7a704e Mon Sep 17 00:00:00 2001 From: Steven Loria Date: Tue, 16 Feb 2016 00:02:20 -0500 Subject: [PATCH] Add JSONIFY_MIMETYPE configuration variable Allow jsonify responses' mimetype to be configured --- CHANGES | 1 + docs/config.rst | 1 + flask/app.py | 1 + flask/json.py | 2 +- tests/test_basic.py | 12 ++++++++++++ 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 0337ec47e1..6df9e0f066 100644 --- a/CHANGES +++ b/CHANGES @@ -181,6 +181,7 @@ Released on June 13th 2013, codename Limoncello. information around which means that teardown handlers are able to distinguish error from success cases. - Added the ``JSONIFY_PRETTYPRINT_REGULAR`` configuration variable. +- Added the ``JSONIFY_MIMETYPE`` configuration variable. - Flask now orders JSON keys by default to not trash HTTP caches due to different hash seeds between different workers. - Added `appcontext_pushed` and `appcontext_popped` signals. diff --git a/docs/config.rst b/docs/config.rst index 245d3bab90..f5e207ae69 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -183,6 +183,7 @@ The following configuration values are used internally by Flask: if they are not requested by an XMLHttpRequest object (controlled by the ``X-Requested-With`` header) +``JSONIFY_MIMETYPE`` Mimetype used for jsonify responses. ``TEMPLATES_AUTO_RELOAD`` Whether to check for modifications of the template source and reload it automatically. By default the value is diff --git a/flask/app.py b/flask/app.py index da1998c630..bd50caf205 100644 --- a/flask/app.py +++ b/flask/app.py @@ -315,6 +315,7 @@ def _set_request_globals_class(self, value): 'JSON_AS_ASCII': True, 'JSON_SORT_KEYS': True, 'JSONIFY_PRETTYPRINT_REGULAR': True, + 'JSONIFY_MIMETYPE': 'application/json', 'TEMPLATES_AUTO_RELOAD': None, }) diff --git a/flask/json.py b/flask/json.py index cfc28c53a5..2bd4790231 100644 --- a/flask/json.py +++ b/flask/json.py @@ -264,7 +264,7 @@ def get_current_user(): return current_app.response_class( (dumps(data, indent=indent, separators=separators), '\n'), - mimetype='application/json' + mimetype=current_app.config['JSONIFY_MIMETYPE'] ) diff --git a/tests/test_basic.py b/tests/test_basic.py index d7cc7a3f0b..7ee8563af9 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -1029,6 +1029,18 @@ def test_jsonify_prettyprint(): assert rv.data == pretty_response +def test_jsonify_mimetype(): + app = flask.Flask(__name__) + app.config.update({"JSONIFY_MIMETYPE": 'application/vnd.api+json'}) + with app.test_request_context(): + msg = { + "msg": {"submsg": "W00t"}, + } + rv = flask.make_response( + flask.jsonify(msg), 200) + assert rv.mimetype == 'application/vnd.api+json' + + def test_url_generation(): app = flask.Flask(__name__)