From 5dcc5b5d902a54246198c443a2273601a1670e50 Mon Sep 17 00:00:00 2001 From: Csanad Telbisz Date: Wed, 22 Jun 2022 13:56:19 +0200 Subject: [PATCH 1/2] test json deserialization with Commit --- src/test/java/org/omg/sysml/JSONTest.java | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/test/java/org/omg/sysml/JSONTest.java diff --git a/src/test/java/org/omg/sysml/JSONTest.java b/src/test/java/org/omg/sysml/JSONTest.java new file mode 100644 index 0000000..6a1a816 --- /dev/null +++ b/src/test/java/org/omg/sysml/JSONTest.java @@ -0,0 +1,41 @@ +package org.omg.sysml; + +import org.junit.Test; +import org.omg.sysml.model.Commit; + +import java.lang.reflect.Type; + +import static org.junit.Assert.*; + +public class JSONTest { + private final JSON json = new JSON(); + + /** + * Tests JSON deserialization with a Commit provided in a string in JSON format. + */ + @Test + public void testJsonDeserialization() { + String elementId = "13ca289c-b7fe-4f41-a639-1e8f9b3199d5"; + String jsonString = + "{" + + "\"@id\":\"36391e9a-8ab2-4a1a-bd6f-93edd0361c5f\"," + + "\"@type\":\"Commit\"," + + "\"change\":[" + + "{" + + "\"identity\":{" + + "\"@id\":\"" + elementId + "\"," + + "\"@type\":\"DataIdentity\"" + + "}," + + "\"payload\":{" + + "\"@type\":\"ActionDefinition\"," + + "\"@id\":\"" + elementId + "\"," + + "\"elementId\":\"" + elementId + "\"" + + "}" + + "}" + + "]" + + "}"; + Type type = Commit.class; + Commit commit = json.deserialize(jsonString, type); + assertEquals(elementId, commit.getChange().get(0).getPayload().get("elementId")); + } +} From 649d75ff5e4e7528e7eb597716be7a22ede1847b Mon Sep 17 00:00:00 2001 From: Csanad Telbisz Date: Wed, 22 Jun 2022 14:03:51 +0200 Subject: [PATCH 2/2] Data deserialization problem hotfix left possibility for using the hashmap with other types in a future version --- src/main/java/org/omg/sysml/JSON.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/omg/sysml/JSON.java b/src/main/java/org/omg/sysml/JSON.java index 75dd951..2a8de40 100644 --- a/src/main/java/org/omg/sysml/JSON.java +++ b/src/main/java/org/omg/sysml/JSON.java @@ -58,7 +58,7 @@ public Class getClassForElement(JsonElement readElement) { Map classByDiscriminatorValue = new HashMap(); classByDiscriminatorValue.put("Constraint", Constraint.class); return getClassByDiscriminator(classByDiscriminatorValue, - getDiscriminatorValue(readElement, "@type")); + getDiscriminatorValue(readElement, "@type"), Constraint.class); } }) .registerTypeSelector(Data.class, new TypeSelector() { @@ -67,7 +67,7 @@ public Class getClassForElement(JsonElement readElement) { Map classByDiscriminatorValue = new HashMap(); classByDiscriminatorValue.put("Data", Data.class); return getClassByDiscriminator(classByDiscriminatorValue, - getDiscriminatorValue(readElement, "@type")); + getDiscriminatorValue(readElement, "@type"), Data.class); } }) ; @@ -90,10 +90,13 @@ private static String getDiscriminatorValue(JsonElement readElement, String disc * @param discriminatorValue The value of the OpenAPI discriminator in the input data. * @return The Java class that implements the OpenAPI schema */ - private static Class getClassByDiscriminator(Map classByDiscriminatorValue, String discriminatorValue) { + private static Class getClassByDiscriminator(Map classByDiscriminatorValue, String discriminatorValue, Class defaultClass) { Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue); if (null == clazz) { - throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">"); + if(null == defaultClass) { + throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">"); + } + return defaultClass; } return clazz; }