Search in sources :

Example 11 with JsonPointer

use of com.fasterxml.jackson.core.JsonPointer in project KaiZen-OpenAPI-Editor by RepreZen.

the class PathParamHyperlinkDetector method findParameterPath.

private Iterable<JsonPointer> findParameterPath(JsonDocument doc, JsonPointer basePath, String parameter) {
    AbstractNode parent = doc.getModel().find(basePath);
    if (parent == null || !parent.isObject()) {
        return Lists.newArrayList();
    }
    List<JsonPointer> paths = new ArrayList<>();
    for (HttpMethod method : HttpMethod.values()) {
        String mName = method.name().toLowerCase();
        if (parent.get(mName) == null) {
            continue;
        }
        AbstractNode parameters = parent.get(mName).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.add(ptr);
                        }
                    }
                } else if (current.isObject() && current.get("name") != null) {
                    if (parameter.equals(current.get("name").asValue().getValue())) {
                        paths.add(JsonPointer.compile(basePath + "/" + mName + "/parameters/" + i));
                    }
                }
            }
        }
    }
    return paths;
}
Also used : AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) ArrayList(java.util.ArrayList) JsonPointer(com.fasterxml.jackson.core.JsonPointer) HttpMethod(io.swagger.models.HttpMethod)

Example 12 with JsonPointer

use of com.fasterxml.jackson.core.JsonPointer in project KaiZen-OpenAPI-Editor by RepreZen.

the class PathParamHyperlinkDetector method doDetect.

@Override
protected IHyperlink[] doDetect(JsonDocument doc, ITextViewer viewer, HyperlinkInfo info, JsonPointer pointer) {
    // find selected parameter
    Matcher matcher = PARAMETER_PATTERN.matcher(info.text);
    String parameter = null;
    int start = 0, end = 0;
    while (matcher.find() && parameter == null) {
        if (matcher.start() <= info.column && matcher.end() >= info.column) {
            parameter = matcher.group(1);
            start = matcher.start();
            end = matcher.end();
        }
    }
    // no parameter found
    if (emptyToNull(parameter) == null) {
        return null;
    }
    Iterable<JsonPointer> targetPaths = findParameterPath(doc, pointer, parameter);
    IRegion linkRegion = new Region(info.getOffset() + start, end - start);
    List<IHyperlink> links = new ArrayList<>();
    for (JsonPointer path : targetPaths) {
        IRegion target = doc.getRegion(path);
        if (target != null) {
            links.add(new SwaggerHyperlink(parameter, viewer, linkRegion, target));
        }
    }
    return links.isEmpty() ? null : links.toArray(new IHyperlink[links.size()]);
}
Also used : SwaggerHyperlink(com.reprezen.swagedit.core.hyperlinks.SwaggerHyperlink) Matcher(java.util.regex.Matcher) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) ArrayList(java.util.ArrayList) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) JsonPointer(com.fasterxml.jackson.core.JsonPointer) IRegion(org.eclipse.jface.text.IRegion)

Example 13 with JsonPointer

use of com.fasterxml.jackson.core.JsonPointer in project KaiZen-OpenAPI-Editor by RepreZen.

the class NodeDeserializer method deserializeObjectNode.

protected ObjectNode deserializeObjectNode(JsonParser p, DeserializationContext context, JsonLocation startLocation) throws IllegalArgumentException, IOException {
    final Model model = (Model) context.getAttribute(ATTRIBUTE_MODEL);
    final AbstractNode parent = (AbstractNode) context.getAttribute(ATTRIBUTE_PARENT);
    final JsonPointer ptr = (JsonPointer) context.getAttribute(ATTRIBUTE_POINTER);
    final ObjectNode node = model.objectNode(parent, ptr);
    node.setStartLocation(createLocation(startLocation));
    while (p.nextToken() != JsonToken.END_OBJECT) {
        String name = p.getCurrentName();
        JsonPointer pp = JsonPointer.compile(ptr.toString() + "/" + name.replaceAll("/", "~1"));
        context.setAttribute(ATTRIBUTE_PARENT, node);
        context.setAttribute(ATTRIBUTE_POINTER, pp);
        AbstractNode v = deserialize(p, context);
        v.setProperty(name);
        node.put(name, v);
    }
    node.setEndLocation(createLocation(p.getCurrentLocation()));
    return node;
}
Also used : JsonPointer(com.fasterxml.jackson.core.JsonPointer)

Example 14 with JsonPointer

use of com.fasterxml.jackson.core.JsonPointer in project KaiZen-OpenAPI-Editor by RepreZen.

the class NodeDeserializer method deserializeArrayNode.

protected ArrayNode deserializeArrayNode(JsonParser p, DeserializationContext context, JsonLocation startLocation) throws IOException {
    final Model model = (Model) context.getAttribute(ATTRIBUTE_MODEL);
    final AbstractNode parent = (AbstractNode) context.getAttribute(ATTRIBUTE_PARENT);
    final JsonPointer ptr = (JsonPointer) context.getAttribute(ATTRIBUTE_POINTER);
    ArrayNode node = model.arrayNode(parent, ptr);
    int i = 0;
    while (p.nextToken() != JsonToken.END_ARRAY) {
        JsonPointer pp = JsonPointer.compile(ptr.toString() + "/" + i);
        context.setAttribute(ATTRIBUTE_PARENT, node);
        context.setAttribute(ATTRIBUTE_POINTER, pp);
        AbstractNode v = deserialize(p, context);
        node.add(v);
        i++;
    }
    node.setStartLocation(createLocation(startLocation));
    node.setEndLocation(createLocation(p.getCurrentLocation()));
    return node;
}
Also used : JsonPointer(com.fasterxml.jackson.core.JsonPointer)

Example 15 with JsonPointer

use of com.fasterxml.jackson.core.JsonPointer in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonReferenceFactory method createSimpleReference.

/**
     * Returns a simple reference if the value node points to a definition inside the same document.
     * 
     * @param baseURI
     * @param value
     * @return reference
     */
public JsonReference createSimpleReference(URI baseURI, AbstractNode valueNode) {
    if (valueNode.isArray() || valueNode.isObject()) {
        return null;
    }
    final Object value = valueNode.asValue().getValue();
    if (!(value instanceof String)) {
        return null;
    }
    String stringValue = (String) value;
    if (Strings.emptyToNull(stringValue) == null || stringValue.startsWith("#") || stringValue.contains("/")) {
        return null;
    }
    final Model model = valueNode.getModel();
    if (model != null) {
        JsonPointer ptr = JsonPointer.compile("/definitions/" + value);
        AbstractNode target = model.find(ptr);
        if (target != null) {
            return new JsonReference.SimpleReference(baseURI, ptr, valueNode);
        }
    }
    return null;
}
Also used : AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) Model(com.reprezen.swagedit.core.model.Model) JsonPointer(com.fasterxml.jackson.core.JsonPointer)

Aggregations

JsonPointer (com.fasterxml.jackson.core.JsonPointer)18 ArrayList (java.util.ArrayList)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)2 SwaggerHyperlink (com.reprezen.swagedit.core.hyperlinks.SwaggerHyperlink)2 AbstractNode (com.reprezen.swagedit.core.model.AbstractNode)2 IRegion (org.eclipse.jface.text.IRegion)2 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)2 BeanProperty (com.fasterxml.jackson.databind.BeanProperty)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 JsonFormatVisitable (com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)1 JsonDocument (com.reprezen.swagedit.core.editor.JsonDocument)1 Model (com.reprezen.swagedit.core.model.Model)1 ObjectNode (com.reprezen.swagedit.core.model.ObjectNode)1 HttpMethod (io.swagger.models.HttpMethod)1 URI (java.net.URI)1