From bea93ab328b82836f0c54ad995cb9ea5467a3fa9 Mon Sep 17 00:00:00 2001 From: Hossein Rimaz Date: Mon, 2 Oct 2023 15:17:55 +0200 Subject: [PATCH 1/2] Fix submodelElements loss after calling $metamodel endpoint See issue #85 --- .../InMemorySubmodelRepository.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/InMemorySubmodelRepository.java b/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/InMemorySubmodelRepository.java index cb9159460..747735cba 100644 --- a/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/InMemorySubmodelRepository.java +++ b/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/InMemorySubmodelRepository.java @@ -34,6 +34,10 @@ import java.util.TreeMap; import java.util.stream.Collectors; +import org.eclipse.digitaltwin.aas4j.v3.dataformat.DeserializationException; +import org.eclipse.digitaltwin.aas4j.v3.dataformat.SerializationException; +import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonDeserializer; +import org.eclipse.digitaltwin.aas4j.v3.dataformat.json.JsonSerializer; import org.eclipse.digitaltwin.aas4j.v3.model.OperationVariable; import org.eclipse.digitaltwin.aas4j.v3.model.Submodel; import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement; @@ -220,8 +224,18 @@ public SubmodelValueOnly getSubmodelByIdValueOnly(String submodelId) { @Override public Submodel getSubmodelByIdMetadata(String submodelId) { Submodel submodel = getSubmodel(submodelId); - submodel.setSubmodelElements(null); - return submodel; + try { + String submodelAsJSON = new JsonSerializer().write(submodel); + Submodel submodelDeepCopy = new JsonDeserializer().readReferable(submodelAsJSON, Submodel.class); + submodelDeepCopy.setSubmodelElements(null); + return submodelDeepCopy; + } catch (DeserializationException e) { + e.printStackTrace(); + throw new RuntimeException("Unable to deserialize the Submodel"); + } catch (SerializationException e) { + e.printStackTrace(); + throw new RuntimeException("Unable to serialize the Submodel"); + } } @Override From 692b1839a7049ab69c82cb9baf928c04b355efae Mon Sep 17 00:00:00 2001 From: Hossein Rimaz Date: Wed, 4 Oct 2023 10:23:38 +0200 Subject: [PATCH 2/2] Improve exception handling --- .../submodelrepository/InMemorySubmodelRepository.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/InMemorySubmodelRepository.java b/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/InMemorySubmodelRepository.java index 747735cba..e22bbc98e 100644 --- a/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/InMemorySubmodelRepository.java +++ b/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/InMemorySubmodelRepository.java @@ -230,11 +230,9 @@ public Submodel getSubmodelByIdMetadata(String submodelId) { submodelDeepCopy.setSubmodelElements(null); return submodelDeepCopy; } catch (DeserializationException e) { - e.printStackTrace(); - throw new RuntimeException("Unable to deserialize the Submodel"); + throw new RuntimeException("Unable to deserialize the Submodel", e); } catch (SerializationException e) { - e.printStackTrace(); - throw new RuntimeException("Unable to serialize the Submodel"); + throw new RuntimeException("Unable to serialize the Submodel", e); } }