From a7f1a21c1204828388eaed1e3903a74c904c8147 Mon Sep 17 00:00:00 2001 From: Hsiaoming Yang Date: Tue, 7 Mar 2017 10:09:46 +0900 Subject: [PATCH] Don't rely on X-Requested-With for pretty print json response (#2193) * Don't rely on X-Requested-With for pretty print json response * Fix test cases for pretty print json patch * Fix gramma error in docs for pretty print json config * Add changelog for JSONIFY_PRETTYPRINT_REGULAR --- CHANGES | 3 +++ docs/config.rst | 8 +++----- flask/app.py | 2 +- flask/json.py | 2 +- tests/test_basic.py | 2 +- tests/test_helpers.py | 2 ++ 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CHANGES b/CHANGES index 2c0a4869..7c50d0c7 100644 --- a/CHANGES +++ b/CHANGES @@ -11,6 +11,9 @@ Major release, unreleased - Make `app.run()` into a noop if a Flask application is run from the development server on the command line. This avoids some behavior that was confusing to debug for newcomers. +- Change default configuration `JSONIFY_PRETTYPRINT_REGULAR=False`. jsonify() + method returns compressed response by default, and pretty response in + debug mode. Version 0.12.1 -------------- diff --git a/docs/config.rst b/docs/config.rst index c36cc852..75ce239a 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -194,11 +194,9 @@ The following configuration values are used internally by Flask: This is not recommended but might give you a performance improvement on the cost of cacheability. -``JSONIFY_PRETTYPRINT_REGULAR`` If this is set to ``True`` (the default) - jsonify responses will be pretty printed - if they are not requested by an - XMLHttpRequest object (controlled by - the ``X-Requested-With`` header) +``JSONIFY_PRETTYPRINT_REGULAR`` If this is set to ``True`` or the Flask app + is running in debug mode, jsonify responses + will be pretty printed. ``JSONIFY_MIMETYPE`` MIME type used for jsonify responses. ``TEMPLATES_AUTO_RELOAD`` Whether to check for modifications of the template source and reload it diff --git a/flask/app.py b/flask/app.py index 7745ace6..bc38d9ea 100644 --- a/flask/app.py +++ b/flask/app.py @@ -314,7 +314,7 @@ class Flask(_PackageBoundObject): 'PREFERRED_URL_SCHEME': 'http', 'JSON_AS_ASCII': True, 'JSON_SORT_KEYS': True, - 'JSONIFY_PRETTYPRINT_REGULAR': True, + 'JSONIFY_PRETTYPRINT_REGULAR': False, 'JSONIFY_MIMETYPE': 'application/json', 'TEMPLATES_AUTO_RELOAD': None, }) diff --git a/flask/json.py b/flask/json.py index 19b337c3..0ba9d717 100644 --- a/flask/json.py +++ b/flask/json.py @@ -248,7 +248,7 @@ def jsonify(*args, **kwargs): indent = None separators = (',', ':') - if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] and not request.is_xhr: + if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] or current_app.debug: indent = 2 separators = (', ', ': ') diff --git a/tests/test_basic.py b/tests/test_basic.py index 6341234b..942eb0f6 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -995,7 +995,7 @@ def test_make_response_with_response_instance(): rv = flask.make_response( flask.jsonify({'msg': 'W00t'}), 400) assert rv.status_code == 400 - assert rv.data == b'{\n "msg": "W00t"\n}\n' + assert rv.data == b'{"msg":"W00t"}\n' assert rv.mimetype == 'application/json' rv = flask.make_response( diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 3e2ea8cd..fd448fb8 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -289,6 +289,8 @@ class TestJSON(object): def test_json_key_sorting(self): app = flask.Flask(__name__) app.testing = True + app.debug = True + assert app.config['JSON_SORT_KEYS'] == True d = dict.fromkeys(range(20), 'foo')