Search in sources :

Example 1 with ProcessingMessage

use of com.github.fge.jsonschema.core.report.ProcessingMessage in project swagger-parser by swagger-api.

the class SwaggerJsonValidator method fillMessages.

private static boolean fillMessages(final ProcessingReport report, final MessageBuilder builder) {
    final Severity severity = LEVEL_MAP.get(report.getLogLevel());
    final ArrayNode node = JacksonUtils.nodeFactory().arrayNode();
    for (final ProcessingMessage processingMessage : report) {
        node.add(processingMessage.asJson());
    }
    final String reportAsString = JacksonUtils.prettyPrint(node);
    final Message message = new Message("", reportAsString, severity);
    builder.append(message);
    return report.isSuccess();
}
Also used : ProcessingMessage(com.github.fge.jsonschema.core.report.ProcessingMessage) Message(io.swagger.report.Message) ProcessingMessage(com.github.fge.jsonschema.core.report.ProcessingMessage) Severity(io.swagger.report.Severity) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 2 with ProcessingMessage

use of com.github.fge.jsonschema.core.report.ProcessingMessage in project ocvn by devgateway.

the class ReleaseExportTest method testReleaseExportIsValid.

@Test
public void testReleaseExportIsValid() throws Exception {
    final ClassLoader classLoader = getClass().getClassLoader();
    final File file = new File(classLoader.getResource("json/award-release-test.json").getFile());
    final JsonImport releaseJsonImport = new ReleaseJsonImport(releaseRepository, file, false);
    final Release release = (Release) releaseJsonImport.importObject();
    final MvcResult result = this.mockMvc.perform(MockMvcRequestBuilders.get("/api/ocds/release/ocid/" + release.getOcid()).accept(MediaType.APPLICATION_JSON_UTF8)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)).andReturn();
    final String content = result.getResponse().getContentAsString();
    final JsonNode jsonNodeResponse = JsonLoader.fromString(content);
    final OcdsSchemaValidatorService.ProcessingReportWithNode processingReport = ocdsSchemaValidator.validate(jsonNodeResponse);
    if (!processingReport.getReport().isSuccess()) {
        for (ProcessingMessage processingMessage : processingReport.getReport()) {
            logger.error(">>> processingMessage: \n" + processingMessage);
        }
    }
    Assert.assertEquals("Is the release valid?", true, processingReport.getReport().isSuccess());
}
Also used : ReleaseJsonImport(org.devgateway.ocds.persistence.mongo.spring.json.ReleaseJsonImport) JsonImport(org.devgateway.ocds.persistence.mongo.spring.json.JsonImport) ProcessingMessage(com.github.fge.jsonschema.core.report.ProcessingMessage) ReleaseJsonImport(org.devgateway.ocds.persistence.mongo.spring.json.ReleaseJsonImport) JsonNode(com.fasterxml.jackson.databind.JsonNode) MvcResult(org.springframework.test.web.servlet.MvcResult) OcdsSchemaValidatorService(org.devgateway.ocds.persistence.mongo.spring.OcdsSchemaValidatorService) File(java.io.File) Release(org.devgateway.ocds.persistence.mongo.Release) Test(org.junit.Test) AbstractWebTest(org.devgateway.toolkit.web.AbstractWebTest)

Example 3 with ProcessingMessage

use of com.github.fge.jsonschema.core.report.ProcessingMessage in project arctic-sea by 52North.

the class JSONValidator method encode.

public String encode(ProcessingReport report, JsonNode instance) {
    ObjectNode objectNode = Json.nodeFactory().objectNode();
    objectNode.set(JSONConstants.INSTANCE, instance);
    ArrayNode errors = objectNode.putArray(JSONConstants.ERRORS);
    for (ProcessingMessage m : report) {
        errors.add(m.asJson());
    }
    return Json.print(objectNode);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ProcessingMessage(com.github.fge.jsonschema.core.report.ProcessingMessage) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 4 with ProcessingMessage

use of com.github.fge.jsonschema.core.report.ProcessingMessage 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 5 with ProcessingMessage

use of com.github.fge.jsonschema.core.report.ProcessingMessage 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

ProcessingMessage (com.github.fge.jsonschema.core.report.ProcessingMessage)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 File (java.io.File)6 Release (org.devgateway.ocds.persistence.mongo.Release)6 OcdsSchemaValidatorService (org.devgateway.ocds.persistence.mongo.spring.OcdsSchemaValidatorService)6 JsonImport (org.devgateway.ocds.persistence.mongo.spring.json.JsonImport)6 ReleaseJsonImport (org.devgateway.ocds.persistence.mongo.spring.json.ReleaseJsonImport)6 AbstractWebTest (org.devgateway.toolkit.web.AbstractWebTest)6 Test (org.junit.Test)6 MvcResult (org.springframework.test.web.servlet.MvcResult)6 ProcessingReport (com.github.fge.jsonschema.core.report.ProcessingReport)5 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)4 IOException (java.io.IOException)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 JsonSchema (com.github.fge.jsonschema.main.JsonSchema)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1