-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Fix JSON.set_path key derivation to strip only the file extension #4251
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
Open
SWAPI03
wants to merge
1
commit into
redis:master
Choose a base branch
from
SWAPI03:fix/json-setpath-extension-key
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1620,10 +1620,15 @@ def test_set_file(client): | |
| @pytest.mark.redismod | ||
| def test_set_path(client): | ||
| import json | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same note for the imports. |
||
| import os | ||
| import tempfile | ||
|
|
||
| root = tempfile.mkdtemp() | ||
| sub = tempfile.mkdtemp(dir=root) | ||
| # Place the file under a directory whose name contains a dot, to guard | ||
| # against the derived key being truncated at the first dot rather than at | ||
| # the file extension. | ||
| sub = os.path.join(root, "v1.2") | ||
| os.makedirs(sub) | ||
| jsonfile = tempfile.mkstemp(suffix=".json", dir=sub)[1] | ||
| nojsonfile = tempfile.mkstemp(dir=root)[1] | ||
|
|
||
|
|
@@ -1635,4 +1640,36 @@ def test_set_path(client): | |
| result = {jsonfile: True, nojsonfile: False} | ||
| assert client.json().set_path(Path.root_path(), root) == result | ||
| res = {"hello": "world"} | ||
| assert client.json().get(jsonfile.rsplit(".")[0]) == res | ||
| assert client.json().get(jsonfile.rsplit(".", 1)[0]) == res | ||
|
|
||
|
|
||
| def test_set_path_key_strips_only_extension(monkeypatch): | ||
| # Regression test: JSON.set_path must strip only the file extension when | ||
| # deriving the key, so a path containing dots in a directory (e.g. a | ||
| # versioned "v1.2" folder) is not truncated at the first dot. set_file is | ||
| # stubbed so this runs without a server. | ||
| import json | ||
| import os | ||
| import tempfile | ||
|
|
||
| root = tempfile.mkdtemp() | ||
| dotted_dir = os.path.join(root, "v1.2") | ||
| os.makedirs(dotted_dir) | ||
| json_file = os.path.join(dotted_dir, "data.json") | ||
| with open(json_file, "w") as fp: | ||
| fp.write(json.dumps({"hello": "world"})) | ||
|
|
||
| json_client = redis.Redis().json() | ||
| captured = [] | ||
|
|
||
| def fake_set_file(name, *args, **kwargs): | ||
| captured.append(name) | ||
| return True | ||
|
|
||
| monkeypatch.setattr(json_client, "set_file", fake_set_file) | ||
|
|
||
| result = json_client.set_path(Path.root_path(), root) | ||
|
|
||
| expected_key = json_file[: -len(".json")] | ||
| assert captured == [expected_key] | ||
| assert result == {json_file: True} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move those imports to the top level in the import section.