<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 com.reprezen.swagedit.core.validation.Messages.error_invalid_operation_ref;
import static org.eclipse.core.resources.IMarker.SEVERITY_WARNING;

import java.net.URI;
import java.util.Objects;
import java.util.Set;

import com.fasterxml.jackson.core.JsonPointer;
import com.reprezen.swagedit.core.editor.JsonDocument;
import com.reprezen.swagedit.core.json.references.JsonReference;
import com.reprezen.swagedit.core.json.references.JsonReferenceFactory;
import com.reprezen.swagedit.core.json.references.JsonReferenceValidator;
import com.reprezen.swagedit.core.model.AbstractNode;
import com.reprezen.swagedit.core.model.ValueNode;
import com.reprezen.swagedit.core.validation.SwaggerError;

public class OpenApi3ReferenceValidator extends JsonReferenceValidator {

    private final JsonPointer schemaPointer = JsonPointer.compile("/definitions/linkOrReference");
    private final JsonPointer operationPointer = JsonPointer.compile("/definitions/operation");

    public OpenApi3ReferenceValidator() {
        super(new OpenApi3ReferenceFactory());
    }

    OpenApi3ReferenceValidator(OpenApi3ReferenceFactory factory) {
        super(factory);
    }

    @Override
    protected void validateType(JsonDocument doc, URI baseURI, AbstractNode node, JsonReference reference,
            Set<SwaggerError> errors) {

        if (schemaPointer.equals(node.getType().getPointer())) {
            AbstractNode target = findTarget(doc, baseURI, reference);
            boolean isValidType = target != null && target.getType() != null
                    && Objects.equals(operationPointer, target.getType().getPointer());

            if (!isValidType) {
                errors.add(createReferenceError(SEVERITY_WARNING, error_invalid_operation_ref, reference));
            }
        } else {
            super.validateType(doc, baseURI, node, reference, errors);
        }
    }

    public static class OpenApi3ReferenceFactory extends JsonReferenceFactory {

        private static final String OPERATION_REF = "operationRef";

        @Override
        protected Boolean isReference(AbstractNode node) {
            return super.isReference(node) || node.get(OPERATION_REF) != null;
        }

        @Override
        protected ValueNode getReferenceValue(AbstractNode node) {
            ValueNode valueNode = super.getReferenceValue(node);
            if (valueNode == null) {<commit_msg>[#306] OpenAPI v3: Validation for Link#operationRef values<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 com.reprezen.swagedit.core.validation.Messages.error_invalid_operation_ref;
import static org.eclipse.core.resources.IMarker.SEVERITY_WARNING;

import java.net.URI;
import java.util.Objects;
import java.util.Set;

import com.fasterxml.jackson.core.JsonPointer;
import com.reprezen.swagedit.core.editor.JsonDocument;
import com.reprezen.swagedit.core.json.references.JsonReference;
import com.reprezen.swagedit.core.json.references.JsonReferenceFactory;
import com.reprezen.swagedit.core.json.references.JsonReferenceValidator;
import com.reprezen.swagedit.core.model.AbstractNode;
import com.reprezen.swagedit.core.model.ValueNode;
import com.reprezen.swagedit.core.schema.TypeDefinition;
import com.reprezen.swagedit.core.validation.SwaggerError;

public class OpenApi3ReferenceValidator extends JsonReferenceValidator {

    private final JsonPointer linkTypePointer = JsonPointer.compile("/definitions/linkOrReference");
    private final JsonPointer operationTypePointer = JsonPointer.compile("/definitions/operation");

    public OpenApi3ReferenceValidator() {
        super(new OpenApi3ReferenceFactory());
    }

    OpenApi3ReferenceValidator(OpenApi3ReferenceFactory factory) {
        super(factory);
    }

    @Override
    protected void validateType(JsonDocument doc, URI baseURI, AbstractNode node, JsonReference reference,
            Set<SwaggerError> errors) {

        if (linkTypePointer.equals(node.getType().getPointer())) {
            AbstractNode target = findTarget(doc, baseURI, reference);
            boolean isValidType = isValidOperation(target);

            if (!isValidType) {
                errors.add(createReferenceError(SEVERITY_WARNING, error_invalid_operation_ref, reference));
            }
        } else {
            super.validateType(doc, baseURI, node, reference, errors);
        }
    }

    protected boolean isValidOperation(AbstractNode operation) {
        TypeDefinition type = operation != null ? operation.getType() : null;

        return type != null && Objects.equals(operationTypePointer, type.getPointer());
    }

    public static class OpenApi3ReferenceFactory extends JsonReferenceFactory {

        private static final String OPERATION_REF = "operationRef";

        @Override
        protected Boolean isReference(AbstractNode node) {
            return super.isReference(node) || node.get(OPERATION_REF) != null;
        }

        @Override
        protected ValueNode getReferenceValue(AbstractNode node) {
            ValueNode valueNode = super.getReferenceValue(node);
            if (valueNode == null) {
{'fraction_non_alphanumeric': 0.13433544303797468, 'fraction_numerical': 0.005379746835443038, 'mean_word_length': 4.068965517241379, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '5634162', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1587, 'n_words': 360}