Skip to content

Commit 92141e3

Browse files
authored
Adapt collector context documentation (#876)
After #812 some adaptions are required to retrieve data from the CollectorContext.
1 parent 20a179f commit 92141e3

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

doc/collector-context.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,34 @@ collectorContext.add(SAMPLE_COLLECTOR,"sample-string")
3535

3636
```
3737

38-
To validate the schema with the ability to use CollectorContext, validateAndCollect method has to be invoked on the JsonSchema class. This class returns a ValidationResult that contains the errors encountered during validation and a CollectorContext instance. Objects constructed by Collectors or directly added to CollectorContext can be retrieved from CollectorContext by using the name they were added with.
39-
38+
To retrieve the `CollectorContext` after validation or walking, automatic reseting of collector contexts has to be disabled:
4039

4140
```java
42-
ValidationResult validationResult = jsonSchema.validateAndCollect(jsonNode);
43-
CollectorContext context = validationResult.getCollectorContext();
44-
List<String> contextValue = (List<String>)context.get(SAMPLE_COLLECTOR);
45-
41+
SchemaValidatorsConfig configuration = new SchemaValidatorsConfig();
42+
configuration.setResetCollectorContext(false);
43+
JsonSchema jsonSchema = factory.getSchema(uri, configuration);
44+
4645
```
4746

48-
Note that CollectorContext will be removed from ThreadLocal once validateAndCollect method returns. Also the data collected by Collectors is loaded into CollectorContext only after all the validations are done.
47+
To use the `CollectorContext` while validating, the `validateAndCollect` method has to be invoked on the `JsonSchema` class.
48+
This method returns a `ValidationResult` that contains the errors encountered during validation and a `CollectorContext` instance.
49+
Objects constructed by collectors or directly added to `CollectorContext` can be retrieved from `CollectorContext` by using the name they were added with.
50+
51+
52+
```java
53+
List<String> contextValue;
54+
try {
55+
ValidationResult validationResult = jsonSchema.validateAndCollect(jsonNode);
56+
CollectorContext context = validationResult.getCollectorContext();
57+
contextValue = (List<String>)context.get(SAMPLE_COLLECTOR);
58+
} finally {
59+
// Remove all data from collector context.
60+
// Otherwise, data is kept between validation runs which is usually unintended
61+
// and a potential memory leak.
62+
if (CollectorContext.getInstance() != null) CollectorContext.getInstance().reset();
63+
}
64+
// do something with contextValue
65+
```
4966

5067
There might be usecases where a collector needs to collect the data at multiple touch points. For example one usecase might be collecting data in a validator and a formatter. If you are using a Collector rather than a Object, the combine method of the Collector allows to define how we want to combine the data into existing Collector. CollectorContext combineWithCollector method calls the combine method on the Collector. User just needs to call the CollectorContext combineWithCollector method every time some data needs to merged into existing Collector. The collect method on the Collector is called by the framework at the end of validation to return the data that was collected.
5168

0 commit comments

Comments
 (0)