From b95620e308289db5da6c3263742efcda373bfafd Mon Sep 17 00:00:00 2001 From: Haider Iqbal Date: Thu, 5 Sep 2024 12:15:57 +0100 Subject: [PATCH] - Update conditional in JsonHelper (#732) --- .../spot/ols/repository/v1/JsonHelper.java | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/uk/ac/ebi/spot/ols/repository/v1/JsonHelper.java b/backend/src/main/java/uk/ac/ebi/spot/ols/repository/v1/JsonHelper.java index 44842360c..1a0831572 100644 --- a/backend/src/main/java/uk/ac/ebi/spot/ols/repository/v1/JsonHelper.java +++ b/backend/src/main/java/uk/ac/ebi/spot/ols/repository/v1/JsonHelper.java @@ -6,6 +6,7 @@ import com.google.gson.JsonObject; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class JsonHelper { @@ -41,11 +42,48 @@ public static String objectToString(JsonElement value) { return elements.get(0); - } else if(value.isJsonObject()) { + } else if(value.isJsonObject() && value.getAsJsonObject().get("value") != null) { return objectToString(value.getAsJsonObject().get("value")); + + /* This is a special case for the OLS API. If the value is a nested JsonObject like this + an example from OIO ontology: + { + "http://www.geneontology.org/formats/oboInOwl#hasURI": { + "type": [ + "literal" + ], + "datatype": "http://www.w3.org/2001/XMLSchema#anyURI", + "value": "http://www.obofoundry.org/wiki/index.php/Definitions" + }, + "http://www.w3.org/1999/02/22-rdf-syntax-ns#type": "http://www.geneontology.org/formats/oboInOwl#DbXref", + "http://www.w3.org/2000/01/rdf-schema#label": { + "type": [ + "literal" + ], + "value": "URL:http://www.obofoundry.org/wiki/index.php/Definitions" + }, + "isObsolete": false + } + + * For this type of JsonObject we need to iterate through the entries and find value key. + * For sake of simplicity I've returned the first value which is found as we don't have any + * mechanism to judge which value to prefer over the other. + */ + + } else if (value.isJsonObject()) { + for (Map.Entry entry : value.getAsJsonObject().entrySet()) { + JsonElement element = entry.getValue(); + if (element.isJsonObject()) { + JsonObject obj = element.getAsJsonObject(); + if (obj.has("value")) { + return obj.get("value").getAsString(); + } + } + } } else { return value.getAsString(); } + return value.getAsString(); } public static List getValues(JsonObject json, String predicate) {