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;
}
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()]);
}
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;
}
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;
}
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;
}
Aggregations