diff --git a/src/main/java/com/networknt/schema/AdditionalPropertiesValidator.java b/src/main/java/com/networknt/schema/AdditionalPropertiesValidator.java index f4f10cf02..505d50eb4 100644 --- a/src/main/java/com/networknt/schema/AdditionalPropertiesValidator.java +++ b/src/main/java/com/networknt/schema/AdditionalPropertiesValidator.java @@ -93,9 +93,9 @@ public Set validate(JsonNode node, JsonNode rootNode, String errors.add(buildValidationMessage(at, pname)); } else { if (additionalPropertiesSchema != null) { - ValidatorState state = validatorState.get(); + ValidatorState state = (ValidatorState) CollectorContext.getInstance().get(ValidatorState.VALIDATOR_STATE_KEY); if (state != null && state.isWalkEnabled()) { - errors.addAll(additionalPropertiesSchema.walk(node.get(pname), rootNode, at + "." + pname, state.isValidationEnabledWhileWalking())); + errors.addAll(additionalPropertiesSchema.walk(node.get(pname), rootNode, at + "." + pname, state.isValidationEnabled())); } else { errors.addAll(additionalPropertiesSchema.validate(node.get(pname), rootNode, at + "." + pname)); } diff --git a/src/main/java/com/networknt/schema/BaseJsonValidator.java b/src/main/java/com/networknt/schema/BaseJsonValidator.java index 53d1c8fa3..629925997 100644 --- a/src/main/java/com/networknt/schema/BaseJsonValidator.java +++ b/src/main/java/com/networknt/schema/BaseJsonValidator.java @@ -37,11 +37,6 @@ public abstract class BaseJsonValidator implements JsonValidator { protected SchemaValidatorsConfig config; protected final boolean failFast; - /** - * ThreadLocal to allow to pass state in recursive validator calls - */ - protected final static ThreadLocal validatorState = new ThreadLocal(); - public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidatorTypeCode validatorType, ValidationContext validationContext) { this(schemaPath, schemaNode, parentSchema, validatorType, false, diff --git a/src/main/java/com/networknt/schema/CollectorContext.java b/src/main/java/com/networknt/schema/CollectorContext.java index ea895f052..b2b242f09 100644 --- a/src/main/java/com/networknt/schema/CollectorContext.java +++ b/src/main/java/com/networknt/schema/CollectorContext.java @@ -118,7 +118,7 @@ public void combineWithCollector(String name, Object data) { /** * Reset the context */ - void reset() { + public void reset() { this.collectorMap = new HashMap(); this.collectorLoadMap = new HashMap(); } diff --git a/src/main/java/com/networknt/schema/JsonSchema.java b/src/main/java/com/networknt/schema/JsonSchema.java index 098c90fc0..ac6497c79 100644 --- a/src/main/java/com/networknt/schema/JsonSchema.java +++ b/src/main/java/com/networknt/schema/JsonSchema.java @@ -220,7 +220,12 @@ private Map read(JsonNode schemaNode) { public Set validate(JsonNode jsonNode, JsonNode rootNode, String at) { Set errors = new LinkedHashSet(); + // Get the collector context. + getCollectorContext(); + // Set the walkEnabled and isValidationEnabled flag in internal validator state. + setValidatorState(false, true); for (JsonValidator v : getValidators().values()) { + // Validate. errors.addAll(v.validate(jsonNode, rootNode, at)); } return errors; @@ -230,25 +235,21 @@ public ValidationResult validateAndCollect(JsonNode node) { return validateAndCollect(node, node, AT_ROOT); } - /** - * This method both validates and collects the data in a CollectionContext. + * This method both validates and collects the data in a CollectorContext. + * Unlike others this methods cleans and removes everything from collector + * context before returning. * * @param jsonNode JsonNode * @param rootNode JsonNode - * @param at String path + * @param at String path * @return ValidationResult */ protected ValidationResult validateAndCollect(JsonNode jsonNode, JsonNode rootNode, String at) { try { - CollectorContext collectorContext; - if(this.config !=null && this.config.getCollectorContext() != null){ - collectorContext = this.config.getCollectorContext(); - } else { - collectorContext = new CollectorContext(); - } - // Set the collector context in thread info, this is unique for every thread. - ThreadInfo.set(CollectorContext.COLLECTOR_CONTEXT_THREAD_LOCAL_KEY, collectorContext); + // Get the collector context from the thread local. + CollectorContext collectorContext = getCollectorContext(); + // Valdiate. Set errors = validate(jsonNode, rootNode, at); // Load all the data from collectors into the context. collectorContext.loadCollectors(); @@ -271,58 +272,71 @@ protected ValidationResult validateAndCollect(JsonNode jsonNode, JsonNode rootNo * @return result of ValidationResult */ public ValidationResult walk(JsonNode node, boolean shouldValidateSchema) { - // Create the collector context object. - CollectorContext collectorContext = new CollectorContext(); - // Set the collector context in thread info, this is unique for every thread. - ThreadInfo.set(CollectorContext.COLLECTOR_CONTEXT_THREAD_LOCAL_KEY, collectorContext); + // Get the collector context from the thread local. + CollectorContext collectorContext = getCollectorContext(); // Set the walkEnabled flag in internal validator state. setValidatorState(true, shouldValidateSchema); // Walk through the schema. - Set errors = walk(node, node, AT_ROOT, shouldValidateSchema); - // Load all the data from collectors into the context. - collectorContext.loadCollectors(); - // Collect errors and collector context into validation result. - ValidationResult validationResult = new ValidationResult(errors, collectorContext); - return validationResult; - } + Set errors = walk(node, node, AT_ROOT, shouldValidateSchema); + // Load all the data from collectors into the context. + collectorContext.loadCollectors(); + // Collect errors and collector context into validation result. + ValidationResult validationResult = new ValidationResult(errors, collectorContext); + return validationResult; + } @Override - public Set walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) { - Set validationMessages = new LinkedHashSet(); - // Walk through all the JSONWalker's. - for (Entry entry : getValidators().entrySet()) { - JsonSchemaWalker jsonWalker = entry.getValue(); - String schemaPathWithKeyword = entry.getKey(); - try { - // Call all the pre-walk listeners. If all the pre-walk listeners return true - // then continue to walk method. - if (keywordWalkListenerRunner.runPreWalkListeners(schemaPathWithKeyword, node, rootNode, at, schemaPath, - schemaNode, parentSchema, validationContext.getJsonSchemaFactory())) { - validationMessages.addAll(jsonWalker.walk(node, rootNode, at, shouldValidateSchema)); - } - } finally { - // Call all the post-walk listeners. - keywordWalkListenerRunner.runPostWalkListeners(schemaPathWithKeyword, node, rootNode, at, schemaPath, - schemaNode, parentSchema, validationContext.getJsonSchemaFactory(), validationMessages); - } - } - return validationMessages; + public Set walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) { + Set validationMessages = new LinkedHashSet(); + // Walk through all the JSONWalker's. + for (Entry entry : getValidators().entrySet()) { + JsonSchemaWalker jsonWalker = entry.getValue(); + String schemaPathWithKeyword = entry.getKey(); + try { + // Call all the pre-walk listeners. If atleast one of the pre walk listeners + // returns SKIP, then skip the walk. + if (keywordWalkListenerRunner.runPreWalkListeners(schemaPathWithKeyword, node, rootNode, at, schemaPath, + schemaNode, parentSchema, validationContext.getJsonSchemaFactory())) { + validationMessages.addAll(jsonWalker.walk(node, rootNode, at, shouldValidateSchema)); + } + } finally { + // Call all the post-walk listeners. + keywordWalkListenerRunner.runPostWalkListeners(schemaPathWithKeyword, node, rootNode, at, schemaPath, + schemaNode, parentSchema, validationContext.getJsonSchemaFactory(), validationMessages); + } + } + return validationMessages; } /************************ END OF WALK METHODS **********************************/ - private void setValidatorState(boolean isWalkEnabled, boolean shouldValidateSchema) { - // Get the Validator state object storing validation data - ValidatorState state = validatorState.get(); - if (state == null) { - // if one has not been created, instantiate one - state = new ValidatorState(); - state.setWalkEnabled(isWalkEnabled); - state.setValidationEnabledWhileWalking(shouldValidateSchema); - validatorState.set(state); - } - } + private void setValidatorState(boolean isWalkEnabled, boolean shouldValidateSchema) { + // Get the Validator state object storing validation data + Object stateObj = CollectorContext.getInstance().get(ValidatorState.VALIDATOR_STATE_KEY); + // if one has not been created, instantiate one + if (stateObj == null) { + ValidatorState state = new ValidatorState(); + state.setWalkEnabled(isWalkEnabled); + state.setValidationEnabled(shouldValidateSchema); + CollectorContext.getInstance().add(ValidatorState.VALIDATOR_STATE_KEY, state); + } + } + + + public CollectorContext getCollectorContext() { + CollectorContext collectorContext = (CollectorContext) ThreadInfo.get(CollectorContext.COLLECTOR_CONTEXT_THREAD_LOCAL_KEY); + if (collectorContext == null) { + if (this.config != null && this.config.getCollectorContext() != null) { + collectorContext = this.config.getCollectorContext(); + } else { + collectorContext = new CollectorContext(); + } + // Set the collector context in thread info, this is unique for every thread. + ThreadInfo.set(CollectorContext.COLLECTOR_CONTEXT_THREAD_LOCAL_KEY, collectorContext); + } + return collectorContext; + } @Override public String toString() { diff --git a/src/main/java/com/networknt/schema/OneOfValidator.java b/src/main/java/com/networknt/schema/OneOfValidator.java index e15194ac7..91af23bde 100644 --- a/src/main/java/com/networknt/schema/OneOfValidator.java +++ b/src/main/java/com/networknt/schema/OneOfValidator.java @@ -128,15 +128,10 @@ public OneOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS public Set validate(JsonNode node, JsonNode rootNode, String at) { debug(logger, node, rootNode, at); - ValidatorState state = validatorState.get(); - if (state == null) { - state = new ValidatorState(); - validatorState.set(state); - } + ValidatorState state = (ValidatorState) CollectorContext.getInstance().get(ValidatorState.VALIDATOR_STATE_KEY); // this is a complex validator, we set the flag to true state.setComplexValidator(true); - int numberOfValidSchema = 0; Set errors = new LinkedHashSet(); Set childErrors = new LinkedHashSet(); @@ -164,7 +159,7 @@ public Set validate(JsonNode node, JsonNode rootNode, String if (!state.isWalkEnabled()) { schemaErrors = schema.validate(node, rootNode, at); } else { - schemaErrors = schema.walk(node, rootNode, at, state.isValidationEnabledWhileWalking()); + schemaErrors = schema.walk(node, rootNode, at, state.isValidationEnabled()); } // check if any validation errors have occurred @@ -202,11 +197,17 @@ else if (numberOfValidSchema < 1) { state.setMatchedNode(true); // reset the ValidatorState object in the ThreadLocal - validatorState.remove(); + resetValidatorState(); return Collections.unmodifiableSet(errors); } + private void resetValidatorState() { + ValidatorState state = (ValidatorState) CollectorContext.getInstance().get(ValidatorState.VALIDATOR_STATE_KEY); + state.setComplexValidator(false); + state.setMatchedNode(true); + } + public List getChildSchemas() { List childJsonSchemas = new ArrayList(); for (ShortcutValidator shortcutValidator: schemas ) { @@ -241,4 +242,5 @@ private ValidationMessage getMultiSchemasValidErrorMsg(String at){ return message; } + } diff --git a/src/main/java/com/networknt/schema/PropertiesValidator.java b/src/main/java/com/networknt/schema/PropertiesValidator.java index cc7a74590..67ef30ef0 100644 --- a/src/main/java/com/networknt/schema/PropertiesValidator.java +++ b/src/main/java/com/networknt/schema/PropertiesValidator.java @@ -49,12 +49,7 @@ public Set validate(JsonNode node, JsonNode rootNode, String Set errors = new LinkedHashSet(); // get the Validator state object storing validation data - ValidatorState state = validatorState.get(); - if (state == null) { - // if one has not been created, instantiate one - state = new ValidatorState(); - validatorState.set(state); - } + ValidatorState state = (ValidatorState) CollectorContext.getInstance().get(ValidatorState.VALIDATOR_STATE_KEY); for (Map.Entry entry : schemas.entrySet()) { JsonSchema propertySchema = entry.getValue(); @@ -75,7 +70,7 @@ public Set validate(JsonNode node, JsonNode rootNode, String errors.addAll(propertySchema.validate(propertyNode, rootNode, at + "." + entry.getKey())); } else { // check if walker is enabled. If it is enabled it is upto the walker implementation to decide about the validation. - walkSchema(entry, node, rootNode, at, state.isValidationEnabledWhileWalking(), errors); + walkSchema(entry, node, rootNode, at, state.isValidationEnabled(), errors); } // reset the complex flag to the original value before the recursive call diff --git a/src/main/java/com/networknt/schema/ValidatorState.java b/src/main/java/com/networknt/schema/ValidatorState.java index e3a80d859..2664ea126 100644 --- a/src/main/java/com/networknt/schema/ValidatorState.java +++ b/src/main/java/com/networknt/schema/ValidatorState.java @@ -16,6 +16,9 @@ package com.networknt.schema; public class ValidatorState { + + static final String VALIDATOR_STATE_KEY = "com.networknt.schema.ValidatorState"; + /** * Flag set when a node has matched Works in conjunction with the next flag: * isComplexValidator, to be used for complex validators such as oneOf, for ex @@ -37,7 +40,7 @@ public class ValidatorState { /** * Flag to check if validation is enabled while walking. */ - private boolean isValidationEnabledWhileWalking = false; + private boolean isValidationEnabled = false; public void setMatchedNode(boolean matchedNode) { this.matchedNode = matchedNode; @@ -63,12 +66,12 @@ public void setWalkEnabled(boolean isWalkEnabled) { this.isWalkEnabled = isWalkEnabled; } - public boolean isValidationEnabledWhileWalking() { - return isValidationEnabledWhileWalking; + public boolean isValidationEnabled() { + return isValidationEnabled; } - public void setValidationEnabledWhileWalking(boolean isValidationEnabledWhileWalking) { - this.isValidationEnabledWhileWalking = isValidationEnabledWhileWalking; + public void setValidationEnabled(boolean isValidationEnabled) { + this.isValidationEnabled = isValidationEnabled; } } diff --git a/src/main/java/com/networknt/schema/walk/AbstractWalkListenerRunner.java b/src/main/java/com/networknt/schema/walk/AbstractWalkListenerRunner.java index 23631fb86..e21db44b0 100644 --- a/src/main/java/com/networknt/schema/walk/AbstractWalkListenerRunner.java +++ b/src/main/java/com/networknt/schema/walk/AbstractWalkListenerRunner.java @@ -23,20 +23,23 @@ protected WalkEvent constructWalkEvent(String keyWordName, JsonNode node, JsonNo } protected boolean runPreWalkListeners(List walkListeners, WalkEvent walkEvent) { - boolean continueRunningListenersAndWalk = true; + boolean continueToWalkMethod = true; if (walkListeners != null) { for (JsonSchemaWalkListener walkListener : walkListeners) { - if (WalkFlow.SKIP.equals(walkListener.onWalkStart(walkEvent))) { - continueRunningListenersAndWalk = false; - break; + WalkFlow walkFlow = walkListener.onWalkStart(walkEvent); + if (WalkFlow.SKIP.equals(walkFlow) || WalkFlow.ABORT.equals(walkFlow)) { + continueToWalkMethod = false; + if (WalkFlow.ABORT.equals(walkFlow)) { + break; + } } } } - return continueRunningListenersAndWalk; + return continueToWalkMethod; } protected void runPostWalkListeners(List walkListeners, WalkEvent walkEvent, - Set validationMessages) { + Set validationMessages) { if (walkListeners != null) { for (JsonSchemaWalkListener walkListener : walkListeners) { walkListener.onWalkEnd(walkEvent, validationMessages); diff --git a/src/main/java/com/networknt/schema/walk/WalkEvent.java b/src/main/java/com/networknt/schema/walk/WalkEvent.java index 97a04373d..cd950d6a7 100644 --- a/src/main/java/com/networknt/schema/walk/WalkEvent.java +++ b/src/main/java/com/networknt/schema/walk/WalkEvent.java @@ -54,6 +54,10 @@ public JsonSchema getRefSchema(URI schemaUri) { return currentJsonSchemaFactory.getSchema(schemaUri); } + public JsonSchemaFactory getCurrentJsonSchemaFactory() { + return currentJsonSchemaFactory; + } + static class WalkEventBuilder { private WalkEvent keywordWalkEvent = null; diff --git a/src/main/java/com/networknt/schema/walk/WalkFlow.java b/src/main/java/com/networknt/schema/walk/WalkFlow.java index 04d3cbbf2..af2da7b5c 100644 --- a/src/main/java/com/networknt/schema/walk/WalkFlow.java +++ b/src/main/java/com/networknt/schema/walk/WalkFlow.java @@ -2,9 +2,11 @@ public enum WalkFlow { - SKIP("SkipWalk", "Skip the walk methods"), + SKIP("SkipWalk", "Skip only the walk method, but continue invoking the other listeners"), - CONTINUE("ContinueToWalk", "continue to invoke the walk method"); + ABORT("Abort", "Aborts all the walk listeners and walk method itself"), + + CONTINUE("ContinueToWalk", "continue to invoke the walk method and other listeners"); private String name; diff --git a/src/test/java/com/networknt/schema/CollectorContextTest.java b/src/test/java/com/networknt/schema/CollectorContextTest.java index dcbdf5661..2ba81aebe 100644 --- a/src/test/java/com/networknt/schema/CollectorContextTest.java +++ b/src/test/java/com/networknt/schema/CollectorContextTest.java @@ -19,6 +19,8 @@ import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; + +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -28,20 +30,25 @@ public class CollectorContextTest { - private static final String SAMPLE_COLLECTOR = "sampleCollectorType"; + private static final String SAMPLE_COLLECTOR = "sampleCollector"; - private static final String SAMPLE_COLLECTOR_OTHER = "sampleCollectorOtherType"; + private static final String SAMPLE_COLLECTOR_OTHER = "sampleCollectorOther"; private JsonSchema jsonSchema; private JsonSchema jsonSchemaForCombine; - @Before public void setup() throws Exception { setupSchema(); } + @After + public void cleanup() { + if (CollectorContext.getInstance() != null) { + CollectorContext.getInstance().reset(); + } + } @SuppressWarnings("unchecked") @Test @@ -57,7 +64,7 @@ public void testCollectorContextWithKeyword() throws Exception { @SuppressWarnings("unchecked") @Test - public void testCollectorContextWithMultiplThreads() throws Exception { + public void testCollectorContextWithMultipleThreads() throws Exception { ValidationThread validationRunnable1 = new ValidationThread("{\"test-property1\":\"sample1\" }", "thread1"); ValidationThread validationRunnable2 = new ValidationThread("{\"test-property1\":\"sample2\" }", "thread2"); @@ -95,28 +102,15 @@ public void testCollectorContextWithMultiplThreads() throws Exception { Assert.assertEquals(contextValue3.get(0), "actual_value_added_to_context3"); } - @SuppressWarnings("unchecked") - @Test - public void testCollectorWithFormat() throws JsonMappingException, JsonProcessingException, IOException { - ObjectMapper objectMapper = new ObjectMapper(); - ValidationResult validationResult = jsonSchemaForCombine.validateAndCollect(objectMapper - .readTree("{\"property1\":\"sample1\",\"property2\":\"sample2\",\"property3\":\"sample3\" }")); - List values = (List) validationResult.getCollectorContext().get(SAMPLE_COLLECTOR); - List values1 = (List) validationResult.getCollectorContext().get(SAMPLE_COLLECTOR_OTHER); - Assert.assertEquals(values.size(), 1); - Assert.assertEquals(values1.size(), 3); - } - @SuppressWarnings("unchecked") @Test public void testCollectorGetAll() throws JsonMappingException, JsonProcessingException, IOException { ObjectMapper objectMapper = new ObjectMapper(); ValidationResult validationResult = jsonSchemaForCombine.validateAndCollect(objectMapper .readTree("{\"property1\":\"sample1\",\"property2\":\"sample2\",\"property3\":\"sample3\" }")); - Map map = validationResult.getCollectorContext().getAll(); - Iterator collectionIterator = map.values().iterator(); - Assert.assertEquals(((List) collectionIterator.next()).size(), 1); - Assert.assertEquals(((List) collectionIterator.next()).size(), 3); + CollectorContext collectorContext = validationResult.getCollectorContext(); + Assert.assertEquals(((List) collectorContext.get(SAMPLE_COLLECTOR)).size(), 1); + Assert.assertEquals(((List) collectorContext.get(SAMPLE_COLLECTOR_OTHER)).size(), 3); } private JsonMetaSchema getJsonMetaSchema(String uri) throws Exception { @@ -216,7 +210,6 @@ private String getSchemaStringMultipleProperties() { + "}"; } - private class ValidationThread implements Runnable { private String data; @@ -266,7 +259,7 @@ public String getValue() { @Override public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, - ValidationContext validationContext) throws JsonSchemaException, Exception { + ValidationContext validationContext) throws JsonSchemaException, Exception { if (schemaNode != null && schemaNode.isArray()) { return new CustomValidator(); } @@ -274,7 +267,6 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc } } - /** * We will be collecting information/data by adding the data in the form of * collectors into collector context object while we are validating this node. @@ -299,11 +291,11 @@ public Set validate(JsonNode rootNode) { return validate(rootNode, rootNode, BaseJsonValidator.AT_ROOT); } - @Override - public Set walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) { - // Ignore this method for testing. - return null; - } + @Override + public Set walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) { + // Ignore this method for testing. + return null; + } } @@ -341,7 +333,7 @@ public String getValue() { @Override public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, - ValidationContext validationContext) throws JsonSchemaException, Exception { + ValidationContext validationContext) throws JsonSchemaException, Exception { if (schemaNode != null && schemaNode.isArray()) { return new CustomValidator1(); } @@ -378,10 +370,10 @@ public Set validate(JsonNode rootNode) { } @Override - public Set walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) { - // Ignore this method for testing. - return null; - } + public Set walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) { + // Ignore this method for testing. + return null; + } } private ValidationResult validate(String jsonData) throws JsonMappingException, JsonProcessingException, Exception { diff --git a/src/test/java/com/networknt/schema/JsonWalkTest.java b/src/test/java/com/networknt/schema/JsonWalkTest.java index 26c902d8f..ae9422341 100644 --- a/src/test/java/com/networknt/schema/JsonWalkTest.java +++ b/src/test/java/com/networknt/schema/JsonWalkTest.java @@ -6,6 +6,8 @@ import com.networknt.schema.walk.JsonSchemaWalkListener; import com.networknt.schema.walk.WalkEvent; import com.networknt.schema.walk.WalkFlow; + +import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -21,7 +23,7 @@ public class JsonWalkTest { private JsonSchema jsonSchema; - private static final String SAMPLE_COLLECTOR = "sampleCollectorType"; + private static final String SAMPLE_WALK_COLLECTOR_TYPE = "sampleWalkCollectorType"; private static final String CUSTOM_KEYWORD = "custom-keyword"; @@ -30,6 +32,11 @@ public void setup() { setupSchema(); } + @After + public void cleanup() { + CollectorContext.getInstance().reset(); + } + private void setupSchema() { final JsonMetaSchema metaSchema = getJsonMetaSchema(); SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig(); @@ -54,7 +61,7 @@ public void testWalk() throws IOException { ObjectMapper objectMapper = new ObjectMapper(); ValidationResult result = jsonSchema.walk( objectMapper.readTree(getClass().getClassLoader().getResourceAsStream("data/walk-data.json")), false); - JsonNode collectedNode = (JsonNode) result.getCollectorContext().get(SAMPLE_COLLECTOR); + JsonNode collectedNode = (JsonNode) result.getCollectorContext().get(SAMPLE_WALK_COLLECTOR_TYPE); assertEquals(collectedNode, (objectMapper.readTree("{" + " \"PROPERTY1\": \"sample1\"," + " \"PROPERTY2\": \"sample2\"," @@ -123,11 +130,11 @@ public WalkFlow onWalkStart(WalkEvent keywordWalkEvent) { String keyWordName = keywordWalkEvent.getKeyWordName(); JsonNode schemaNode = keywordWalkEvent.getSchemaNode(); CollectorContext collectorContext = CollectorContext.getInstance(); - if (collectorContext.get(SAMPLE_COLLECTOR) == null) { - collectorContext.add(SAMPLE_COLLECTOR, mapper.createObjectNode()); + if (collectorContext.get(SAMPLE_WALK_COLLECTOR_TYPE) == null) { + collectorContext.add(SAMPLE_WALK_COLLECTOR_TYPE, mapper.createObjectNode()); } if (keyWordName.equals(CUSTOM_KEYWORD) && schemaNode.get(CUSTOM_KEYWORD).isArray()) { - ObjectNode objectNode = (ObjectNode) collectorContext.get(SAMPLE_COLLECTOR); + ObjectNode objectNode = (ObjectNode) collectorContext.get(SAMPLE_WALK_COLLECTOR_TYPE); objectNode.put(keywordWalkEvent.getSchemaNode().get("title").textValue().toUpperCase(), keywordWalkEvent.getNode().textValue()); } @@ -146,10 +153,10 @@ private static class RefKeywordListener implements JsonSchemaWalkListener { public WalkFlow onWalkStart(WalkEvent keywordWalkEvent) { ObjectMapper mapper = new ObjectMapper(); CollectorContext collectorContext = CollectorContext.getInstance(); - if (collectorContext.get(SAMPLE_COLLECTOR) == null) { - collectorContext.add(SAMPLE_COLLECTOR, mapper.createObjectNode()); + if (collectorContext.get(SAMPLE_WALK_COLLECTOR_TYPE) == null) { + collectorContext.add(SAMPLE_WALK_COLLECTOR_TYPE, mapper.createObjectNode()); } - ObjectNode objectNode = (ObjectNode) collectorContext.get(SAMPLE_COLLECTOR); + ObjectNode objectNode = (ObjectNode) collectorContext.get(SAMPLE_WALK_COLLECTOR_TYPE); objectNode.set(keywordWalkEvent.getSchemaNode().get("title").textValue().toLowerCase(), keywordWalkEvent.getNode()); return WalkFlow.SKIP; diff --git a/src/test/java/com/networknt/schema/TypeFactoryTest.java b/src/test/java/com/networknt/schema/TypeFactoryTest.java index f61465c96..d20a2cfe1 100755 --- a/src/test/java/com/networknt/schema/TypeFactoryTest.java +++ b/src/test/java/com/networknt/schema/TypeFactoryTest.java @@ -51,6 +51,7 @@ public void testValidIntegralValuesWithoutJavaSemantics() { } } + @Test public void testWithLosslessNarrowing() { schemaValidatorsConfig.setLosslessNarrowing(true); @@ -73,5 +74,6 @@ public void testWithoutLosslessNarrowing() { assertSame(validValue, JsonType.NUMBER, getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.5")), schemaValidatorsConfig)); } + } -} +} \ No newline at end of file