@@ -258,10 +258,14 @@ private JsonNode getMessageNode(JsonNode schemaNode, JsonSchema parentSchema) {
258
258
259
259
@ Override
260
260
public Set <ValidationMessage > validate (JsonNode node ) {
261
- Set <ValidationMessage > errors = validate (node , node , AT_ROOT );
262
- // Process UnEvaluatedProperties after all the validators are called.
263
- errors .addAll (processUnEvaluatedProperties (node , node , AT_ROOT , true , true ));
264
- return errors ;
261
+ try {
262
+ Set <ValidationMessage > errors = validate (node , node , AT_ROOT );
263
+ return errors ;
264
+ } finally {
265
+ if (validationContext .getConfig ().isResetCollectorContext ()) {
266
+ CollectorContext .getInstance ().reset ();
267
+ }
268
+ }
265
269
}
266
270
267
271
public Set <ValidationMessage > validate (JsonNode jsonNode , JsonNode rootNode , String at ) {
@@ -274,6 +278,10 @@ public Set<ValidationMessage> validate(JsonNode jsonNode, JsonNode rootNode, Str
274
278
for (JsonValidator v : getValidators ().values ()) {
275
279
errors .addAll (v .validate (jsonNode , rootNode , at ));
276
280
}
281
+
282
+ // Process UnEvaluatedProperties after all the validators are called if there are no errors.
283
+ errors .addAll (processUnEvaluatedProperties (jsonNode , rootNode , at , true , true ));
284
+
277
285
if (null != config && config .isOpenAPI3StyleDiscriminators ()) {
278
286
ObjectNode discriminator = (ObjectNode ) schemaNode .get ("discriminator" );
279
287
if (null != discriminator ) {
@@ -324,10 +332,10 @@ private ValidationResult validateAndCollect(JsonNode jsonNode, JsonNode rootNode
324
332
SchemaValidatorsConfig config = validationContext .getConfig ();
325
333
// Get the collector context from the thread local.
326
334
CollectorContext collectorContext = getCollectorContext ();
335
+ // Set the walkEnabled and isValidationEnabled flag in internal validator state.
336
+ setValidatorState (false , true );
327
337
// Validate.
328
338
Set <ValidationMessage > errors = validate (jsonNode , rootNode , at );
329
- // Validate UnEvaluatedProperties after all the validators are processed.
330
- errors .addAll (processUnEvaluatedProperties (jsonNode , rootNode , at , true , true ));
331
339
// When walk is called in series of nested call we don't want to load the collectors every time. Leave to the API to decide when to call collectors.
332
340
if (config .doLoadCollectors ()) {
333
341
// Load all the data from collectors into the context.
@@ -337,7 +345,9 @@ private ValidationResult validateAndCollect(JsonNode jsonNode, JsonNode rootNode
337
345
ValidationResult validationResult = new ValidationResult (errors , collectorContext );
338
346
return validationResult ;
339
347
} finally {
340
- ThreadInfo .remove (CollectorContext .COLLECTOR_CONTEXT_THREAD_LOCAL_KEY );
348
+ if (validationContext .getConfig ().isResetCollectorContext ()) {
349
+ CollectorContext .getInstance ().reset ();
350
+ }
341
351
}
342
352
}
343
353
@@ -353,24 +363,30 @@ private ValidationResult validateAndCollect(JsonNode jsonNode, JsonNode rootNode
353
363
* @return result of ValidationResult
354
364
*/
355
365
public ValidationResult walk (JsonNode node , boolean shouldValidateSchema ) {
356
- // Get the config.
357
- SchemaValidatorsConfig config = validationContext .getConfig ();
358
- // Get the collector context from the thread local.
359
- CollectorContext collectorContext = getCollectorContext ();
360
- // Set the walkEnabled flag in internal validator state.
361
- setValidatorState (true , shouldValidateSchema );
362
- // Walk through the schema.
363
- Set <ValidationMessage > errors = walk (node , node , AT_ROOT , shouldValidateSchema );
364
- // When walk is called in series of nested call we don't want to load the collectors every time. Leave to the API to decide when to call collectors.
365
- if (config .doLoadCollectors ()) {
366
- // Load all the data from collectors into the context.
367
- collectorContext .loadCollectors ();
366
+ try {
367
+ // Get the config.
368
+ SchemaValidatorsConfig config = validationContext .getConfig ();
369
+ // Get the collector context from the thread local.
370
+ CollectorContext collectorContext = getCollectorContext ();
371
+ // Set the walkEnabled flag in internal validator state.
372
+ setValidatorState (true , shouldValidateSchema );
373
+ // Walk through the schema.
374
+ Set <ValidationMessage > errors = walk (node , node , AT_ROOT , shouldValidateSchema );
375
+ // When walk is called in series of nested call we don't want to load the collectors every time. Leave to the API to decide when to call collectors.
376
+ if (config .doLoadCollectors ()) {
377
+ // Load all the data from collectors into the context.
378
+ collectorContext .loadCollectors ();
379
+ }
380
+ // Process UnEvaluatedProperties after all the validators are called.
381
+ errors .addAll (processUnEvaluatedProperties (node , node , AT_ROOT , shouldValidateSchema , false ));
382
+ // Collect errors and collector context into validation result.
383
+ ValidationResult validationResult = new ValidationResult (errors , collectorContext );
384
+ return validationResult ;
385
+ } finally {
386
+ if (validationContext .getConfig ().isResetCollectorContext ()) {
387
+ CollectorContext .getInstance ().reset ();
388
+ }
368
389
}
369
- // Process UnEvaluatedProperties after all the validators are called.
370
- errors .addAll (processUnEvaluatedProperties (node , node , AT_ROOT , shouldValidateSchema , false ));
371
- // Collect errors and collector context into validation result.
372
- ValidationResult validationResult = new ValidationResult (errors , collectorContext );
373
- return validationResult ;
374
390
}
375
391
376
392
@ Override
@@ -408,6 +424,10 @@ public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at,
408
424
validationMessages );
409
425
}
410
426
}
427
+ if (shouldValidateSchema ) {
428
+ // Process UnEvaluatedProperties after all the validators are called if there are no errors.
429
+ validationMessages .addAll (processUnEvaluatedProperties (node , rootNode , at , true , true ));
430
+ }
411
431
return validationMessages ;
412
432
}
413
433
0 commit comments