Search in sources :

Example 21 with AbstractNode

use of com.reprezen.swagedit.core.model.AbstractNode in project KaiZen-OpenAPI-Editor by RepreZen.

the class OpenApi3ReferenceValidator method validateType.

@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);
    }
}
Also used : AbstractNode(com.reprezen.swagedit.core.model.AbstractNode)

Example 22 with AbstractNode

use of com.reprezen.swagedit.core.model.AbstractNode in project KaiZen-OpenAPI-Editor by RepreZen.

the class OpenApi3Validator method validateSecuritySchemeScopes.

private void validateSecuritySchemeScopes(AbstractNode node, String name, AbstractNode securityScheme, Set<SwaggerError> errors) {
    String type = getType(securityScheme);
    if (type == null) {
        return;
    }
    boolean shouldHaveScopes = oauthScopes.contains(type);
    List<String> scopes = getSecurityScopes(securityScheme);
    AbstractNode values = node.get(name);
    if (values.isArray()) {
        ArrayNode scopeValues = values.asArray();
        if (scopeValues.size() > 0 && !shouldHaveScopes) {
            String message = String.format(Messages.error_scope_should_be_empty, name, type, name);
            errors.add(error(node.get(name), IMarker.SEVERITY_ERROR, message));
        } else if (scopeValues.size() == 0 && shouldHaveScopes) {
            String message = String.format(Messages.error_scope_should_not_be_empty, name, type);
            errors.add(error(node.get(name), IMarker.SEVERITY_ERROR, message));
        } else {
            for (AbstractNode scope : scopeValues.elements()) {
                try {
                    String scopeName = (String) scope.asValue().getValue();
                    if (!scopes.contains(scopeName)) {
                        String message = String.format(Messages.error_invalid_scope_reference, scopeName, name);
                        errors.add(error(scope, IMarker.SEVERITY_ERROR, message));
                    }
                } catch (Exception e) {
                // Invalid scope name type.
                // No need to create an error, it will be handle by the schema validation.
                }
            }
        }
    }
}
Also used : AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) ArrayNode(com.reprezen.swagedit.core.model.ArrayNode)

Example 23 with AbstractNode

use of com.reprezen.swagedit.core.model.AbstractNode in project KaiZen-OpenAPI-Editor by RepreZen.

the class PathParamHyperlinkDetector method findParameterPath.

/*
     * Returns a Map having for keys a method name and for value the pointer to the parameter.
     */
private Map<String, JsonPointer> findParameterPath(JsonDocument doc, JsonPointer basePath, String parameter) {
    Map<String, JsonPointer> paths = Maps.newHashMap();
    AbstractNode parent = doc.getModel().find(basePath);
    if (parent == null || !parent.isObject()) {
        return paths;
    }
    for (String method : methods) {
        if (parent.get(method) == null) {
            continue;
        }
        AbstractNode parameters = parent.get(method).get("parameters");
        if (parameters != null && parameters.isArray()) {
            for (int i = 0; i < parameters.size(); i++) {
                AbstractNode current = parameters.get(i);
                if (JsonReference.isReference(current)) {
                    JsonPointer ptr = JsonReference.getPointer(current.asObject());
                    AbstractNode resolved = doc.getModel().find(ptr);
                    if (resolved != null && resolved.isObject() && resolved.get("name") != null) {
                        if (parameter.equals(resolved.get("name").asValue().getValue())) {
                            paths.put(method, ptr);
                        }
                    }
                } else if (current.isObject() && current.get("name") != null) {
                    if (parameter.equals(current.get("name").asValue().getValue())) {
                        paths.put(method, JsonPointer.compile(basePath + "/" + method + "/parameters/" + i));
                    }
                }
            }
        }
    }
    return paths;
}
Also used : AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) JsonPointer(com.fasterxml.jackson.core.JsonPointer)

Example 24 with AbstractNode

use of com.reprezen.swagedit.core.model.AbstractNode in project KaiZen-OpenAPI-Editor by RepreZen.

the class ReferenceHyperlinkDetector method doDetect.

@Override
protected IHyperlink[] doDetect(JsonDocument doc, ITextViewer viewer, HyperlinkInfo info, JsonPointer pointer) {
    URI baseURI = getBaseURI();
    AbstractNode node = doc.getModel().find(pointer);
    JsonReference reference = getFactory().createSimpleReference(getBaseURI(), node);
    if (reference == null) {
        reference = getFactory().create(node);
    }
    if (reference.isInvalid() || reference.isMissing(doc, getBaseURI())) {
        return null;
    }
    if (reference.isLocal()) {
        IRegion target = doc.getRegion(reference.getPointer());
        if (target == null) {
            return null;
        }
        return new IHyperlink[] { new SwaggerHyperlink(reference.getPointer().toString(), viewer, info.region, target) };
    } else {
        URI resolved;
        try {
            resolved = baseURI.resolve(reference.getUri());
        } catch (IllegalArgumentException e) {
            // the given string violates RFC 2396
            return null;
        }
        IFile file = DocumentUtils.getWorkspaceFile(resolved);
        if (file != null && file.exists()) {
            return new IHyperlink[] { createFileHyperlink(info.region, info.text, file, reference.getPointer()) };
        }
    }
    return null;
}
Also used : JsonReference(com.reprezen.swagedit.core.json.references.JsonReference) IFile(org.eclipse.core.resources.IFile) AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) URI(java.net.URI) IRegion(org.eclipse.jface.text.IRegion)

Example 25 with AbstractNode

use of com.reprezen.swagedit.core.model.AbstractNode in project KaiZen-OpenAPI-Editor by RepreZen.

the class ComponentContextType method isReference.

protected boolean isReference(Model model, JsonPointer pointer) {
    AbstractNode contextNode = model.find(pointer);
    if (contextNode == null) {
        return false;
    }
    TypeDefinition type = contextNode.getType();
    if (type instanceof MultipleTypeDefinition) {
        // MultipleTypeDefinition is a special case, it happens when several properties match a property
        for (TypeDefinition nestedType : ((MultipleTypeDefinition) type).getMultipleTypes()) {
            if (getReferencePointerString().equals(nestedType.getPointer().toString())) {
                return true;
            }
        }
    }
    JsonPointer pointerToType = type.getPointer();
    if (pointerToType == null) {
        return false;
    }
    return getReferencePointerString().equals(pointerToType.toString());
}
Also used : MultipleTypeDefinition(com.reprezen.swagedit.core.schema.MultipleTypeDefinition) AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) JsonPointer(com.fasterxml.jackson.core.JsonPointer) MultipleTypeDefinition(com.reprezen.swagedit.core.schema.MultipleTypeDefinition) ComplexTypeDefinition(com.reprezen.swagedit.core.schema.ComplexTypeDefinition) TypeDefinition(com.reprezen.swagedit.core.schema.TypeDefinition)

Aggregations

AbstractNode (com.reprezen.swagedit.core.model.AbstractNode)29 Model (com.reprezen.swagedit.core.model.Model)6 JsonPointer (com.fasterxml.jackson.core.JsonPointer)5 JsonReference (com.reprezen.swagedit.core.json.references.JsonReference)5 URI (java.net.URI)5 IFile (org.eclipse.core.resources.IFile)5 IRegion (org.eclipse.jface.text.IRegion)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)4 Proposal (com.reprezen.swagedit.core.assist.Proposal)3 JsonDocumentManager (com.reprezen.swagedit.core.json.references.JsonDocumentManager)3 TypeDefinition (com.reprezen.swagedit.core.schema.TypeDefinition)3 Matcher (java.util.regex.Matcher)3 SwaggerHyperlink (com.reprezen.swagedit.core.hyperlinks.SwaggerHyperlink)2 JsonReferenceFactory (com.reprezen.swagedit.core.json.references.JsonReferenceFactory)2 ArrayNode (com.reprezen.swagedit.core.model.ArrayNode)2 ComplexTypeDefinition (com.reprezen.swagedit.core.schema.ComplexTypeDefinition)2 MultipleTypeDefinition (com.reprezen.swagedit.core.schema.MultipleTypeDefinition)2 Position (org.eclipse.jface.text.Position)2 JsonDocument (com.reprezen.swagedit.core.editor.JsonDocument)1