1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
|
"""JSON Patch, as per RFC 6902."""
from __future__ import annotations
import copy
import json
from abc import ABC
from abc import abstractmethod
from io import IOBase
from typing import Any
from typing import Dict
from typing import Iterable
from typing import List
from typing import Mapping
from typing import MutableMapping
from typing import MutableSequence
from typing import TypeVar
from typing import Union
from jsonpath._data import load_data
from jsonpath.exceptions import JSONPatchError
from jsonpath.exceptions import JSONPatchTestFailure
from jsonpath.exceptions import JSONPointerError
from jsonpath.exceptions import JSONPointerIndexError
from jsonpath.exceptions import JSONPointerKeyError
from jsonpath.exceptions import JSONPointerTypeError
from jsonpath.pointer import UNDEFINED
from jsonpath.pointer import JSONPointer
class Op(ABC):
"""One of the JSON Patch operations."""
name = "base"
@abstractmethod
def apply(
self, data: Union[MutableSequence[object], MutableMapping[str, object]]
) -> Union[MutableSequence[object], MutableMapping[str, object]]:
"""Apply this patch operation to _data_."""
@abstractmethod
def asdict(self) -> Dict[str, object]:
"""Return a dictionary representation of this operation."""
class OpAdd(Op):
"""The JSON Patch _add_ operation."""
__slots__ = ("path", "value")
name = "add"
def __init__(self, path: JSONPointer, value: object) -> None:
self.path = path
self.value = value
def apply(
self, data: Union[MutableSequence[object], MutableMapping[str, object]]
) -> Union[MutableSequence[object], MutableMapping[str, object]]:
"""Apply this patch operation to _data_."""
parent, obj = self.path.resolve_parent(data)
if parent is None:
# Replace the root object.
# The following op, if any, will raise a JSONPatchError if needed.
return self.value # type: ignore
target = self.path.parts[-1]
if isinstance(parent, MutableSequence):
if obj is UNDEFINED:
if target == "-":
parent.append(self.value)
else:
index = self.path._index(target) # noqa: SLF001
if index == len(parent):
parent.append(self.value)
else:
raise JSONPatchError("index out of range")
else:
parent.insert(int(target), self.value)
elif isinstance(parent, MutableMapping):
parent[str(target)] = self.value
else:
raise JSONPatchError(
f"unexpected operation on {parent.__class__.__name__!r}"
)
return data
def asdict(self) -> Dict[str, object]:
"""Return a dictionary representation of this operation."""
return {"op": self.name, "path": str(self.path), "value": self.value}
class OpAddNe(OpAdd):
"""A non-standard _add if not exists_ operation.
This is like _OpAdd_, but only adds object/dict keys/values if they key does
not already exist.
**New in version 1.2.0**
"""
__slots__ = ("path", "value")
name = "addne"
def apply(
self, data: Union[MutableSequence[object], MutableMapping[str, object]]
) -> Union[MutableSequence[object], MutableMapping[str, object]]:
"""Apply this patch operation to _data_."""
parent, obj = self.path.resolve_parent(data)
if parent is None:
# Replace the root object.
# The following op, if any, will raise a JSONPatchError if needed.
return self.value # type: ignore
target = self.path.parts[-1]
if isinstance(parent, MutableSequence):
if obj is UNDEFINED:
parent.append(self.value)
else:
parent.insert(int(target), self.value)
elif isinstance(parent, MutableMapping) and target not in parent:
parent[target] = self.value
return data
class OpAddAp(OpAdd):
"""A non-standard add operation that appends to arrays/lists .
This is like _OpAdd_, but assumes an index of "-" if the path can not
be resolved rather than raising a JSONPatchError.
**New in version 1.2.0**
"""
__slots__ = ("path", "value")
name = "addap"
def apply(
self, data: Union[MutableSequence[object], MutableMapping[str, object]]
) -> Union[MutableSequence[object], MutableMapping[str, object]]:
"""Apply this patch operation to _data_."""
parent, obj = self.path.resolve_parent(data)
if parent is None:
# Replace the root object.
# The following op, if any, will raise a JSONPatchError if needed.
return self.value # type: ignore
target = self.path.parts[-1]
if isinstance(parent, MutableSequence):
if obj is UNDEFINED:
parent.append(self.value)
else:
parent.insert(int(target), self.value)
elif isinstance(parent, MutableMapping):
parent[target] = self.value
else:
raise JSONPatchError(
f"unexpected operation on {parent.__class__.__name__!r}"
)
return data
class OpRemove(Op):
"""The JSON Patch _remove_ operation."""
__slots__ = ("path",)
name = "remove"
def __init__(self, path: JSONPointer) -> None:
self.path = path
def apply(
self, data: Union[MutableSequence[object], MutableMapping[str, object]]
) -> Union[MutableSequence[object], MutableMapping[str, object]]:
"""Apply this patch operation to _data_."""
parent, obj = self.path.resolve_parent(data)
if parent is None:
raise JSONPatchError("can't remove root")
if isinstance(parent, MutableSequence):
if obj is UNDEFINED:
raise JSONPatchError("can't remove nonexistent item")
del parent[int(self.path.parts[-1])]
elif isinstance(parent, MutableMapping):
if obj is UNDEFINED:
raise JSONPatchError("can't remove nonexistent property")
del parent[str(self.path.parts[-1])]
else:
raise JSONPatchError(
f"unexpected operation on {parent.__class__.__name__!r}"
)
return data
def asdict(self) -> Dict[str, object]:
"""Return a dictionary representation of this operation."""
return {"op": self.name, "path": str(self.path)}
class OpReplace(Op):
"""The JSON Patch _replace_ operation."""
__slots__ = ("path", "value")
name = "replace"
def __init__(self, path: JSONPointer, value: object) -> None:
self.path = path
self.value = value
def apply(
self, data: Union[MutableSequence[object], MutableMapping[str, object]]
) -> Union[MutableSequence[object], MutableMapping[str, object]]:
"""Apply this patch operation to _data_."""
parent, obj = self.path.resolve_parent(data)
if parent is None:
return self.value # type: ignore
if isinstance(parent, MutableSequence):
if obj is UNDEFINED:
raise JSONPatchError("can't replace nonexistent item")
parent[int(self.path.parts[-1])] = self.value
elif isinstance(parent, MutableMapping):
if obj is UNDEFINED:
raise JSONPatchError("can't replace nonexistent property")
parent[str(self.path.parts[-1])] = self.value
else:
raise JSONPatchError(
f"unexpected operation on {parent.__class__.__name__!r}"
)
return data
def asdict(self) -> Dict[str, object]:
"""Return a dictionary representation of this operation."""
return {"op": self.name, "path": str(self.path), "value": self.value}
class OpMove(Op):
"""The JSON Patch _move_ operation."""
__slots__ = ("source", "dest")
name = "move"
def __init__(self, from_: JSONPointer, path: JSONPointer) -> None:
self.source = from_
self.dest = path
def apply(
self, data: Union[MutableSequence[object], MutableMapping[str, object]]
) -> Union[MutableSequence[object], MutableMapping[str, object]]:
"""Apply this patch operation to _data_."""
if self.dest.is_relative_to(self.source):
raise JSONPatchError("can't move object to one of its own children")
source_parent, source_obj = self.source.resolve_parent(data)
if source_obj is UNDEFINED:
raise JSONPatchError("source object does not exist")
if isinstance(source_parent, MutableSequence):
del source_parent[int(self.source.parts[-1])]
if isinstance(source_parent, MutableMapping):
del source_parent[str(self.source.parts[-1])]
dest_parent, _ = self.dest.resolve_parent(data)
if dest_parent is None:
# Move source to root
return source_obj # type: ignore
if isinstance(dest_parent, MutableSequence):
dest_parent.insert(int(self.dest.parts[-1]), source_obj)
elif isinstance(dest_parent, MutableMapping):
dest_parent[str(self.dest.parts[-1])] = source_obj
else:
raise JSONPatchError(
f"unexpected operation on {dest_parent.__class__.__name__!r}"
)
return data
def asdict(self) -> Dict[str, object]:
"""Return a dictionary representation of this operation."""
return {"op": self.name, "from": str(self.source), "path": str(self.dest)}
class OpCopy(Op):
"""The JSON Patch _copy_ operation."""
__slots__ = ("source", "dest")
name = "copy"
def __init__(self, from_: JSONPointer, path: JSONPointer) -> None:
self.source = from_
self.dest = path
def apply(
self, data: Union[MutableSequence[object], MutableMapping[str, object]]
) -> Union[MutableSequence[object], MutableMapping[str, object]]:
"""Apply this patch operation to _data_."""
source_parent, source_obj = self.source.resolve_parent(data)
if source_obj is UNDEFINED:
raise JSONPatchError("source object does not exist")
dest_parent, dest_obj = self.dest.resolve_parent(data)
if dest_parent is None:
# Copy source to root
return copy.deepcopy(source_obj) # type: ignore
if isinstance(dest_parent, MutableSequence):
dest_parent.insert(int(self.dest.parts[-1]), copy.deepcopy(source_obj))
elif isinstance(dest_parent, MutableMapping):
dest_parent[str(self.dest.parts[-1])] = copy.deepcopy(source_obj)
else:
raise JSONPatchError(
f"unexpected operation on {dest_parent.__class__.__name__!r}"
)
return data
def asdict(self) -> Dict[str, object]:
"""Return a dictionary representation of this operation."""
return {"op": self.name, "from": str(self.source), "path": str(self.dest)}
class OpTest(Op):
"""The JSON Patch _test_ operation."""
__slots__ = ("path", "value")
name = "test"
def __init__(self, path: JSONPointer, value: object) -> None:
self.path = path
self.value = value
def apply(
self, data: Union[MutableSequence[object], MutableMapping[str, object]]
) -> Union[MutableSequence[object], MutableMapping[str, object]]:
"""Apply this patch operation to _data_."""
_, obj = self.path.resolve_parent(data)
if not obj == self.value:
raise JSONPatchTestFailure
return data
def asdict(self) -> Dict[str, object]:
"""Return a dictionary representation of this operation."""
return {"op": self.name, "path": str(self.path), "value": self.value}
Self = TypeVar("Self", bound="JSONPatch")
class JSONPatch:
"""Modify JSON-like data with JSON Patch.
RFC 6902 defines operations to manipulate a JSON document. `JSONPatch`
supports parsing and applying standard JSON Patch formatted operations,
and provides a Python builder API following the same semantics as RFC 6902.
Arguments:
ops: A JSON Patch formatted document or equivalent Python objects.
unicode_escape: If `True`, UTF-16 escape sequences will be decoded
before parsing JSON pointers.
uri_decode: If `True`, JSON pointers will be unescaped using _urllib_
before being parsed.
Raises:
JSONPatchError: If _ops_ is given and any of the provided operations
is malformed.
"""
def __init__(
self,
ops: Union[str, IOBase, Iterable[Mapping[str, object]], None] = None,
*,
unicode_escape: bool = True,
uri_decode: bool = False,
) -> None:
self.ops: List[Op] = []
self.unicode_escape = unicode_escape
self.uri_decode = uri_decode
if ops:
self._load(ops)
def _load(self, patch: Union[str, IOBase, Iterable[Mapping[str, object]]]) -> None:
if isinstance(patch, IOBase):
_patch = json.loads(patch.read())
elif isinstance(patch, str):
_patch = json.loads(patch)
else:
_patch = patch
try:
self._build(_patch)
except TypeError as err:
raise JSONPatchError(
"expected a sequence of patch operations, "
f"found {_patch.__class__.__name__!r}"
) from err
def _build(self, patch: Iterable[Mapping[str, object]]) -> None:
for i, operation in enumerate(patch):
try:
op = operation["op"]
except KeyError as err:
raise JSONPatchError(f"missing 'op' member at op {i}") from err
if op == "add":
self.add(
path=self._op_pointer(operation, "path", "add", i),
value=self._op_value(operation, "value", "add", i),
)
elif op == "addne":
self.addne(
path=self._op_pointer(operation, "path", "addne", i),
value=self._op_value(operation, "value", "addne", i),
)
elif op == "addap":
self.addap(
path=self._op_pointer(operation, "path", "addap", i),
value=self._op_value(operation, "value", "addap", i),
)
elif op == "remove":
self.remove(path=self._op_pointer(operation, "path", "add", i))
elif op == "replace":
self.replace(
path=self._op_pointer(operation, "path", "replace", i),
value=self._op_value(operation, "value", "replace", i),
)
elif op == "move":
self.move(
from_=self._op_pointer(operation, "from", "move", i),
path=self._op_pointer(operation, "path", "move", i),
)
elif op == "copy":
self.copy(
from_=self._op_pointer(operation, "from", "copy", i),
path=self._op_pointer(operation, "path", "copy", i),
)
elif op == "test":
self.test(
path=self._op_pointer(operation, "path", "test", i),
value=self._op_value(operation, "value", "test", i),
)
else:
raise JSONPatchError(
"expected 'op' to be one of 'add', 'remove', 'replace', "
f"'move', 'copy' or 'test' ({op}:{i})"
)
def _op_pointer(
self, operation: Mapping[str, object], key: str, op: str, i: int
) -> JSONPointer:
try:
pointer = operation[key]
except KeyError as err:
raise JSONPatchError(f"missing property {key!r} ({op}:{i})") from err
if not isinstance(pointer, str):
raise JSONPatchError(
f"expected a JSON Pointer string for {key!r}, "
f"found {pointer.__class__.__name__!r} "
f"({op}:{i})"
)
try:
return JSONPointer(
pointer, unicode_escape=self.unicode_escape, uri_decode=self.uri_decode
)
except JSONPointerError as err:
raise JSONPatchError(f"{err} ({op}:{i})") from err
def _op_value(
self, operation: Mapping[str, object], key: str, op: str, i: int
) -> object:
try:
return operation[key]
except KeyError as err:
raise JSONPatchError(f"missing property {key!r} ({op}:{i})") from err
def _ensure_pointer(self, path: Union[str, JSONPointer]) -> JSONPointer:
if isinstance(path, str):
return JSONPointer(
path,
unicode_escape=self.unicode_escape,
uri_decode=self.uri_decode,
)
assert isinstance(path, JSONPointer)
return path
def add(self: Self, path: Union[str, JSONPointer], value: object) -> Self:
"""Append an _add_ operation to this patch.
Arguments:
path: A string representation of a JSON Pointer, or one that has
already been parsed.
value: The object to add.
Returns:
This `JSONPatch` instance, so we can build a JSON Patch by chaining
calls to JSON Patch operation methods.
"""
pointer = self._ensure_pointer(path)
self.ops.append(OpAdd(path=pointer, value=value))
return self
def addne(self: Self, path: Union[str, JSONPointer], value: object) -> Self:
"""Append an _addne_ operation to this patch.
Arguments:
path: A string representation of a JSON Pointer, or one that has
already been parsed.
value: The object to add.
Returns:
This `JSONPatch` instance, so we can build a JSON Patch by chaining
calls to JSON Patch operation methods.
"""
pointer = self._ensure_pointer(path)
self.ops.append(OpAddNe(path=pointer, value=value))
return self
def addap(self: Self, path: Union[str, JSONPointer], value: object) -> Self:
"""Append an _addap_ operation to this patch.
Arguments:
path: A string representation of a JSON Pointer, or one that has
already been parsed.
value: The object to add.
Returns:
This `JSONPatch` instance, so we can build a JSON Patch by chaining
calls to JSON Patch operation methods.
"""
pointer = self._ensure_pointer(path)
self.ops.append(OpAddAp(path=pointer, value=value))
return self
def remove(self: Self, path: Union[str, JSONPointer]) -> Self:
"""Append a _remove_ operation to this patch.
Arguments:
path: A string representation of a JSON Pointer, or one that has
already been parsed.
Returns:
This `JSONPatch` instance, so we can build a JSON Patch by chaining
calls to JSON Patch operation methods.
"""
pointer = self._ensure_pointer(path)
self.ops.append(OpRemove(path=pointer))
return self
def replace(self: Self, path: Union[str, JSONPointer], value: object) -> Self:
"""Append a _replace_ operation to this patch.
Arguments:
path: A string representation of a JSON Pointer, or one that has
already been parsed.
value: The object to add.
Returns:
This `JSONPatch` instance, so we can build a JSON Patch by chaining
calls to JSON Patch operation methods.
"""
pointer = self._ensure_pointer(path)
self.ops.append(OpReplace(path=pointer, value=value))
return self
def move(
self: Self, from_: Union[str, JSONPointer], path: Union[str, JSONPointer]
) -> Self:
"""Append a _move_ operation to this patch.
Arguments:
from_: A string representation of a JSON Pointer, or one that has
already been parsed.
path: A string representation of a JSON Pointer, or one that has
already been parsed.
Returns:
This `JSONPatch` instance, so we can build a JSON Patch by chaining
calls to JSON Patch operation methods.
"""
source_pointer = self._ensure_pointer(from_)
dest_pointer = self._ensure_pointer(path)
self.ops.append(OpMove(from_=source_pointer, path=dest_pointer))
return self
def copy(
self: Self, from_: Union[str, JSONPointer], path: Union[str, JSONPointer]
) -> Self:
"""Append a _copy_ operation to this patch.
Arguments:
from_: A string representation of a JSON Pointer, or one that has
already been parsed.
path: A string representation of a JSON Pointer, or one that has
already been parsed.
Returns:
This `JSONPatch` instance, so we can build a JSON Patch by chaining
calls to JSON Patch operation methods.
"""
source_pointer = self._ensure_pointer(from_)
dest_pointer = self._ensure_pointer(path)
self.ops.append(OpCopy(from_=source_pointer, path=dest_pointer))
return self
def test(self: Self, path: Union[str, JSONPointer], value: object) -> Self:
"""Append a test operation to this patch.
Arguments:
path: A string representation of a JSON Pointer, or one that has
already been parsed.
value: The object to test.
Returns:
This `JSONPatch` instance, so we can build a JSON Patch by chaining
calls to JSON Patch operation methods.
"""
pointer = self._ensure_pointer(path)
self.ops.append(OpTest(path=pointer, value=value))
return self
def apply(
self,
data: Union[str, IOBase, MutableSequence[Any], MutableMapping[str, Any]],
) -> object:
"""Apply all operations from this patch to _data_.
If _data_ is a string or file-like object, it will be loaded with
_json.loads_. Otherwise _data_ should be a JSON-like data structure and
will be modified in place.
When modifying _data_ in place, we return modified data too. This is
to allow for replacing _data's_ root element, which is allowed by some
patch operations.
Arguments:
data: The target JSON "document" or equivalent Python objects.
Returns:
Modified input data.
Raises:
JSONPatchError: When a patch operation fails.
JSONPatchTestFailure: When a _test_ operation does not pass.
`JSONPatchTestFailure` is a subclass of `JSONPatchError`.
"""
_data = load_data(data)
for i, op in enumerate(self.ops):
try:
_data = op.apply(_data)
except JSONPatchTestFailure as err:
raise JSONPatchTestFailure(f"test failed ({op.name}:{i})") from err
except JSONPointerKeyError as err:
raise JSONPatchError(f"{err} ({op.name}:{i})") from err
except JSONPointerIndexError as err:
raise JSONPatchError(f"{err} ({op.name}:{i})") from err
except JSONPointerTypeError as err:
raise JSONPatchError(f"{err} ({op.name}:{i})") from err
except (JSONPointerError, JSONPatchError) as err:
raise JSONPatchError(f"{err} ({op.name}:{i})") from err
return _data
def asdicts(self) -> List[Dict[str, object]]:
"""Return a list of this patch's operations as dictionaries."""
return [op.asdict() for op in self.ops]
def apply(
patch: Union[str, IOBase, Iterable[Mapping[str, object]], None],
data: Union[str, IOBase, MutableSequence[Any], MutableMapping[str, Any]],
*,
unicode_escape: bool = True,
uri_decode: bool = False,
) -> object:
"""Apply the JSON Patch _patch_ to _data_.
If _data_ is a string or file-like object, it will be loaded with
_json.loads_. Otherwise _data_ should be a JSON-like data structure and
will be **modified in-place**.
When modifying _data_ in-place, we return modified data too. This is
to allow for replacing _data's_ root element, which is allowed by some
patch operations.
Arguments:
patch: A JSON Patch formatted document or equivalent Python objects.
data: The target JSON "document" or equivalent Python objects.
unicode_escape: If `True`, UTF-16 escape sequences will be decoded
before parsing JSON pointers.
uri_decode: If `True`, JSON pointers will be unescaped using _urllib_
before being parsed.
Returns:
Modified input data.
Raises:
JSONPatchError: When a patch operation fails.
JSONPatchTestFailure: When a _test_ operation does not pass.
`JSONPatchTestFailure` is a subclass of `JSONPatchError`.
"""
return JSONPatch(
patch,
unicode_escape=unicode_escape,
uri_decode=uri_decode,
).apply(data)
|