Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions jsonpatch_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def patch_files():
""" Applies a JSON patch on a JSON file """
args = jsonpatch_parser.parse_args()
doc = json.load(args.ORIGINAL)
patch = json.load(args.PATCH)
result = jsonpatch.apply_patch(doc, patch)
result = jsonpatch.apply_patch(doc, args.PATCH.read())

if args.in_place:
# 2026-10-11: Release the input handle before replacing it on Windows.
Expand Down
21 changes: 21 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2082,6 +2082,27 @@ def test_jsonpatch_patch_from_stdin(self):
self.assertEqual(res.returncode, 0, res.stderr)
self.assertEqual(json.loads(res.stdout), {'a': 2})

def test_jsonpatch_rejects_invalid_patch_text_before_in_place_write(self):
remove = '[{"op":"remove","path":"/keep"}]'
for patch in ('[{"op":"test","op":"remove","path":"/keep","value":13}]',
json.dumps(remove)):
for use_stdin in (False, True):
with self.subTest(patch=patch, stdin=use_stdin):
doc = self.write('doc.json', {'keep': 7})
with open(doc, 'rb') as f:
original = f.read()
if use_stdin:
res = self.run_script('jsonpatch', '-i', doc, stdin=patch)
else:
path = os.path.join(self.dir, 'patch.json')
with open(path, 'w', encoding='utf-8') as f:
f.write(patch)
res = self.run_script('jsonpatch', '-i', doc, path)
self.assertNotEqual(res.returncode, 0)
self.assertIn('InvalidJsonPatch', res.stderr)
with open(doc, 'rb') as f:
self.assertEqual(f.read(), original)

def test_jsonpatch_utf8_files_without_utf8_mode(self):
doc = self.write('doc.json', {'a': 'ä'}, ensure_ascii=False)
patch = self.write('patch.json', [
Expand Down
Loading