@@ -60,6 +60,10 @@ class JsonPatchException(Exception):
60
60
"""Base Json Patch exception"""
61
61
62
62
63
+ class InvalidJsonPatch (JsonPatchException ):
64
+ """ Raised if an invalid JSON Patch is created """
65
+
66
+
63
67
class JsonPatchConflict (JsonPatchException ):
64
68
"""Raised if patch could not be applied due to conflict situation such as:
65
69
- attempt to add object key then it already exists;
@@ -341,15 +345,15 @@ def apply(self, obj, in_place=False):
341
345
342
346
def _get_operation (self , operation ):
343
347
if 'op' not in operation :
344
- raise JsonPatchException ("Operation does not contain 'op' member" )
348
+ raise InvalidJsonPatch ("Operation does not contain 'op' member" )
345
349
346
350
op = operation ['op' ]
347
351
348
352
if not isinstance (op , basestring ):
349
- raise JsonPatchException ("Operation must be a string" )
353
+ raise InvalidJsonPatch ("Operation must be a string" )
350
354
351
355
if op not in self .operations :
352
- raise JsonPatchException ("Unknown operation {0!r}" .format (op ))
356
+ raise InvalidJsonPatch ("Unknown operation {0!r}" .format (op ))
353
357
354
358
cls = self .operations [op ]
355
359
return cls (operation )
@@ -397,7 +401,12 @@ class AddOperation(PatchOperation):
397
401
"""Adds an object property or an array element."""
398
402
399
403
def apply (self , obj ):
400
- value = self .operation ["value" ]
404
+ try :
405
+ value = self .operation ["value" ]
406
+ except KeyError as ex :
407
+ raise InvalidJsonPatch (
408
+ "The operation does not contain a 'value' member" )
409
+
401
410
subobj , part = self .pointer .to_last (obj )
402
411
403
412
if isinstance (subobj , list ):
@@ -426,7 +435,12 @@ class ReplaceOperation(PatchOperation):
426
435
"""Replaces an object property or an array element by new value."""
427
436
428
437
def apply (self , obj ):
429
- value = self .operation ["value" ]
438
+ try :
439
+ value = self .operation ["value" ]
440
+ except KeyError as ex :
441
+ raise InvalidJsonPatch (
442
+ "The operation does not contain a 'value' member" )
443
+
430
444
subobj , part = self .pointer .to_last (obj )
431
445
432
446
if part is None :
@@ -451,15 +465,20 @@ class MoveOperation(PatchOperation):
451
465
"""Moves an object property or an array element to new location."""
452
466
453
467
def apply (self , obj ):
454
- from_ptr = JsonPointer (self .operation ['from' ])
468
+ try :
469
+ from_ptr = JsonPointer (self .operation ['from' ])
470
+ except KeyError as ex :
471
+ raise InvalidJsonPatch (
472
+ "The operation does not contain a 'from' member" )
473
+
455
474
subobj , part = from_ptr .to_last (obj )
456
475
try :
457
476
value = subobj [part ]
458
477
except (KeyError , IndexError ) as ex :
459
478
raise JsonPatchConflict (str (ex ))
460
479
461
480
if isinstance (subobj , dict ) and self .pointer .contains (from_ptr ):
462
- raise JsonPatchException ('Cannot move values into its own children' )
481
+ raise JsonPatchConflict ('Cannot move values into its own children' )
463
482
464
483
obj = RemoveOperation ({
465
484
'op' : 'remove' ,
@@ -488,12 +507,16 @@ def apply(self, obj):
488
507
except JsonPointerException as ex :
489
508
raise JsonPatchTestFailed (str (ex ))
490
509
491
- if 'value' in self . operation :
510
+ try :
492
511
value = self .operation ['value' ]
493
- if val != value :
494
- msg = '{0} ({1}) is not equal to tested value {2} ({3})'
495
- raise JsonPatchTestFailed (msg .format (val , type (val ),
496
- value , type (value )))
512
+ except KeyError as ex :
513
+ raise InvalidJsonPatch (
514
+ "The operation does not contain a 'value' member" )
515
+
516
+ if val != value :
517
+ msg = '{0} ({1}) is not equal to tested value {2} ({3})'
518
+ raise JsonPatchTestFailed (msg .format (val , type (val ),
519
+ value , type (value )))
497
520
498
521
return obj
499
522
@@ -502,7 +525,12 @@ class CopyOperation(PatchOperation):
502
525
""" Copies an object property or an array element to a new location """
503
526
504
527
def apply (self , obj ):
505
- from_ptr = JsonPointer (self .operation ['from' ])
528
+ try :
529
+ from_ptr = JsonPointer (self .operation ['from' ])
530
+ except KeyError as ex :
531
+ raise InvalidJsonPatch (
532
+ "The operation does not contain a 'from' member" )
533
+
506
534
subobj , part = from_ptr .to_last (obj )
507
535
try :
508
536
value = copy .deepcopy (subobj [part ])
0 commit comments