1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Text ;
5+ using System . Text . Json ;
6+ using System . Text . Json . Nodes ;
7+ using Json . More ;
8+ using Json . Pointer ;
9+ using NUnit . Framework ;
10+
11+ namespace Json . Patch . Tests ;
12+
13+ public class GithubTests
14+ {
15+ [ Test ]
16+ public void Issue393_PatchDoesNothing ( )
17+ {
18+ const string mask = "*****" ;
19+ var maskJson = JsonValue . Create ( mask ) ;
20+
21+ var pathsToPatch = new [ ] { "/first_name" , "/last_name" } ;
22+
23+ var patchOperations = pathsToPatch . Select ( path => PatchOperation . Replace ( JsonPointer . Parse ( path ) , maskJson ) ) ;
24+ var patchConfig = new JsonPatch ( patchOperations ) ;
25+
26+ Console . WriteLine ( JsonSerializer . Serialize ( patchConfig ) ) ;
27+
28+ const string singleObjectJson = "{" +
29+ "\" _id\" :\" 640729d45434f90313d25c78\" ," +
30+ "\" guid\" :\" f2e2767c-03e0-4862-addc-7d46c55efb33\" ," +
31+ "\" first_name\" :\" Kathrine\" ," +
32+ "\" last_name\" :\" Pate\" " +
33+ "}" ;
34+
35+ var singleObject = JsonDocument . Parse ( singleObjectJson ) . RootElement ;
36+ var patchedSingleObject = patchConfig . Apply ( singleObject . AsNode ( ) ) . Result ;
37+ Console . WriteLine ( JsonSerializer . Serialize ( patchedSingleObject ) ) ;
38+
39+ const string arrayObjectJson = "[" +
40+ "{" +
41+ "\" _id\" :\" 640729d45434f90313d25c78\" ," +
42+ "\" guid\" :\" f2e2767c-03e0-4862-addc-7d46c55efb33\" ," +
43+ "\" first_name\" :\" Kathrine\" ," +
44+ "\" last_name\" :\" Pate\" " +
45+ "}," +
46+ "{\" _id\" :\" 640729d45b5824ffcabc30a5\" ," +
47+ "\" guid\" :\" 73193eda-074b-4f31-9f09-507a008ccb75\" ," +
48+ "\" first_name\" :\" Rivers\" ," +
49+ "\" last_name\" :\" Smith\" " +
50+ "}" +
51+ "]" ;
52+
53+ var arrayObject = JsonDocument . Parse ( arrayObjectJson ) . RootElement ;
54+
55+ // Way 1: patch whole array
56+ var patchedArray = patchConfig . Apply ( arrayObject . AsNode ( ) ) . Result ; // <- does nothing
57+
58+ Console . WriteLine ( JsonSerializer . Serialize ( patchedArray ) ) ;
59+ }
60+ [ Test ]
61+ public void Issue393_NodeAlreadyHasParent_2 ( )
62+ {
63+ const string mask = "*****" ;
64+ var maskJson = JsonValue . Create ( mask ) ;
65+
66+ var pathsToPatch = new [ ] { "/first_name" , "/last_name" } ;
67+
68+ var patchOperations = pathsToPatch . Select ( path => PatchOperation . Replace ( JsonPointer . Parse ( path ) , maskJson ) ) ;
69+ var patchConfig = new JsonPatch ( patchOperations ) ;
70+
71+ const string singleObjectJson = "{" +
72+ "\" _id\" :\" 640729d45434f90313d25c78\" ," +
73+ "\" guid\" :\" f2e2767c-03e0-4862-addc-7d46c55efb33\" ," +
74+ "\" first_name\" :\" Kathrine\" ," +
75+ "\" last_name\" :\" Pate\" " +
76+ "}" ;
77+
78+ var singleObject = JsonDocument . Parse ( singleObjectJson ) . RootElement ;
79+ var patchedSingleObject = patchConfig . Apply ( singleObject . AsNode ( ) ) . Result ;
80+ Console . WriteLine ( JsonSerializer . Serialize ( patchedSingleObject ) ) ;
81+
82+ const string arrayObjectJson = "[" +
83+ "{" +
84+ "\" _id\" :\" 640729d45434f90313d25c78\" ," +
85+ "\" guid\" :\" f2e2767c-03e0-4862-addc-7d46c55efb33\" ," +
86+ "\" first_name\" :\" Kathrine\" ," +
87+ "\" last_name\" :\" Pate\" " +
88+ "}," +
89+ "{\" _id\" :\" 640729d45b5824ffcabc30a5\" ," +
90+ "\" guid\" :\" 73193eda-074b-4f31-9f09-507a008ccb75\" ," +
91+ "\" first_name\" :\" Rivers\" ," +
92+ "\" last_name\" :\" Smith\" " +
93+ "}" +
94+ "]" ;
95+
96+ var arrayObject = JsonDocument . Parse ( arrayObjectJson ) . RootElement ;
97+
98+ var jsonArray = arrayObject . AsNode ( ) . AsArray ( ) ;
99+
100+ // Way 2: just patch every element
101+ foreach ( var element in jsonArray )
102+ {
103+ var patchedNode = patchConfig . Apply ( element ) . Result ; // <- throws an error
104+ Console . WriteLine ( JsonSerializer . Serialize ( patchedNode ) ) ;
105+ }
106+ }
107+ [ Test ]
108+ public void Issue393_NodeAlreadyHasParent_3 ( )
109+ {
110+ const string mask = "*****" ;
111+ var maskJson = JsonValue . Create ( mask ) ;
112+
113+ var pathsToPatch = new [ ] { "/first_name" , "/last_name" } ;
114+
115+ var patchOperations = pathsToPatch . Select ( path => PatchOperation . Replace ( JsonPointer . Parse ( path ) , maskJson ) ) ;
116+ var patchConfig = new JsonPatch ( patchOperations ) ;
117+
118+ const string singleObjectJson = "{" +
119+ "\" _id\" :\" 640729d45434f90313d25c78\" ," +
120+ "\" guid\" :\" f2e2767c-03e0-4862-addc-7d46c55efb33\" ," +
121+ "\" first_name\" :\" Kathrine\" ," +
122+ "\" last_name\" :\" Pate\" " +
123+ "}" ;
124+
125+ var singleObject = JsonNode . Parse ( singleObjectJson ) ;
126+ var patchedSingleObject = patchConfig . Apply ( singleObject ) . Result ;
127+ Console . WriteLine ( JsonSerializer . Serialize ( patchedSingleObject ) ) ;
128+
129+ const string arrayObjectJson = "[" +
130+ "{" +
131+ "\" _id\" :\" 640729d45434f90313d25c78\" ," +
132+ "\" guid\" :\" f2e2767c-03e0-4862-addc-7d46c55efb33\" ," +
133+ "\" first_name\" :\" Kathrine\" ," +
134+ "\" last_name\" :\" Pate\" " +
135+ "}," +
136+ "{\" _id\" :\" 640729d45b5824ffcabc30a5\" ," +
137+ "\" guid\" :\" 73193eda-074b-4f31-9f09-507a008ccb75\" ," +
138+ "\" first_name\" :\" Rivers\" ," +
139+ "\" last_name\" :\" Smith\" " +
140+ "}" +
141+ "]" ;
142+
143+ var arrayObject = JsonNode . Parse ( arrayObjectJson ) ;
144+
145+ var jsonArray = arrayObject . AsArray ( ) ;
146+
147+ // Way 3: remove from initial array and then patch
148+ for ( int currentIndex = jsonArray . Count - 1 ; currentIndex >= 0 ; currentIndex -- )
149+ {
150+ var nodeToPatch = jsonArray [ currentIndex ] ;
151+ jsonArray . RemoveAt ( currentIndex ) ;
152+
153+ var patchedNode = patchConfig . Apply ( nodeToPatch ) . Result ; // <- throws an error
154+ Console . WriteLine ( JsonSerializer . Serialize ( patchedNode ) ) ;
155+ }
156+ }
157+ }
0 commit comments