22using System . Collections . Generic ;
33using System . Linq ;
44using System . Text ;
5+ using System . Text . Encodings . Web ;
56using System . Text . Json ;
67using System . Text . Json . Nodes ;
78using Json . More ;
@@ -53,10 +54,11 @@ public void Issue393_PatchDoesNothing()
5354 var arrayObject = JsonDocument . Parse ( arrayObjectJson ) . RootElement ;
5455
5556 // Way 1: patch whole array
56- var patchedArray = patchConfig . Apply ( arrayObject . AsNode ( ) ) . Result ; // <- does nothing
57+ var patchedArray = patchConfig . Apply ( arrayObject . AsNode ( ) ) . Result ; // <- does nothing
5758
5859 Console . WriteLine ( JsonSerializer . Serialize ( patchedArray ) ) ;
5960 }
61+
6062 [ Test ]
6163 public void Issue393_NodeAlreadyHasParent_2 ( )
6264 {
@@ -100,10 +102,11 @@ public void Issue393_NodeAlreadyHasParent_2()
100102 // Way 2: just patch every element
101103 foreach ( var element in jsonArray )
102104 {
103- var patchedNode = patchConfig . Apply ( element ) . Result ; // <- throws an error
105+ var patchedNode = patchConfig . Apply ( element ) . Result ; // <- throws an error
104106 Console . WriteLine ( JsonSerializer . Serialize ( patchedNode ) ) ;
105107 }
106108 }
109+
107110 [ Test ]
108111 public void Issue393_NodeAlreadyHasParent_3 ( )
109112 {
@@ -150,8 +153,31 @@ public void Issue393_NodeAlreadyHasParent_3()
150153 var nodeToPatch = jsonArray [ currentIndex ] ;
151154 jsonArray . RemoveAt ( currentIndex ) ;
152155
153- var patchedNode = patchConfig . Apply ( nodeToPatch ) . Result ; // <- throws an error
156+ var patchedNode = patchConfig . Apply ( nodeToPatch ) . Result ; // <- throws an error
154157 Console . WriteLine ( JsonSerializer . Serialize ( patchedNode ) ) ;
155158 }
156159 }
160+
161+ [ Test ]
162+ public void Issue397_ReplaceShouldThrowForMissingValue ( )
163+ {
164+ const string mask = "*****" ;
165+ var maskJson = JsonValue . Create ( mask ) ;
166+
167+ var pathsToPatch = new [ ] { "/first_name" , "/last_name" } ;
168+
169+ var patchOperations = pathsToPatch . Select ( path => PatchOperation . Replace ( JsonPointer . Parse ( path ) , maskJson ) ) ;
170+ var patchConfig = new JsonPatch ( patchOperations ) ;
171+
172+ const string singleObjectJson = @"{
173+ ""id"":""640729d45434f90313d25c78"",
174+ ""guid"":""f2e2767c-03e0-4862-addc-7d46c55efb33"",
175+ ""city"":""Boston""
176+ }" ;
177+
178+ var singleObject = JsonNode . Parse ( singleObjectJson ) ;
179+ var result = patchConfig . Apply ( singleObject ) ;
180+ Console . WriteLine ( JsonSerializer . Serialize ( result , new JsonSerializerOptions { WriteIndented = true , Encoder = JavaScriptEncoder . UnsafeRelaxedJsonEscaping } ) ) ;
181+ Assert . IsNotNull ( patchConfig . Apply ( singleObject ) . Error ) ;
182+ }
157183}
0 commit comments