use of oap.json.JsonException in project oap by oaplatform.
the class WsService method getValues.
private LinkedHashMap<Reflection.Parameter, Object> getValues(LinkedHashMap<Reflection.Parameter, Object> values) {
try {
val res = new LinkedHashMap<Reflection.Parameter, Object>();
values.forEach((key, value) -> {
Object map = map(key.type(), value);
res.put(key, map);
});
return res;
} catch (JsonException e) {
throw new WsClientException(e);
}
}
use of oap.json.JsonException in project oap by oaplatform.
the class JsonPartialValidatorPeer method validate.
@Override
@SneakyThrows
public ValidationErrors validate(Object value, LinkedHashMap<Reflection.Parameter, Object> originalValues) {
try {
final Object objectId = getValue(originalValues, validate.idParameterName());
final String id = objectId instanceof Optional ? ((Optional) objectId).get().toString() : objectId.toString();
final Object root = method.invoke(instance, id);
if (root == null) {
return ValidationErrors.empty();
}
final String fetchedRoot = Binder.json.marshal(Binder.json.clone(root));
log.trace("Retrieved object [{}] with id [{}]", fetchedRoot, id);
final Map rootMap = Binder.json.unmarshal(Map.class, fetchedRoot);
final Map partialValue = Binder.json.unmarshal(Map.class, (String) value);
Map child = rootMap;
for (String pathElement : validate.path().split("(?<=})\\.")) {
if (pathElement.contains("$")) {
final String[] split = pathElement.split("\\.");
final Object next = child.get(split[0]);
Preconditions.checkState(next != null, "schema has no elements for value " + split[0]);
Preconditions.checkState(next instanceof List, split[0] + " should be of type list");
final Object idValue = ((Optional) getValue(originalValues, split[1].replaceAll("\\$|\\{|\\}", ""))).get();
final Optional matchedChild = ((List) next).stream().filter(o -> idValue.equals(((Map) o).get("id").toString())).findFirst();
child = (Map) matchedChild.get();
continue;
}
final Object next = child.get(pathElement);
if (next == null) {
child.put(pathElement, Collections.singletonList(partialValue));
break;
} else if (next instanceof List) {
final List childElements = (List) next;
childElements.removeIf(elements -> {
final Object partialValueId = partialValue.get("id");
return partialValueId != null && partialValueId.toString().equals(((Map) elements).get("id").toString());
});
childElements.add(partialValue);
child.put(pathElement, childElements);
break;
} else {
child = (Map) next;
}
}
return ValidationErrors.errors(factory.validate(rootMap, validate.ignoreRequired()));
} catch (JsonException e) {
throw new WsClientException(e.getMessage(), e);
}
}
Aggregations