From dc8595d13e7516c0bd50a74eb1ae01a6da865108 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 19 Apr 2024 12:11:27 +0200 Subject: [PATCH] Fixed serialization of unknown properties --- .../serialization/ElementMapper.java | 50 ++++++++++++------- .../informationsystem/utils/UUIDUtility.java | 6 ++- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/serialization/ElementMapper.java b/src/main/java/org/gcube/informationsystem/serialization/ElementMapper.java index b3f4f24..aac9290 100644 --- a/src/main/java/org/gcube/informationsystem/serialization/ElementMapper.java +++ b/src/main/java/org/gcube/informationsystem/serialization/ElementMapper.java @@ -25,6 +25,7 @@ import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; import org.gcube.com.fasterxml.jackson.databind.SerializationFeature; import org.gcube.com.fasterxml.jackson.databind.exc.InvalidTypeIdException; import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode; +import org.gcube.com.fasterxml.jackson.databind.node.JsonNodeType; import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode; import org.gcube.com.fasterxml.jackson.databind.node.TextNode; import org.gcube.informationsystem.base.reference.AccessType; @@ -32,6 +33,7 @@ import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.discovery.Discovery; import org.gcube.informationsystem.discovery.knowledge.Knowledge; import org.gcube.informationsystem.model.reference.ModelElement; +import org.gcube.informationsystem.model.reference.properties.Property; import org.gcube.informationsystem.types.TypeMapper; import org.gcube.informationsystem.types.reference.Type; import org.slf4j.Logger; @@ -207,24 +209,26 @@ public abstract class ElementMapper { ArrayNode arrayNode = (ArrayNode) objectNode.get(ModelElement.SUPERTYPES_PROPERTY); String candidatedSupertype = null; - for(int i = 0; i < arrayNode.size(); i++) { - String superType = arrayNode.get(i).asText(); - if(knownTypes.containsKey(superType)) { - candidatedSupertype = superType; - try { - // Checking if it is one of the base type. In some cases we need to use dummy - // implementation - AccessType accessType = AccessType.getAccessType(superType); - // It is one of the BaseType. - // Looking if we need to set the dummy implementation class - if(accessType.getDummyImplementationClass()!=null) { - // This should not happen because the type has been assigned already to the dummy class. - candidatedSupertype = accessType.getDummyImplementationClass().getSimpleName(); + if(arrayNode!=null) { + for(int i = 0; i < arrayNode.size(); i++) { + String superType = arrayNode.get(i).asText(); + if(knownTypes.containsKey(superType)) { + candidatedSupertype = superType; + try { + // Checking if it is one of the base type. In some cases we need to use dummy + // implementation + AccessType accessType = AccessType.getAccessType(superType); + // It is one of the BaseType. + // Looking if we need to set the dummy implementation class + if(accessType.getDummyImplementationClass()!=null) { + // This should not happen because the type has been assigned already to the dummy class. + candidatedSupertype = accessType.getDummyImplementationClass().getSimpleName(); + } + } catch(Exception ex) { + // can continue discovery } - } catch(Exception ex) { - // can continue discovery + break; } - break; } } @@ -243,7 +247,17 @@ public abstract class ElementMapper { } protected static JsonNode analizeTypes(ObjectNode objectNode) { - String cls = objectNode.get(Element.TYPE_PROPERTY).asText(); + String cls = null; + + JsonNode typeJN = objectNode.get(Element.TYPE_PROPERTY); + if(typeJN !=null) { + cls = objectNode.get(Element.TYPE_PROPERTY).asText(); + } + + if(cls==null && objectNode.getNodeType()==JsonNodeType.OBJECT) { + cls = Property.NAME; + } + if(!knownTypes.containsKey(cls)) { objectNode = setTypeToBestAvailable(objectNode); } @@ -267,7 +281,7 @@ public abstract class ElementMapper { } return objectNode; } - + protected static ArrayNode analizeTypes(ArrayNode arrayNode) { ArrayNode ret = mapper.createArrayNode(); diff --git a/src/main/java/org/gcube/informationsystem/utils/UUIDUtility.java b/src/main/java/org/gcube/informationsystem/utils/UUIDUtility.java index 574b2e9..2bebfeb 100644 --- a/src/main/java/org/gcube/informationsystem/utils/UUIDUtility.java +++ b/src/main/java/org/gcube/informationsystem/utils/UUIDUtility.java @@ -33,7 +33,11 @@ public class UUIDUtility { } public static String getUUIDAsString(JsonNode jsonNode){ - return getUUID(jsonNode).toString(); + UUID uuid = getUUID(jsonNode); + if(uuid!=null) { + return uuid.toString(); + } + return null; } public static String getUUIDAsString(String json) throws JsonProcessingException, IOException{