@@ -220,7 +220,12 @@ private Map<String, JsonValidator> read(JsonNode schemaNode) {
220220
221221 public Set <ValidationMessage > validate (JsonNode jsonNode , JsonNode rootNode , String at ) {
222222 Set <ValidationMessage > errors = new LinkedHashSet <ValidationMessage >();
223+ // Get the collector context.
224+ getCollectorContext ();
225+ // Set the walkEnabled and isValidationEnabled flag in internal validator state.
226+ setValidatorState (false , true );
223227 for (JsonValidator v : getValidators ().values ()) {
228+ // Validate.
224229 errors .addAll (v .validate (jsonNode , rootNode , at ));
225230 }
226231 return errors ;
@@ -230,25 +235,21 @@ public ValidationResult validateAndCollect(JsonNode node) {
230235 return validateAndCollect (node , node , AT_ROOT );
231236 }
232237
233-
234238 /**
235- * This method both validates and collects the data in a CollectionContext.
239+ * This method both validates and collects the data in a CollectorContext.
240+ * Unlike others this methods cleans and removes everything from collector
241+ * context before returning.
236242 *
237243 * @param jsonNode JsonNode
238244 * @param rootNode JsonNode
239- * @param at String path
245+ * @param at String path
240246 * @return ValidationResult
241247 */
242248 protected ValidationResult validateAndCollect (JsonNode jsonNode , JsonNode rootNode , String at ) {
243249 try {
244- CollectorContext collectorContext ;
245- if (this .config !=null && this .config .getCollectorContext () != null ){
246- collectorContext = this .config .getCollectorContext ();
247- } else {
248- collectorContext = new CollectorContext ();
249- }
250- // Set the collector context in thread info, this is unique for every thread.
251- ThreadInfo .set (CollectorContext .COLLECTOR_CONTEXT_THREAD_LOCAL_KEY , collectorContext );
250+ // Get the collector context from the thread local.
251+ CollectorContext collectorContext = getCollectorContext ();
252+ // Valdiate.
252253 Set <ValidationMessage > errors = validate (jsonNode , rootNode , at );
253254 // Load all the data from collectors into the context.
254255 collectorContext .loadCollectors ();
@@ -271,58 +272,71 @@ protected ValidationResult validateAndCollect(JsonNode jsonNode, JsonNode rootNo
271272 * @return result of ValidationResult
272273 */
273274 public ValidationResult walk (JsonNode node , boolean shouldValidateSchema ) {
274- // Create the collector context object.
275- CollectorContext collectorContext = new CollectorContext ();
276- // Set the collector context in thread info, this is unique for every thread.
277- ThreadInfo .set (CollectorContext .COLLECTOR_CONTEXT_THREAD_LOCAL_KEY , collectorContext );
275+ // Get the collector context from the thread local.
276+ CollectorContext collectorContext = getCollectorContext ();
278277 // Set the walkEnabled flag in internal validator state.
279278 setValidatorState (true , shouldValidateSchema );
280279 // Walk through the schema.
281- Set <ValidationMessage > errors = walk (node , node , AT_ROOT , shouldValidateSchema );
282- // Load all the data from collectors into the context.
283- collectorContext .loadCollectors ();
284- // Collect errors and collector context into validation result.
285- ValidationResult validationResult = new ValidationResult (errors , collectorContext );
286- return validationResult ;
287- }
280+ Set <ValidationMessage > errors = walk (node , node , AT_ROOT , shouldValidateSchema );
281+ // Load all the data from collectors into the context.
282+ collectorContext .loadCollectors ();
283+ // Collect errors and collector context into validation result.
284+ ValidationResult validationResult = new ValidationResult (errors , collectorContext );
285+ return validationResult ;
286+ }
288287
289288
290289 @ Override
291- public Set <ValidationMessage > walk (JsonNode node , JsonNode rootNode , String at , boolean shouldValidateSchema ) {
292- Set <ValidationMessage > validationMessages = new LinkedHashSet <ValidationMessage >();
293- // Walk through all the JSONWalker's.
294- for (Entry <String , JsonValidator > entry : getValidators ().entrySet ()) {
295- JsonSchemaWalker jsonWalker = entry .getValue ();
296- String schemaPathWithKeyword = entry .getKey ();
297- try {
298- // Call all the pre-walk listeners. If all the pre- walk listeners return true
299- // then continue to walk method .
300- if (keywordWalkListenerRunner .runPreWalkListeners (schemaPathWithKeyword , node , rootNode , at , schemaPath ,
301- schemaNode , parentSchema , validationContext .getJsonSchemaFactory ())) {
302- validationMessages .addAll (jsonWalker .walk (node , rootNode , at , shouldValidateSchema ));
303- }
304- } finally {
305- // Call all the post-walk listeners.
306- keywordWalkListenerRunner .runPostWalkListeners (schemaPathWithKeyword , node , rootNode , at , schemaPath ,
307- schemaNode , parentSchema , validationContext .getJsonSchemaFactory (), validationMessages );
308- }
309- }
310- return validationMessages ;
290+ public Set <ValidationMessage > walk (JsonNode node , JsonNode rootNode , String at , boolean shouldValidateSchema ) {
291+ Set <ValidationMessage > validationMessages = new LinkedHashSet <ValidationMessage >();
292+ // Walk through all the JSONWalker's.
293+ for (Entry <String , JsonValidator > entry : getValidators ().entrySet ()) {
294+ JsonSchemaWalker jsonWalker = entry .getValue ();
295+ String schemaPathWithKeyword = entry .getKey ();
296+ try {
297+ // Call all the pre-walk listeners. If atleast one of the pre walk listeners
298+ // returns SKIP, then skip the walk.
299+ if (keywordWalkListenerRunner .runPreWalkListeners (schemaPathWithKeyword , node , rootNode , at , schemaPath ,
300+ schemaNode , parentSchema , validationContext .getJsonSchemaFactory ())) {
301+ validationMessages .addAll (jsonWalker .walk (node , rootNode , at , shouldValidateSchema ));
302+ }
303+ } finally {
304+ // Call all the post-walk listeners.
305+ keywordWalkListenerRunner .runPostWalkListeners (schemaPathWithKeyword , node , rootNode , at , schemaPath ,
306+ schemaNode , parentSchema , validationContext .getJsonSchemaFactory (), validationMessages );
307+ }
308+ }
309+ return validationMessages ;
311310 }
312311
313312 /************************ END OF WALK METHODS **********************************/
314313
315- private void setValidatorState (boolean isWalkEnabled , boolean shouldValidateSchema ) {
316- // Get the Validator state object storing validation data
317- ValidatorState state = validatorState .get ();
318- if (state == null ) {
319- // if one has not been created, instantiate one
320- state = new ValidatorState ();
321- state .setWalkEnabled (isWalkEnabled );
322- state .setValidationEnabledWhileWalking (shouldValidateSchema );
323- validatorState .set (state );
324- }
325- }
314+ private void setValidatorState (boolean isWalkEnabled , boolean shouldValidateSchema ) {
315+ // Get the Validator state object storing validation data
316+ Object stateObj = CollectorContext .getInstance ().get (ValidatorState .VALIDATOR_STATE_KEY );
317+ // if one has not been created, instantiate one
318+ if (stateObj == null ) {
319+ ValidatorState state = new ValidatorState ();
320+ state .setWalkEnabled (isWalkEnabled );
321+ state .setValidationEnabled (shouldValidateSchema );
322+ CollectorContext .getInstance ().add (ValidatorState .VALIDATOR_STATE_KEY , state );
323+ }
324+ }
325+
326+
327+ public CollectorContext getCollectorContext () {
328+ CollectorContext collectorContext = (CollectorContext ) ThreadInfo .get (CollectorContext .COLLECTOR_CONTEXT_THREAD_LOCAL_KEY );
329+ if (collectorContext == null ) {
330+ if (this .config != null && this .config .getCollectorContext () != null ) {
331+ collectorContext = this .config .getCollectorContext ();
332+ } else {
333+ collectorContext = new CollectorContext ();
334+ }
335+ // Set the collector context in thread info, this is unique for every thread.
336+ ThreadInfo .set (CollectorContext .COLLECTOR_CONTEXT_THREAD_LOCAL_KEY , collectorContext );
337+ }
338+ return collectorContext ;
339+ }
326340
327341 @ Override
328342 public String toString () {
0 commit comments