From f973e6f6936aa0dd16478196bbc914f79279ba36 Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti <65240126+fjtirado@users.noreply.github.com> Date: Thu, 6 Jul 2023 18:12:34 +0200 Subject: [PATCH] [KOGITO-9501] Changing Everit to json-schema-validator (#3107) * [KOGITO-9501] Changing JsonSchema validator library * [KOGITO-9501] Different validation message * [KOGITO-9501] Changing to networknt library --- kogito-build/kogito-dependencies-bom/pom.xml | 26 +++++----- .../pom.xml | 4 -- .../pom.xml | 8 ++-- .../workflow/actions/JsonSchemaValidator.java | 47 +++++++++---------- .../schema/OpenApiModelSchemaGenerator.java | 2 +- .../quarkus/workflows/ExpressionRestIT.java | 2 +- .../pom.xml | 6 --- 7 files changed, 39 insertions(+), 56 deletions(-) diff --git a/kogito-build/kogito-dependencies-bom/pom.xml b/kogito-build/kogito-dependencies-bom/pom.xml index 82b1ec4ead3..4d0c8e82278 100644 --- a/kogito-build/kogito-dependencies-bom/pom.xml +++ b/kogito-build/kogito-dependencies-bom/pom.xml @@ -22,8 +22,9 @@ 3.2.3 - 2.13.4 - 2.13.4.2 + 1.0.86 + 2.14.2 + 2.14.2 2.8.0 2.4.10 1.0.0-preview.20220705 @@ -34,9 +35,7 @@ 0.0.8 1.5.2 3.24.2 - 2.2.14 - 2.13.4 - 1.14.2 + 2.14.2 4.18.0 2.33.2 3.19.6 @@ -704,22 +703,21 @@ ${version.com.github.victools} - com.github.java-json-tools + com.networknt json-schema-validator - ${version.com.github.java-json-tools} - test + ${version.com.networknt} + + + org.apache.commons + commons-lang3 + + com.fasterxml.jackson.datatype jackson-datatype-json-org ${version.com.fasterxml.jackson.datatype} - - com.github.erosb - everit-json-schema - ${version.com.github.erosb} - - io.serverlessworkflow diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-builder/pom.xml b/kogito-serverless-workflow/kogito-serverless-workflow-builder/pom.xml index d136e351f72..249be21f3c6 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-builder/pom.xml +++ b/kogito-serverless-workflow/kogito-serverless-workflow-builder/pom.xml @@ -22,10 +22,6 @@ org.kie.kogito kogito-serverless-workflow-runtime - - com.github.erosb - everit-json-schema - org.kie.kogito kogito-codegen-api diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/pom.xml b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/pom.xml index 66d2730012a..0543a5e2eac 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/pom.xml +++ b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/pom.xml @@ -26,10 +26,10 @@ org.kie.kogito kogito-rest-utils - - com.github.erosb - everit-json-schema - + + com.networknt + json-schema-validator + com.fasterxml.jackson.datatype jackson-datatype-json-org diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/JsonSchemaValidator.java b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/JsonSchemaValidator.java index aee96420316..080580c2033 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/JsonSchemaValidator.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/JsonSchemaValidator.java @@ -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; @@ -43,8 +43,7 @@ public class JsonSchemaValidator implements WorkflowModelValidator { protected final String schemaRef; protected final boolean failOnValidationErrors; - - private final AtomicReference schemaObject = new AtomicReference<>(); + private final AtomicReference schemaObject = new AtomicReference<>(); public JsonSchemaValidator(String schema, boolean failOnValidationErrors) { this.schemaRef = schema; @@ -54,32 +53,28 @@ public JsonSchemaValidator(String schema, boolean failOnValidationErrors) { @Override public void validate(Map 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 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); - } - } } diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/OpenApiModelSchemaGenerator.java b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/OpenApiModelSchemaGenerator.java index 2fd573c56f4..4759f8b5436 100644 --- a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/OpenApiModelSchemaGenerator.java +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/OpenApiModelSchemaGenerator.java @@ -115,7 +115,7 @@ private static Optional getSchema(Optional 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); } diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/ExpressionRestIT.java b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/ExpressionRestIT.java index efcf877f3c9..f05c7a4d7b0 100644 --- a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/ExpressionRestIT.java +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/test/java/org/kie/kogito/quarkus/workflows/ExpressionRestIT.java @@ -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()); } } diff --git a/quarkus/integration-tests/integration-tests-quarkus-processes/pom.xml b/quarkus/integration-tests/integration-tests-quarkus-processes/pom.xml index 8998f0558b6..1192bb4c6a8 100644 --- a/quarkus/integration-tests/integration-tests-quarkus-processes/pom.xml +++ b/quarkus/integration-tests/integration-tests-quarkus-processes/pom.xml @@ -83,12 +83,6 @@ rest-assured test - - - com.github.java-json-tools - json-schema-validator - test - io.rest-assured json-schema-validator