<commit_before>/*******************************************************************************
 *  Copyright (c) 2016 ModelSolv, Inc. and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *  
 *  Contributors:
 *     ModelSolv, Inc. - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.reprezen.swagedit.openapi3.validation;

import static org.eclipse.core.resources.IMarker.SEVERITY_ERROR;

import java.net.URI;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.Status;

import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Lists;
import com.reprezen.jsonoverlay.PositionInfo;
import com.reprezen.kaizen.oasparser.OpenApi3Parser;
import com.reprezen.kaizen.oasparser.model3.OpenApi3;
import com.reprezen.kaizen.oasparser.val.ValidationResults.Severity;
import com.reprezen.kaizen.oasparser.val.ValidationResults.ValidationItem;
import com.reprezen.swagedit.core.editor.JsonDocument;
import com.reprezen.swagedit.core.json.references.JsonReferenceValidator;
import com.reprezen.swagedit.core.model.AbstractNode;
import com.reprezen.swagedit.core.model.ArrayNode;
import com.reprezen.swagedit.core.model.Model;
import com.reprezen.swagedit.core.validation.JsonSchemaValidator;
import com.reprezen.swagedit.core.validation.Messages;
import com.reprezen.swagedit.core.validation.SwaggerError;
import com.reprezen.swagedit.core.validation.Validator;
import com.reprezen.swagedit.openapi3.Activator;
import com.reprezen.swagedit.openapi3.schema.OpenApi3Schema;

public class OpenApi3Validator extends Validator {

    public static class OpenApi3SchemaValidator extends JsonSchemaValidator {

        public OpenApi3SchemaValidator(JsonNode schema, Map<String, JsonNode> preloadSchemas) {
            super(schema, preloadSchemas);
        }
    }

    private final JsonPointer operationPointer = JsonPointer.compile("/definitions/operation");
    private final JsonPointer securityPointer = JsonPointer.compile("/components/securitySchemes");

    private final JsonNode schema;
    private final Map<String, JsonNode> preloadedSchemas;
    private boolean advancedValidation = false;

    private JsonSchemaValidator schemaValidator;
    private JsonReferenceValidator referenceValidator;

    public OpenApi3Validator(Map<String, JsonNode> preloadedSchemas) {
        this.preloadedSchemas = preloadedSchemas;
        this.schema = preloadedSchemas.get(OpenApi3Schema.URL);
    }

    public void setAdvancedValidation(boolean advancedValidation) {
        this.advancedValidation = advancedValidation;
    }

    @Override
    public JsonReferenceValidator getReferenceValidator() {
        if (referenceValidator == null) {
            referenceValidator = new OpenApi3ReferenceValidator(getSchemaValidator());
        }
        return referenceValidator;
    }

    @Override
    public JsonSchemaValidator getSchemaValidator() {
        if (schemaValidator == null) {
            schemaValidator = new OpenApi3SchemaValidator(schema, preloadedSchemas);
        }
        return schemaValidator;
    }

    @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();

        // Advanced validation is enable if no errors are detected (does not include warnings and infos),
        // and option in UI is enable.
        if (advancedValidation && nbOfErrors == 0) {
            try {
                OpenApi3 result = new OpenApi3Parser().parse(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;
    }
<commit_msg>ZEN-4359 - Advance validation for OAS3 refreshes only on model save<commit_after>/*******************************************************************************
 *  Copyright (c) 2016 ModelSolv, Inc. and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *  
 *  Contributors:
 *     ModelSolv, Inc. - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.reprezen.swagedit.openapi3.validation;

import static org.eclipse.core.resources.IMarker.SEVERITY_ERROR;

import java.net.URI;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.Status;

import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Lists;
import com.reprezen.jsonoverlay.PositionInfo;
import com.reprezen.kaizen.oasparser.OpenApi3Parser;
import com.reprezen.kaizen.oasparser.model3.OpenApi3;
import com.reprezen.kaizen.oasparser.val.ValidationResults.Severity;
import com.reprezen.kaizen.oasparser.val.ValidationResults.ValidationItem;
import com.reprezen.swagedit.core.editor.JsonDocument;
import com.reprezen.swagedit.core.json.references.JsonReferenceValidator;
import com.reprezen.swagedit.core.model.AbstractNode;
import com.reprezen.swagedit.core.model.ArrayNode;
import com.reprezen.swagedit.core.model.Model;
import com.reprezen.swagedit.core.validation.JsonSchemaValidator;
import com.reprezen.swagedit.core.validation.Messages;
import com.reprezen.swagedit.core.validation.SwaggerError;
import com.reprezen.swagedit.core.validation.Validator;
import com.reprezen.swagedit.openapi3.Activator;
import com.reprezen.swagedit.openapi3.schema.OpenApi3Schema;

public class OpenApi3Validator extends Validator {

    public static class OpenApi3SchemaValidator extends JsonSchemaValidator {

        public OpenApi3SchemaValidator(JsonNode schema, Map<String, JsonNode> preloadSchemas) {
            super(schema, preloadSchemas);
        }
    }

    private final JsonPointer operationPointer = JsonPointer.compile("/definitions/operation");
    private final JsonPointer securityPointer = JsonPointer.compile("/components/securitySchemes");

    private final JsonNode schema;
    private final Map<String, JsonNode> preloadedSchemas;
    private boolean advancedValidation = false;

    private JsonSchemaValidator schemaValidator;
    private JsonReferenceValidator referenceValidator;

    public OpenApi3Validator(Map<String, JsonNode> preloadedSchemas) {
        this.preloadedSchemas = preloadedSchemas;
        this.schema = preloadedSchemas.get(OpenApi3Schema.URL);
    }

    public void setAdvancedValidation(boolean advancedValidation) {
        this.advancedValidation = advancedValidation;
    }

    @Override
    public JsonReferenceValidator getReferenceValidator() {
        if (referenceValidator == null) {
            referenceValidator = new OpenApi3ReferenceValidator(getSchemaValidator());
        }
        return referenceValidator;
    }

    @Override
    public JsonSchemaValidator getSchemaValidator() {
        if (schemaValidator == null) {
            schemaValidator = new OpenApi3SchemaValidator(schema, preloadedSchemas);
        }
        return schemaValidator;
    }

    @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();

        // Advanced validation is enable if no errors are detected (does not include warnings and infos),
        // and option in UI is enable.
        if (advancedValidation && 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;
    }

{'fraction_non_alphanumeric': 0.1212761462074794, 'fraction_numerical': 0.006021550813437566, 'mean_word_length': 3.7076081551466933, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5941317', 'n_tokens_mistral': 2605, 'n_tokens_neox': 2501, 'n_words': 550}