@@ -258,10 +258,14 @@ private JsonNode getMessageNode(JsonNode schemaNode, JsonSchema parentSchema) {
258258
259259 @ Override
260260 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+ }
265269 }
266270
267271 public Set <ValidationMessage > validate (JsonNode jsonNode , JsonNode rootNode , String at ) {
@@ -274,6 +278,10 @@ public Set<ValidationMessage> validate(JsonNode jsonNode, JsonNode rootNode, Str
274278 for (JsonValidator v : getValidators ().values ()) {
275279 errors .addAll (v .validate (jsonNode , rootNode , at ));
276280 }
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+
277285 if (null != config && config .isOpenAPI3StyleDiscriminators ()) {
278286 ObjectNode discriminator = (ObjectNode ) schemaNode .get ("discriminator" );
279287 if (null != discriminator ) {
@@ -324,10 +332,10 @@ private ValidationResult validateAndCollect(JsonNode jsonNode, JsonNode rootNode
324332 SchemaValidatorsConfig config = validationContext .getConfig ();
325333 // Get the collector context from the thread local.
326334 CollectorContext collectorContext = getCollectorContext ();
335+ // Set the walkEnabled and isValidationEnabled flag in internal validator state.
336+ setValidatorState (false , true );
327337 // Validate.
328338 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 ));
331339 // 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.
332340 if (config .doLoadCollectors ()) {
333341 // Load all the data from collectors into the context.
@@ -337,7 +345,9 @@ private ValidationResult validateAndCollect(JsonNode jsonNode, JsonNode rootNode
337345 ValidationResult validationResult = new ValidationResult (errors , collectorContext );
338346 return validationResult ;
339347 } finally {
340- ThreadInfo .remove (CollectorContext .COLLECTOR_CONTEXT_THREAD_LOCAL_KEY );
348+ if (validationContext .getConfig ().isResetCollectorContext ()) {
349+ CollectorContext .getInstance ().reset ();
350+ }
341351 }
342352 }
343353
@@ -353,24 +363,30 @@ private ValidationResult validateAndCollect(JsonNode jsonNode, JsonNode rootNode
353363 * @return result of ValidationResult
354364 */
355365 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+ }
368389 }
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 ;
374390 }
375391
376392 @ Override
@@ -408,6 +424,10 @@ public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at,
408424 validationMessages );
409425 }
410426 }
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+ }
411431 return validationMessages ;
412432 }
413433
0 commit comments