Skip to content

Commit

Permalink
[KOGITO-9501] Changing JsonSchema validator library
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Jul 6, 2023
1 parent f3ce5ef commit f792392
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 42 deletions.
8 changes: 0 additions & 8 deletions kogito-build/kogito-dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
<version.com.github.javaparser>3.24.2</version.com.github.javaparser>
<version.com.github.java-json-tools>2.2.14</version.com.github.java-json-tools>
<version.com.fasterxml.jackson.datatype>2.14.2</version.com.fasterxml.jackson.datatype>
<version.com.github.erosb>1.14.2</version.com.github.erosb>
<version.com.github.victools>4.18.0</version.com.github.victools>
<version.com.github.tomakehurst.wiremock>2.33.2</version.com.github.tomakehurst.wiremock>
<version.com.google.protobuf>3.22.0</version.com.google.protobuf>
Expand Down Expand Up @@ -707,19 +706,12 @@
<groupId>com.github.java-json-tools</groupId>
<artifactId>json-schema-validator</artifactId>
<version>${version.com.github.java-json-tools}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-json-org</artifactId>
<version>${version.com.fasterxml.jackson.datatype}</version>
</dependency>
<dependency>
<groupId>com.github.erosb</groupId>
<artifactId>everit-json-schema</artifactId>
<version>${version.com.github.erosb}</version>
</dependency>

<!-- Serverless Workflow -->
<dependency>
<groupId>io.serverlessworkflow</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-serverless-workflow-runtime</artifactId>
</dependency>
<dependency>
<groupId>com.github.erosb</groupId>
<artifactId>everit-json-schema</artifactId>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-codegen-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
<artifactId>kogito-rest-utils</artifactId>
</dependency>
<dependency>
<groupId>com.github.erosb</groupId>
<artifactId>everit-json-schema</artifactId>
<groupId>com.github.java-json-tools</groupId>
<artifactId>json-schema-validator</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import org.everit.json.schema.Schema;
import org.everit.json.schema.ValidationException;
import org.everit.json.schema.loader.SchemaClient;
import org.everit.json.schema.loader.SchemaLoader;
import org.jbpm.workflow.core.WorkflowModelValidator;
import org.json.JSONObject;
import org.kie.kogito.jackson.utils.ObjectMapperFactory;
import org.kie.kogito.serverless.workflow.SWFConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchemaFactory;

import static org.kie.kogito.serverless.workflow.io.URIContentLoaderFactory.readAllBytes;
import static org.kie.kogito.serverless.workflow.io.URIContentLoaderFactory.runtimeLoader;
Expand All @@ -43,8 +42,7 @@ public class JsonSchemaValidator implements WorkflowModelValidator {

protected final String schemaRef;
protected final boolean failOnValidationErrors;

private final AtomicReference<Schema> schemaObject = new AtomicReference<>();
private final AtomicReference<JsonNode> schemaObject = new AtomicReference<>();

public JsonSchemaValidator(String schema, boolean failOnValidationErrors) {
this.schemaRef = schema;
Expand All @@ -54,32 +52,25 @@ public JsonSchemaValidator(String schema, boolean failOnValidationErrors) {
@Override
public void validate(Map<String, Object> model) {
try {
load().validate(ObjectMapperFactory.get().convertValue(model.getOrDefault(SWFConstants.DEFAULT_WORKFLOW_VAR, NullNode.instance), JSONObject.class));
} catch (ValidationException ex) {
handleException(ex, ex.getCausingExceptions().isEmpty() ? ex : ex.getCausingExceptions());
} catch (IOException ex) {
handleException(ex, ex);
ProcessingReport report = JsonSchemaFactory.byDefault().getJsonSchema(schemaData()).validate((JsonNode) model.getOrDefault(SWFConstants.DEFAULT_WORKFLOW_VAR, NullNode.instance));
if (!report.isSuccess()) {
final String validationMessage = String.format("Validation errors %s", report.toString());
logger.warn(validationMessage);
if (failOnValidationErrors) {
throw new IllegalArgumentException(validationMessage);
}
}
} catch (ProcessingException | IOException ex) {
throw new IllegalStateException("Unexpected error validating schema", ex);
}
}

public Schema load() throws IOException {
Schema result = schemaObject.get();
public JsonNode schemaData() throws IOException {
JsonNode result = schemaObject.get();
if (result == null) {
result = SchemaLoader.builder()
.schemaJson(ObjectMapperFactory.get().readValue(readAllBytes(runtimeLoader(schemaRef)), JSONObject.class))
.resolutionScope(schemaRef)
.schemaClient(SchemaClient.classPathAwareClient())
.build().load().build();
result = ObjectMapperFactory.get().readTree(readAllBytes(runtimeLoader(schemaRef)));
schemaObject.set(result);
}
return result;
}

private void handleException(Throwable ex, Object toAppend) {
String validationError = String.format("Error validating schema: %s", toAppend);
logger.warn(validationError, ex);
if (failOnValidationErrors) {
throw new IllegalArgumentException(validationError);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private static Optional<Schema> getSchema(Optional<WorkflowModelValidator> valid

private static Schema getSchema(JsonSchemaValidator validator) {
try {
return ObjectMapperFactory.get().readValue(validator.load().toString(), JsonSchemaImpl.class);
return ObjectMapperFactory.get().convertValue(validator.schemaData(), JsonSchemaImpl.class);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down

0 comments on commit f792392

Please sign in to comment.