34
34
import com .gravity9 .jsonpatch .JsonPatchMessages ;
35
35
import com .gravity9 .jsonpatch .JsonPatchOperation ;
36
36
import com .gravity9 .jsonpatch .RemoveOperation ;
37
+ import com .jayway .jsonpath .PathNotFoundException ;
37
38
38
39
import javax .annotation .ParametersAreNonnullByDefault ;
39
40
import java .io .IOException ;
@@ -110,10 +111,11 @@ public static JsonPatch asJsonPatch(final JsonNode source,
110
111
* @param target the expected result after applying the patch
111
112
* @param fieldsToIgnore list of JsonPath or JsonPointer paths which should be ignored when generating diff. Non-existing fields are ignored.
112
113
* @return the patch as a {@link JsonPatch}
113
- * @since 1.9
114
+ * @throws JsonPatchException if fieldsToIgnored not in valid JsonPath or JsonPointer format
115
+ * @since 2.0.0
114
116
*/
115
117
public static JsonPatch asJsonPatchIgnoringFields (final JsonNode source ,
116
- final JsonNode target , final List <String > fieldsToIgnore ) {
118
+ final JsonNode target , final List <String > fieldsToIgnore ) throws JsonPatchException {
117
119
BUNDLE .checkNotNull (source , "common.nullArgument" );
118
120
BUNDLE .checkNotNull (target , "common.nullArgument" );
119
121
final List <JsonPatchOperation > ignoredFieldsRemoveOperations = getJsonPatchRemoveOperationsForIgnoredFields (fieldsToIgnore );
@@ -129,23 +131,26 @@ public static JsonPatch asJsonPatchIgnoringFields(final JsonNode source,
129
131
return processor .getPatch ();
130
132
}
131
133
132
- private static JsonNode removeIgnoredFields (JsonNode node , List <JsonPatchOperation > ignoredFieldsRemoveOperations ) {
134
+ private static JsonNode removeIgnoredFields (JsonNode node , List <JsonPatchOperation > ignoredFieldsRemoveOperations ) throws JsonPatchException {
133
135
JsonNode nodeWithoutIgnoredFields = node ;
134
136
for (JsonPatchOperation operation : ignoredFieldsRemoveOperations ) {
135
137
nodeWithoutIgnoredFields = removeIgnoredFieldOrIgnore (nodeWithoutIgnoredFields , operation );
136
138
}
137
139
return nodeWithoutIgnoredFields ;
138
140
}
139
141
140
- private static JsonNode removeIgnoredFieldOrIgnore (JsonNode nodeWithoutIgnoredFields , JsonPatchOperation operation ) {
142
+ private static JsonNode removeIgnoredFieldOrIgnore (JsonNode nodeWithoutIgnoredFields , JsonPatchOperation operation ) throws JsonPatchException {
141
143
try {
142
144
List <JsonPatchOperation > operationsList = new ArrayList <>();
143
145
operationsList .add (operation );
144
146
return new JsonPatch (operationsList ).apply (nodeWithoutIgnoredFields );
145
147
} catch (JsonPatchException e ) {
146
- // If remove for specific path failed , it means that node does not contain specific field which should be ignored.
148
+ // If remove for specific path throws PathNotFound , it means that node does not contain specific field which should be ignored.
147
149
// See more `empty patch if object does not contain ignored field` in diff.json file.
148
- return nodeWithoutIgnoredFields ;
150
+ if (e .getCause () instanceof PathNotFoundException ) {
151
+ return nodeWithoutIgnoredFields ;
152
+ }
153
+ throw e ;
149
154
}
150
155
}
151
156
@@ -175,8 +180,10 @@ public static JsonNode asJson(final JsonNode source, final JsonNode target) {
175
180
* @param target the expected result after applying the patch
176
181
* @param fieldsToIgnore list of JsonPath or JsonPointer paths which should be ignored when generating diff. Non-existing fields are ignored.
177
182
* @return the patch as a {@link JsonNode}
183
+ * @throws JsonPatchException if fieldsToIgnored not in valid JsonPath or JsonPointer format
184
+ * @since 2.0.0
178
185
*/
179
- public static JsonNode asJsonIgnoringFields (final JsonNode source , final JsonNode target , List <String > fieldsToIgnore ) {
186
+ public static JsonNode asJsonIgnoringFields (final JsonNode source , final JsonNode target , List <String > fieldsToIgnore ) throws JsonPatchException {
180
187
final String s ;
181
188
try {
182
189
s = MAPPER .writeValueAsString (asJsonPatchIgnoringFields (source , target , fieldsToIgnore ));
@@ -229,9 +236,9 @@ private static void generateObjectDiffs(final DiffProcessor processor,
229
236
final JsonPointer pointer , final ObjectNode source ,
230
237
final ObjectNode target ) {
231
238
final Set <String > firstFields
232
- = collect (source .fieldNames (), new TreeSet <String >());
239
+ = collect (source .fieldNames (), new TreeSet <>());
233
240
final Set <String > secondFields
234
- = collect (target .fieldNames (), new TreeSet <String >());
241
+ = collect (target .fieldNames (), new TreeSet <>());
235
242
236
243
final Set <String > copy1 = new HashSet <>(firstFields );
237
244
copy1 .removeAll (secondFields );
0 commit comments