Revert "Flatten recursively"

This reverts commit f95181db11405c0d3f88bbf78587bd83413a5efd.
This commit is contained in:
Xavi Aracil 2022-12-21 10:12:56 +01:00
parent 6372735105
commit de1bca76e3

View File

@ -3,6 +3,7 @@ package org.oneedtech.inspect.vc.jsonld.probe;
import static org.oneedtech.inspect.vc.Assertion.ValueType.DATA_URI_OR_URL;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
@ -26,6 +27,7 @@ import org.oneedtech.inspect.vc.Validation;
import org.oneedtech.inspect.vc.jsonld.JsonLdGeneratedObject;
import org.oneedtech.inspect.vc.probe.CredentialParseProbe;
import org.oneedtech.inspect.vc.resource.UriResourceFactory;
import org.oneedtech.inspect.vc.util.CachingDocumentLoader;
import org.oneedtech.inspect.vc.util.JsonNodeUtil;
import org.oneedtech.inspect.vc.util.PrimitiveValueValidator;
@ -36,6 +38,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.google.common.io.Resources;
import foundation.identity.jsonld.ConfigurableDocumentLoader;
/**
* Probe for fetching all elements in the graph for Open Badges 2.0 validation
* Contains the fetch part of "VALIDATE_TYPE_PROPERTY" task in python implementation, as well as the "FLATTEN_EMBEDDED_RESOURCE" task
@ -68,26 +72,9 @@ public class GraphFetcherProbe extends Probe<JsonNode> {
// flatten embeded resource
if (validation.isAllowFlattenEmbeddedResource()) {
result = flatten(ctx, result, root, node, validation);
}
// fetch
List<JsonNode> nodeList = JsonNodeUtil.asNodeList(node);
for (JsonNode childNode : nodeList) {
if (shouldFetch(childNode, validation)) {
// get node from context
result = fetchNode(ctx, result, childNode);
}
}
}
return success(ctx);
}
private ReportItems flatten(RunContext ctx, ReportItems result, JsonNode parentNode, JsonNode node, Validation validation) throws URISyntaxException, Exception {
if (!node.isTextual()) {
if (!node.isObject()) {
result = new ReportItems(List.of(result, error("Property " + validation.getName() + " referenced from " + assertion.getJson().toString() + " is not a JSON object or string as expected", ctx)));
return error("Property " + validation.getName() + " referenced from " + assertion.getJson().toString() + " is not a JSON object or string as expected", ctx);
}
JsonNode idNode = node.get("id");
@ -98,12 +85,12 @@ public class GraphFetcherProbe extends Probe<JsonNode> {
ctx.addGeneratedObject(new JsonLdGeneratedObject(JsonLDCompactionProve.getId(newId.toString()), merged.toString()));
// update existing node with new id
updateNode(validation, parentNode.get("id").asText().strip(), idNode, ctx);
updateNode(validation, idNode, ctx);
return warning("Node id missing at " + node.toString() + ". A blank node ID has been assigned", ctx);
} else if (!idNode.isTextual() || !PrimitiveValueValidator.validateIri(idNode)) {
return new ReportItems(List.of(result, error("Embedded JSON object at " + node.asText() + " has no proper assigned id.", ctx)));
} else { // if (assertion.getCredentialType() == Type.Assertion && !PrimitiveValueValidator.validateUrl(idNode)) {
return error("Embedded JSON object at " + node.asText() + " has no proper assigned id.", ctx);
} else if (assertion.getCredentialType() == Type.Assertion && !PrimitiveValueValidator.validateUrl(idNode)) {
if (!isUrn(idNode)) {
logger.info("ID format for " + idNode.toString() + " at " + assertion.getCredentialType() + " not in an expected HTTP or URN:UUID scheme");
}
@ -113,29 +100,29 @@ public class GraphFetcherProbe extends Probe<JsonNode> {
ctx.addGeneratedObject(new JsonLdGeneratedObject(JsonLDCompactionProve.getId(idNode.asText().strip()), merged.toString()));
// update existing node with new id
updateNode(validation, parentNode.get("id").asText().strip(), idNode, ctx);
updateNode(validation, idNode, ctx);
// } else {
// // update existing node with new id
// updateNode(validation, idNode, ctx);
} else {
// // fetch node and add it to the graph
// result = new ReportItems(List.of(result, fetchNode(ctx, result, idNode)));
}
// update existing node with new id
updateNode(validation, idNode, ctx);
// recursive call
List<Validation> flattenValidations = Type.valueOf(node.get("type")).getValidations().stream()
.filter(val -> val.getType() == ValueType.ID && val.isAllowFlattenEmbeddedResource())
.filter(val -> node.hasNonNull(val.getName()))
.collect(Collectors.toList());
for (Validation val : flattenValidations) {
if (node.hasNonNull(val.getName())) {
result = new ReportItems(List.of(result, flatten(ctx, result, node, node.get(val.getName()), val)));
// fetch node and add it to the graph
result = fetchNode(ctx, result, idNode);
}
}
}
return result;
List<JsonNode> nodeList = JsonNodeUtil.asNodeList(node);
for (JsonNode childNode : nodeList) {
if (shouldFetch(childNode, validation)) {
// get node from context
result = fetchNode(ctx, result, childNode);
}
}
}
return success(ctx);
}
private ReportItems fetchNode(RunContext ctx, ReportItems result, JsonNode idNode)
@ -178,8 +165,8 @@ public class GraphFetcherProbe extends Probe<JsonNode> {
(validation.isAllowDataUri() || ValueType.IRI.getValidationFunction().apply(node));
}
private void updateNode(Validation validation, String parentId, JsonNode idNode, RunContext ctx) throws IOException {
JsonLdGeneratedObject jsonLdGeneratedObject = ctx.getGeneratedObject(JsonLDCompactionProve.getId(parentId));
private void updateNode(Validation validation, JsonNode idNode, RunContext ctx) throws IOException {
JsonLdGeneratedObject jsonLdGeneratedObject = ctx.getGeneratedObject(JsonLDCompactionProve.getId(assertion));
JsonNode merged = createNewJson(ctx, jsonLdGeneratedObject.getJson(), "{\"" + validation.getName() + "\": \"" + idNode.asText().strip() + "\"}");
jsonLdGeneratedObject.setJson(merged.toString());