-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache Param For get_json() Leading to Contradictory Behavior #1698
Comments
I'd recommend not mixing |
@davidism Wouldn't it be any time you perform I guess I'm just trying to say, |
But why are you performing that set/get on a response object multiple times? Response.get_json is pretty much intended for use during testing. |
If it's purely for testing, then it makes more sense. Although, for behavior to change based off of whether something was called or not seems a bit weird. And, if it's going to cache (as it does) one would hope it'd update along with the underlying data. That's why I put the word contradictory in the title. As for why I'm performing the multiple gets with a set in between, it's actually a single get in the actual code, a single set in the actual code, but then a third get in the unit test.
However, it's then my unit test which will perform a get_json() on the response of this function (to assert the change did indeed occur), but will fail. |
It probably doesn't make sense to cache |
So, from a purist perspective, that |
This will be fixed as part of #1963, I'll move the JSON behavior into the |
The location of interest:
werkzeug/src/werkzeug/wrappers/json.py
Line 47 in 0bf2e3e
Example:
In the above example, due to the
set_data
, the data changes. Usingget_json(cache=False)
works because it'll re-parses the data. However, sincecache = False
, this value will not be placed into the cache for the future. If I hadn't usedget_json(cache=False)
, it would've just returned the cached value (which is wrong).Therefore, it seems that performing
set_data
, makes it so that every subsequent call would requireget_json(cache=False)
. My how would be something likeget_json(reparse=True)
and then all subsequently calls can just be from the cache.Now, what the cache parameter represents becomes important, does it mean "cache the current value" or "return the currently cached value". The issue I see is that it's playing both roles (See:
werkzeug/src/werkzeug/wrappers/json.py
Line 110 in 0bf2e3e
werkzeug/src/werkzeug/wrappers/json.py
Line 134 in 0bf2e3e
Consequently, should the data change, if you used cache = True, you'll just return the currently cached value, but if you use cache = False, you wont set the cache to the new value because of line:
werkzeug/src/werkzeug/wrappers/json.py
Line 134 in 0bf2e3e
Therefore, I recommend separating out the
use cache
andset cache
functionalities (i.e. two different kwargs), or, havecache=True
solely be used for obtaining the value from the cache, not for setting it. I.e. it is performingget_json(cache=False)
that perform the set (I.e. removeif cache
check here:werkzeug/src/werkzeug/wrappers/json.py
Line 134 in 0bf2e3e
The text was updated successfully, but these errors were encountered: