Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #8 Data deserialization problem #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/main/java/org/omg/sysml/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Class getClassForElement(JsonElement readElement) {
Map<String, Class> classByDiscriminatorValue = new HashMap<String, Class>();
classByDiscriminatorValue.put("Constraint", Constraint.class);
return getClassByDiscriminator(classByDiscriminatorValue,
getDiscriminatorValue(readElement, "@type"));
getDiscriminatorValue(readElement, "@type"), Constraint.class);
}
})
.registerTypeSelector(Data.class, new TypeSelector() {
Expand All @@ -67,7 +67,7 @@ public Class getClassForElement(JsonElement readElement) {
Map<String, Class> classByDiscriminatorValue = new HashMap<String, Class>();
classByDiscriminatorValue.put("Data", Data.class);
return getClassByDiscriminator(classByDiscriminatorValue,
getDiscriminatorValue(readElement, "@type"));
getDiscriminatorValue(readElement, "@type"), Data.class);
}
})
;
Expand All @@ -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;
}
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/org/omg/sysml/JSONTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}