Search in sources :

Example 6 with ProcessingReport

use of com.github.fge.jsonschema.core.report.ProcessingReport in project syndesis by syndesisio.

the class SwaggerHelper method validateJSonSchema.

private static SwaggerModelInfo validateJSonSchema(final String specification, final Swagger model) {
    try {
        final JsonNode specRoot = convertToJson(specification);
        final ProcessingReport report = SWAGGER_2_0_SCHEMA.validate(specRoot);
        final List<Violation> errors = new ArrayList<>();
        final List<Violation> warnings = new ArrayList<>();
        for (final ProcessingMessage message : report) {
            final boolean added = append(errors, message, Optional.of("error"));
            if (!added) {
                append(warnings, message, Optional.empty());
            }
        }
        return new SwaggerModelInfo.Builder().errors(errors).warnings(warnings).model(model).resolvedSpecification(specification).build();
    } catch (IOException | ProcessingException ex) {
        LOG.error("Unable to load the schema file embedded in the artifact", ex);
        return new SwaggerModelInfo.Builder().addError(new Violation.Builder().error("error").property("").message("Unable to load the swagger schema file embedded in the artifact").build()).build();
    }
}
Also used : Violation(io.syndesis.common.model.Violation) ProcessingMessage(com.github.fge.jsonschema.core.report.ProcessingMessage) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) SwaggerModelInfo(io.syndesis.server.connector.generator.swagger.SwaggerModelInfo) IOException(java.io.IOException) ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 7 with ProcessingReport

use of com.github.fge.jsonschema.core.report.ProcessingReport in project syndesis by syndesisio.

the class ExtensionSchemaValidationTest method addSchemaVersionInPublicModelExtensionTest.

@Test
public void addSchemaVersionInPublicModelExtensionTest() throws ProcessingException, IOException {
    String syndesisExtensionSchema = "/syndesis/syndesis-extension-definition-schema.json";
    JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema("resource:" + syndesisExtensionSchema);
    ExtensionConverter converter = new DefaultExtensionConverter();
    ObjectNode tree = OBJECT_MAPPER.createObjectNode().put("extensionId", "my-extension").put("name", "Name").put("description", "Description").put("version", "1.0.0");
    ProcessingReport report = schema.validate(tree);
    assertFalse(report.toString(), report.iterator().hasNext());
    Extension extension = converter.toInternalExtension(tree);
    assertEquals(ExtensionConverter.getCurrentSchemaVersion(), extension.getSchemaVersion());
}
Also used : Extension(io.syndesis.common.model.extension.Extension) ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonSchema(com.github.fge.jsonschema.main.JsonSchema) Test(org.junit.Test)

Example 8 with ProcessingReport

use of com.github.fge.jsonschema.core.report.ProcessingReport in project syndesis by syndesisio.

the class ExtensionSchemaValidationTest method upgradePublicModelExtensionTest.

@Test
public void upgradePublicModelExtensionTest() throws ProcessingException, IOException {
    String syndesisExtensionSchema = "/syndesis/syndesis-extension-definition-schema.json";
    JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema("resource:" + syndesisExtensionSchema);
    ExtensionConverter converter = new DefaultExtensionConverter();
    Extension extension = new Extension.Builder().extensionId("my-extension").name("Name").description("Description").version("1.0.0").schemaVersion("old-V0.1").extensionType(Extension.Type.Steps).build();
    JsonNode tree = converter.toPublicExtension(extension);
    ProcessingReport report = schema.validate(tree);
    assertFalse(report.toString(), report.iterator().hasNext());
    Extension extensionClone = converter.toInternalExtension(tree);
    assertNotEquals(extensionClone, extension);
    assertEquals(ExtensionConverter.getCurrentSchemaVersion(), extensionClone.getSchemaVersion());
}
Also used : Extension(io.syndesis.common.model.extension.Extension) ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JsonSchema(com.github.fge.jsonschema.main.JsonSchema) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 9 with ProcessingReport

use of com.github.fge.jsonschema.core.report.ProcessingReport in project bender by Nextdoor.

the class BenderConfig method validate.

public static boolean validate(String data, ObjectMapper objectMapper, BenderSchema benderSchema) throws ConfigurationException {
    ProcessingReport report;
    try {
        /*
       * Create object
       */
        JsonNode node = objectMapper.readTree(data);
        /*
       * Create JSON schema
       */
        JsonNode jsonSchema = benderSchema.getSchema();
        /*
       * Validate
       */
        final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
        final JsonSchema schema = factory.getJsonSchema(jsonSchema);
        report = schema.validate(node);
    } catch (IOException | ProcessingException ioe) {
        throw new ConfigurationException("unable to validate config", ioe);
    }
    if (report.isSuccess()) {
        return true;
    } else {
        throw new ConfigurationException("invalid config file", report.iterator().next().asException());
    }
}
Also used : ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JsonSchema(com.github.fge.jsonschema.main.JsonSchema) JsonSchemaFactory(com.github.fge.jsonschema.main.JsonSchemaFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException)

Example 10 with ProcessingReport

use of com.github.fge.jsonschema.core.report.ProcessingReport in project service-proxy by membrane.

the class JSONValidator method validateMessage.

public Outcome validateMessage(Exchange exc, InputStream body, Charset charset, String source) throws Exception {
    List<String> errors;
    boolean success = true;
    try {
        JsonNode node = JsonLoader.fromReader(new InputStreamReader(body, charset));
        ProcessingReport report = schema.validateUnchecked(node);
        success = report.isSuccess();
        errors = new ArrayList<String>();
        for (ProcessingMessage message : report) errors.add(message.getMessage());
    } catch (JsonParseException e) {
        success = false;
        errors = new ArrayList<String>();
        errors.add(e.getMessage());
    }
    if (success) {
        valid.incrementAndGet();
        return Outcome.CONTINUE;
    }
    if (failureHandler == FailureHandler.VOID) {
        StringBuilder message = new StringBuilder();
        message.append(source);
        message.append(": ");
        for (String error : errors) {
            message.append(error);
            message.append(";");
        }
        exc.setProperty("error", message.toString());
        invalid.incrementAndGet();
        return Outcome.ABORT;
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JsonGenerator jg = new JsonFactory().createGenerator(baos);
    jg.writeStartObject();
    jg.writeStringField("source", source);
    jg.writeArrayFieldStart("errors");
    for (String message : errors) jg.writeString(message);
    jg.close();
    if (failureHandler != null) {
        failureHandler.handleFailure(new String(baos.toByteArray(), UTF8), exc);
        exc.setResponse(Response.badRequest().contentType("application/json;charset=utf-8").body("{\"error\":\"error\"}".getBytes(UTF8)).build());
    } else {
        exc.setResponse(Response.badRequest().contentType("application/json;charset=utf-8").body(baos.toByteArray()).build());
    }
    invalid.incrementAndGet();
    return Outcome.ABORT;
}
Also used : InputStreamReader(java.io.InputStreamReader) ProcessingMessage(com.github.fge.jsonschema.core.report.ProcessingMessage) ArrayList(java.util.ArrayList) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Aggregations

ProcessingReport (com.github.fge.jsonschema.core.report.ProcessingReport)24 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)11 JsonSchema (com.github.fge.jsonschema.main.JsonSchema)8 JsonSchemaFactory (com.github.fge.jsonschema.main.JsonSchemaFactory)7 IOException (java.io.IOException)7 ProcessingMessage (com.github.fge.jsonschema.core.report.ProcessingMessage)5 JsonValidator (com.github.fge.jsonschema.main.JsonValidator)4 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 Extension (io.syndesis.common.model.extension.Extension)3 Test (org.junit.Test)3 TestExecReport (de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.reports.TestExecReport)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 ConnectionClosedException (org.apache.http.ConnectionClosedException)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Violation (io.syndesis.common.model.Violation)1