networknt / json-schema-validator

A fast Java JSON schema validator that supports draft V4, V6, V7, V2019-09 and V2020-12
Apache License 2.0
800 stars 320 forks source link

Deprecate `validateAndCollect` on `JsonSchema` #1063

Closed justin-tay closed 3 weeks ago

justin-tay commented 3 weeks ago

Deprecate validateAndCollect in favor of explicitly calling loadCollectors.

This also deprecates the related loadCollectors configuration in SchemaValidatorsConfig.

This makes the CollectorContext.loadCollectors() method public to be explicitly called instead of relying on the SchemaValidatorsConfig.

Proper usage of the validateAndCollect method is confusing. It relies on a configuration set in SchemaValidatorsConfig that is configured on a per schema basis. It immediately runs loadCollectors if set to true and will never be able to run loadCollectors if set to false as the method is not public.

The documentation has been updated to reflect the replacement, which is to explicitly create the CollectorContext to be shared and set for each execution. Finally loadCollectors can be called a the end if needed.

// This creates a CollectorContext that can be used by multiple threads although this is not neccessary in this example
CollectorContext collectorContext = new CollectorContext(new ConcurrentHashMap<>(), new ConcurrentHashMap<>());
// This adds a custom collect keyword that sets values in the CollectorContext whenever it gets processed
JsonMetaSchema metaSchema = JsonMetaSchema.builder(JsonMetaSchema.getV202012()).keyword(new CollectKeyword()).build();
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, builder -> builder.metaSchema(metaSchema));
JsonSchema schema = factory.getSchema("{\n"
        + "  \"collect\": true\n"
        + "}");
for (int i = 0; i < 50; i++) {
    // The shared CollectorContext is set on the ExecutionContext for every run to aggregate data from all the runs
    schema.validate("1", InputFormat.JSON, executionContext -> {
        executionContext.setCollectorContext(collectorContext);
    });
}
// This is called for Collector implementations to aggregate data
collectorContext.loadCollectors();
AtomicInteger result = (AtomicInteger) collectorContext.get("collect");
assertEquals(50, result.get());
codecov-commenter commented 3 weeks ago

Codecov Report

Attention: Patch coverage is 50.00000% with 14 lines in your changes missing coverage. Please review.

Project coverage is 78.77%. Comparing base (48ca3c2) to head (8ed4f5a). Report is 29 commits behind head on master.

Files Patch % Lines
src/main/java/com/networknt/schema/JsonSchema.java 43.47% 13 Missing :warning:
...c/main/java/com/networknt/schema/OutputFormat.java 75.00% 1 Missing :warning:

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1063 +/- ## ============================================ - Coverage 78.90% 78.77% -0.13% - Complexity 1965 2046 +81 ============================================ Files 172 196 +24 Lines 6352 6602 +250 Branches 1255 1248 -7 ============================================ + Hits 5012 5201 +189 - Misses 867 928 +61 Partials 473 473 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.