Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.40.x-prod] [KOGITO-9501] Changing Everit to json-schema-validator #3110

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions kogito-build/kogito-dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
<version.org.apache.kafka>3.2.3</version.org.apache.kafka>

<!-- dependencies versions -->
<version.com.fasterxml.jackson>2.13.4</version.com.fasterxml.jackson>
<version.com.fasterxml.jackson.databind>2.13.4.2</version.com.fasterxml.jackson.databind>
<version.com.networknt>1.0.86</version.com.networknt>
<version.com.fasterxml.jackson>2.14.2</version.com.fasterxml.jackson>
<version.com.fasterxml.jackson.databind>2.14.2</version.com.fasterxml.jackson.databind>
<version.com.jayway.jsonpath>2.8.0</version.com.jayway.jsonpath>
<version.net.minidev.jsonsmart>2.4.10</version.net.minidev.jsonsmart>
<version.net.thisptr.jackson-jq>1.0.0-preview.20220705</version.net.thisptr.jackson-jq>
Expand All @@ -34,9 +35,7 @@
<version.io.quarkiverse.embedded.postgresql>0.0.8</version.io.quarkiverse.embedded.postgresql>
<version.com.github.haifengl.smile>1.5.2</version.com.github.haifengl.smile>
<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.13.4</version.com.fasterxml.jackson.datatype>
<version.com.github.erosb>1.14.2</version.com.github.erosb>
<version.com.fasterxml.jackson.datatype>2.14.2</version.com.fasterxml.jackson.datatype>
<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.19.6</version.com.google.protobuf>
Expand Down Expand Up @@ -704,22 +703,21 @@
<version>${version.com.github.victools}</version>
</dependency>
<dependency>
<groupId>com.github.java-json-tools</groupId>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>${version.com.github.java-json-tools}</version>
<scope>test</scope>
<version>${version.com.networknt}</version>
<exclusions>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</exclusion>
</exclusions>
</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 @@ -26,10 +26,10 @@
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-rest-utils</artifactId>
</dependency>
<dependency>
<groupId>com.github.erosb</groupId>
<artifactId>everit-json-schema</artifactId>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-json-org</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@

import java.io.IOException;
import java.util.Map;
import java.util.Set;
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.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion.VersionFlag;
import com.networknt.schema.ValidationMessage;

import static org.kie.kogito.serverless.workflow.io.URIContentLoaderFactory.readAllBytes;
import static org.kie.kogito.serverless.workflow.io.URIContentLoaderFactory.runtimeLoader;
Expand All @@ -43,8 +43,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 +53,28 @@ 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());
Set<ValidationMessage> report =
JsonSchemaFactory.getInstance(VersionFlag.V4).getSchema(schemaData()).validate((JsonNode) model.getOrDefault(SWFConstants.DEFAULT_WORKFLOW_VAR, NullNode.instance));
if (!report.isEmpty()) {
StringBuilder sb = new StringBuilder("There are JsonSchema validation errors:");
report.forEach(m -> sb.append(System.lineSeparator()).append(m.getMessage()));
final String validationMessage = sb.toString();
logger.warn(validationMessage);
if (failOnValidationErrors) {
throw new IllegalArgumentException(validationMessage);
}
}
} catch (IOException ex) {
handleException(ex, 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
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void testExpressionOutputValidation() {
.post("/invalidOutputExpression")
.then()
.statusCode(is(400))
.body("message", containsString("required key [message] not found"))
.body("message", containsString("message"))
.body("id", notNullValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.java-json-tools</groupId>
<artifactId>json-schema-validator</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
Expand Down