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
This commit is contained in:
Hsiaoming Yang 2017-03-07 10:09:46 +09:00 committed by GitHub
parent f5adb61b28
commit a7f1a21c12
6 changed files with 11 additions and 8 deletions

View file

@ -11,6 +11,9 @@ Major release, unreleased
- Make `app.run()` into a noop if a Flask application is run from the - 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 development server on the command line. This avoids some behavior that
was confusing to debug for newcomers. 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 Version 0.12.1
-------------- --------------

View file

@ -194,11 +194,9 @@ The following configuration values are used internally by Flask:
This is not recommended but might give This is not recommended but might give
you a performance improvement on the you a performance improvement on the
cost of cacheability. cost of cacheability.
``JSONIFY_PRETTYPRINT_REGULAR`` If this is set to ``True`` (the default) ``JSONIFY_PRETTYPRINT_REGULAR`` If this is set to ``True`` or the Flask app
jsonify responses will be pretty printed is running in debug mode, jsonify responses
if they are not requested by an will be pretty printed.
XMLHttpRequest object (controlled by
the ``X-Requested-With`` header)
``JSONIFY_MIMETYPE`` MIME type used for jsonify responses. ``JSONIFY_MIMETYPE`` MIME type used for jsonify responses.
``TEMPLATES_AUTO_RELOAD`` Whether to check for modifications of ``TEMPLATES_AUTO_RELOAD`` Whether to check for modifications of
the template source and reload it the template source and reload it

View file

@ -314,7 +314,7 @@ class Flask(_PackageBoundObject):
'PREFERRED_URL_SCHEME': 'http', 'PREFERRED_URL_SCHEME': 'http',
'JSON_AS_ASCII': True, 'JSON_AS_ASCII': True,
'JSON_SORT_KEYS': True, 'JSON_SORT_KEYS': True,
'JSONIFY_PRETTYPRINT_REGULAR': True, 'JSONIFY_PRETTYPRINT_REGULAR': False,
'JSONIFY_MIMETYPE': 'application/json', 'JSONIFY_MIMETYPE': 'application/json',
'TEMPLATES_AUTO_RELOAD': None, 'TEMPLATES_AUTO_RELOAD': None,
}) })

View file

@ -248,7 +248,7 @@ def jsonify(*args, **kwargs):
indent = None indent = None
separators = (',', ':') 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 indent = 2
separators = (', ', ': ') separators = (', ', ': ')

View file

@ -995,7 +995,7 @@ def test_make_response_with_response_instance():
rv = flask.make_response( rv = flask.make_response(
flask.jsonify({'msg': 'W00t'}), 400) flask.jsonify({'msg': 'W00t'}), 400)
assert rv.status_code == 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' assert rv.mimetype == 'application/json'
rv = flask.make_response( rv = flask.make_response(

View file

@ -289,6 +289,8 @@ class TestJSON(object):
def test_json_key_sorting(self): def test_json_key_sorting(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.testing = True app.testing = True
app.debug = True
assert app.config['JSON_SORT_KEYS'] == True assert app.config['JSON_SORT_KEYS'] == True
d = dict.fromkeys(range(20), 'foo') d = dict.fromkeys(range(20), 'foo')