Search in sources :

Example 1 with SwaggerError

use of com.reprezen.swagedit.core.validation.SwaggerError in project KaiZen-OpenAPI-Editor by RepreZen.

the class OpenApi3Validator method validate.

@Override
public Set<SwaggerError> validate(JsonDocument document, URI baseURI) {
    final Set<SwaggerError> errors = super.validate(document, baseURI);
    final long nbOfErrors = errors.stream().filter(e -> e.getLevel() == SEVERITY_ERROR).count();
    // and option in UI is enable.
    if (isAdvancedValidation() && nbOfErrors == 0) {
        try {
            OpenApi3 result = new OpenApi3Parser().parse(document.get(), baseURI.toURL(), true);
            for (ValidationItem item : result.getValidationResults().getItems()) {
                PositionInfo pos = item.getPositionInfo();
                int line = pos != null ? pos.getLine() : 1;
                errors.add(new SwaggerError(line, getSeverity(item.getSeverity()), item.getMsg()));
            }
        } catch (Exception e) {
            Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, e.getLocalizedMessage()));
        }
    }
    return errors;
}
Also used : Arrays(java.util.Arrays) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) JsonDocument(com.reprezen.swagedit.core.editor.JsonDocument) OpenApi3(com.reprezen.kaizen.oasparser.model3.OpenApi3) Model(com.reprezen.swagedit.core.model.Model) OpenApi3Schema(com.reprezen.swagedit.openapi3.schema.OpenApi3Schema) ValidationItem(com.reprezen.kaizen.oasparser.val.ValidationResults.ValidationItem) ArrayList(java.util.ArrayList) ArrayNode(com.reprezen.swagedit.core.model.ArrayNode) JsonPointer(com.fasterxml.jackson.core.JsonPointer) PositionInfo(com.reprezen.jsonoverlay.PositionInfo) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) IMarker(org.eclipse.core.resources.IMarker) OpenApi3Parser(com.reprezen.kaizen.oasparser.OpenApi3Parser) Iterator(java.util.Iterator) JsonReferenceValidator(com.reprezen.swagedit.core.json.references.JsonReferenceValidator) Severity(com.reprezen.kaizen.oasparser.val.ValidationResults.Severity) Activator(com.reprezen.swagedit.openapi3.Activator) Set(java.util.Set) Status(org.eclipse.core.runtime.Status) AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) Objects(java.util.Objects) List(java.util.List) Messages(com.reprezen.swagedit.core.validation.Messages) Validator(com.reprezen.swagedit.core.validation.Validator) ADVANCED_VALIDATION(com.reprezen.swagedit.openapi3.preferences.OpenApi3PreferenceConstants.ADVANCED_VALIDATION) SwaggerError(com.reprezen.swagedit.core.validation.SwaggerError) JsonSchemaValidator(com.reprezen.swagedit.core.validation.JsonSchemaValidator) SEVERITY_ERROR(org.eclipse.core.resources.IMarker.SEVERITY_ERROR) Status(org.eclipse.core.runtime.Status) SwaggerError(com.reprezen.swagedit.core.validation.SwaggerError) PositionInfo(com.reprezen.jsonoverlay.PositionInfo) ValidationItem(com.reprezen.kaizen.oasparser.val.ValidationResults.ValidationItem) OpenApi3Parser(com.reprezen.kaizen.oasparser.OpenApi3Parser) OpenApi3(com.reprezen.kaizen.oasparser.model3.OpenApi3)

Example 2 with SwaggerError

use of com.reprezen.swagedit.core.validation.SwaggerError in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonReferenceValidator method validateType.

/**
 * This method checks that referenced objects are of expected type as defined in the schema.
 *
 * @param doc
 *            current document
 * @param baseURI
 *            document base URI
 * @param reference
 *            actual reference
 * @param sources
 *            list of node being source of the reference
 * @param schemas
 *            cache of JSON schemas
 * @param errors
 *            current set of errors
 */
protected Set<SwaggerError> validateType(JsonDocument doc, URI baseURI, JsonReference reference, Collection<AbstractNode> sources) {
    Set<SwaggerError> errors = new HashSet<>();
    JsonNode target = findTarget(doc, baseURI, reference);
    // To avoid performing even more cycles, the sources are grouped by their type.
    // Validation is done only once for each sources having same type.
    Map<String, List<AbstractNode>> sourceTypes = groupSourcesByType(sources);
    for (String type : sourceTypes.keySet()) {
        Set<JsonNode> report = schemaValidator.validate(target, type);
        if (!report.isEmpty()) {
            errors.addAll(createReferenceError(SEVERITY_WARNING, error_invalid_reference_type, sources));
        }
    }
    return errors;
}
Also used : SwaggerError(com.reprezen.swagedit.core.validation.SwaggerError) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 3 with SwaggerError

use of com.reprezen.swagedit.core.validation.SwaggerError in project KaiZen-OpenAPI-Editor by RepreZen.

the class OpenApi3ReferenceValidator method validateType.

@Override
protected Set<SwaggerError> validateType(JsonDocument doc, URI baseURI, JsonReference reference, Collection<AbstractNode> sources) {
    Set<SwaggerError> errors = new HashSet<>();
    Map<String, List<AbstractNode>> sourceTypes = groupSourcesByType(sources);
    JsonNode target = findTarget(doc, baseURI, reference);
    for (String type : sourceTypes.keySet()) {
        boolean isOperationValidation = linkTypePointer.equals(type);
        String ptr = isOperationValidation ? operationTypePointer.toString() : type;
        String message = isOperationValidation ? error_invalid_operation_ref : error_invalid_reference_type;
        Set<JsonNode> report = getSchemaValidator().validate(target, ptr);
        if (!report.isEmpty()) {
            errors.addAll(createReferenceError(SEVERITY_WARNING, message, sources));
        }
    }
    return errors;
}
Also used : SwaggerError(com.reprezen.swagedit.core.validation.SwaggerError) List(java.util.List) JsonNode(com.fasterxml.jackson.databind.JsonNode) HashSet(java.util.HashSet)

Example 4 with SwaggerError

use of com.reprezen.swagedit.core.validation.SwaggerError in project KaiZen-OpenAPI-Editor by RepreZen.

the class ErrorProcessorTest method testProcessNode_WithSingleError.

@Test
public void testProcessNode_WithSingleError() throws Exception {
    JsonNode fixture = mapper.readTree(Paths.get("resources", "error-1.json").toFile());
    Set<SwaggerError> errors = processor.processMessageNode(fixture);
    assertEquals(1, errors.size());
    assertTrue(getOnlyElement(errors) instanceof SwaggerError);
}
Also used : SwaggerError(com.reprezen.swagedit.core.validation.SwaggerError) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 5 with SwaggerError

use of com.reprezen.swagedit.core.validation.SwaggerError in project KaiZen-OpenAPI-Editor by RepreZen.

the class ErrorProcessorTest method testProcessNode_WithOneOfError.

@Test
public void testProcessNode_WithOneOfError() throws Exception {
    JsonNode fixture = mapper.readTree(Paths.get("resources", "error-2.json").toFile());
    Set<SwaggerError> errors = processor.processMessageNode(fixture);
    assertEquals(1, errors.size());
    assertTrue(getOnlyElement(errors) instanceof SwaggerError.MultipleSwaggerError);
}
Also used : SwaggerError(com.reprezen.swagedit.core.validation.SwaggerError) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Aggregations

SwaggerError (com.reprezen.swagedit.core.validation.SwaggerError)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 List (java.util.List)3 AbstractNode (com.reprezen.swagedit.core.model.AbstractNode)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 JsonPointer (com.fasterxml.jackson.core.JsonPointer)1 PositionInfo (com.reprezen.jsonoverlay.PositionInfo)1 OpenApi3Parser (com.reprezen.kaizen.oasparser.OpenApi3Parser)1 OpenApi3 (com.reprezen.kaizen.oasparser.model3.OpenApi3)1 Severity (com.reprezen.kaizen.oasparser.val.ValidationResults.Severity)1 ValidationItem (com.reprezen.kaizen.oasparser.val.ValidationResults.ValidationItem)1 JsonDocument (com.reprezen.swagedit.core.editor.JsonDocument)1 JsonReferenceValidator (com.reprezen.swagedit.core.json.references.JsonReferenceValidator)1 ArrayNode (com.reprezen.swagedit.core.model.ArrayNode)1 Location (com.reprezen.swagedit.core.model.Location)1 Model (com.reprezen.swagedit.core.model.Model)1 JsonSchemaValidator (com.reprezen.swagedit.core.validation.JsonSchemaValidator)1 Messages (com.reprezen.swagedit.core.validation.Messages)1