get_json separate cache depending on silent arg
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by fphonor · committed by David Lord · parent 8c0d8c1 · Browse files · 3 files·+30 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+30 added−6 removed
- tests:+15 added−0 removed
- other:+15 added−6 removed
Provenance
- Commit
171eb28- Tree
e8c7546- Author
fphonor <wangxulin108@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
8c0d8c1
3 files changed+30 added−6 removed
modified:flask/wrappers.py+12 added-6 removed
21 unmodified lines22 23 .. versionadded:: 1.024 """25 26- _cached_json = Ellipsis26+ _cached_json = (Ellipsis, Ellipsis)27 28 @property29 def is_json(self):30 """Check if the mimetype indicates JSON data, either28 unmodified lines59 :param force: Ignore the mimetype and always try to parse JSON.60 :param silent: Silence parsing errors and return ``None`` instead.61 :param cache: Store the parsed JSON to return for subsequent calls.62 """63- if cache and self._cached_json is not Ellipsis:64- return self._cached_json63+ if cache and self._cached_json[silent] is not Ellipsis:64+ return self._cached_json[silent]65 66 if not (force or self.is_json):67 return None68 7 unmodified lines76 rv = json.loads(data, encoding=charset)77 except ValueError as e:78 if silent:79 rv = None80+ if cache:81+ normal_rv, _ = self._cached_json82+ self._cached_json = (normal_rv, rv)83 else:84 rv = self.on_json_loading_failed(e)85+ if cache:86+ _, silent_rv = self._cached_json87+ self._cached_json = (rv, silent_rv)88+ else:89+ if cache:90+ self._cached_json = (rv, rv)91 83- if cache:84- self._cached_json = rv85-92 return rv93 94 def on_json_loading_failed(self, e):95 """Called if :meth:`get_json` parsing fails and isn't silenced. If106 unmodified linesmodified:tests/test_helpers.py+15 added
60 unmodified lines61 assert flask.request.get_json(silent=True, cache=True) is None62 with pytest.raises(BadRequest):63 flask.request.get_json(silent=False, cache=False)64 65+ def test_different_silent_on_bad_request(self, app):66+ with app.test_request_context(67+ '/', method='POST', data='malformed',68+ content_type='application/json'):69+ assert flask.request.get_json(silent=True) is None70+ with pytest.raises(BadRequest):71+ flask.request.get_json(silent=False)72+73+ def test_different_silent_on_normal_request(self, app):74+ with app.test_request_context('/', method='POST', json={'foo': 'bar'}):75+ silent_rv = flask.request.get_json(silent=True)76+ normal_rv = flask.request.get_json(silent=False)77+ assert silent_rv is normal_rv78+ assert normal_rv['foo'] == 'bar'79+80 def test_post_empty_json_adds_exception_to_response_content_in_debug(self, app, client):81 app.config['DEBUG'] = True82 app.config['TRAP_BAD_REQUEST_ERRORS'] = False83 856 unmodified linesmodified:CHANGES.rst+3 added
143 unmodified lines144 passing ``subdomain_matching=True`` to the ``Flask`` constructor.145 (`#2635`_)146 - A single trailing slash is stripped from the blueprint ``url_prefix``147 when it is registered with the app. (`#2629`_)148+- :meth:`Request.get_json() <flask.Request.get_json>` doesn't cache the149+ result if parsing fails when ``silent`` is true. (`#2651`_)150 151 .. _pallets/meta#24: https://github.com/pallets/meta/issues/24152 .. _#1421: https://github.com/pallets/flask/issues/1421153 .. _#1489: https://github.com/pallets/flask/pull/148936 unmodified lines190 .. _#2607: https://github.com/pallets/flask/pull/2607191 .. _#2636: https://github.com/pallets/flask/pull/2636192 .. _#2635: https://github.com/pallets/flask/pull/2635193 .. _#2629: https://github.com/pallets/flask/pull/2629194+.. _#2651: https://github.com/pallets/flask/issues/2651195 196 197 Version 0.12.2198 --------------618 unmodified lines