Skip to content

Commit

Permalink
Merge pull request Sage-Bionetworks#48 from marcomarasca/PLFM-6595
Browse files Browse the repository at this point in the history
PLFM-6595: Handle unexpected concreteType
  • Loading branch information
john-hill authored Mar 31, 2022
2 parents bdb4d73 + 07b18b5 commit ef31311
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.sagebionetworks</groupId>
<artifactId>schema-to-pojo</artifactId>
<version>0.6.8</version>
<version>0.6.9</version>
<packaging>pom</packaging>
<name>schema-to-pojo</name>
<description>Container for the rest of the sub-projects</description>
Expand Down
2 changes: 1 addition & 1 deletion schema-to-pojo-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>schema-to-pojo</artifactId>
<groupId>org.sagebionetworks</groupId>
<version>0.6.8</version>
<version>0.6.9</version>
</parent>
<artifactId>schema-to-pojo-core</artifactId>
<name>schema-to-pojo-core</name>
Expand Down
2 changes: 1 addition & 1 deletion schema-to-pojo-gwt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>schema-to-pojo</artifactId>
<groupId>org.sagebionetworks</groupId>
<version>0.6.8</version>
<version>0.6.9</version>
</parent>
<artifactId>schema-to-pojo-gwt</artifactId>
<packaging>jar</packaging>
Expand Down
2 changes: 1 addition & 1 deletion schema-to-pojo-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>schema-to-pojo</artifactId>
<groupId>org.sagebionetworks</groupId>
<version>0.6.8</version>
<version>0.6.9</version>
</parent>
<artifactId>schema-to-pojo-integration-tests</artifactId>
<name>schema-to-pojo-integration-tests</name>
Expand Down
2 changes: 1 addition & 1 deletion schema-to-pojo-lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>schema-to-pojo</artifactId>
<groupId>org.sagebionetworks</groupId>
<version>0.6.8</version>
<version>0.6.9</version>
</parent>
<artifactId>schema-to-pojo-lib</artifactId>
<name>schema-to-pojo-lib</name>
Expand Down
2 changes: 1 addition & 1 deletion schema-to-pojo-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>schema-to-pojo</artifactId>
<groupId>org.sagebionetworks</groupId>
<version>0.6.8</version>
<version>0.6.9</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>schema-to-pojo-maven-plugin</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion schema-to-pojo-org-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>schema-to-pojo</artifactId>
<groupId>org.sagebionetworks</groupId>
<version>0.6.8</version>
<version>0.6.9</version>
</parent>
<artifactId>schema-to-pojo-org-json</artifactId>
<name>schema-to-pojo-org-json</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.json.JSONObject;
import org.sagebionetworks.schema.ObjectSchema;
Expand Down Expand Up @@ -156,20 +155,25 @@ private static <T extends JSONEntity> T createEntityFromAdapter(Class<? extends
// Now create a new instance of the class
try {
T newInstance = null;
if(clazz.isInterface()){
if (clazz.isInterface()) {
String concreteType = extractConcreteType(adapter, clazz);
Class<T> newInstanceClass;
// Use the concrete type to instantiate the object.
try {
newInstance = (T) Class.forName(concreteType).newInstance();
newInstanceClass = (Class<T>) Class.forName(concreteType);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(String.format("Unknown %s : '%s'",ObjectSchema.CONCRETE_TYPE, concreteType), e);
throw new IllegalArgumentException(String.format("Unknown %s : '%s'", ObjectSchema.CONCRETE_TYPE, concreteType), e);
}
}else{
if (!clazz.isAssignableFrom(newInstanceClass)) {
throw new IllegalArgumentException(String.format("Unexpected concreteType: \"%s\" is not of type \"%s\"", concreteType, clazz.getName()));
}
newInstance = newInstanceClass.newInstance();
} else {
newInstance = clazz.newInstance();
}
newInstance.initializeFromJSONObject(adapter);
return newInstance;
} catch (IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
throw e;
} catch (Exception e) {
throw new JSONObjectAdapterException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ public void testCreateEntityFromJSONStringWithMissingConcreteType() throws JSONO

}

@Test
public void testCreateEntityFromJSONStringWithUnexpectedConcreteType() throws JSONObjectAdapterException {

// A json string without concrete type,
String json = "{\"value\":\"some value\", \"concreteType\": \"java.lang.String\"}";

IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
// Call under test
EntityFactory.createEntityFromJSONString(json, SimpleInterface.class);
});

assertEquals("Unexpected concreteType: \"java.lang.String\" is not of type \"org.sagebionetworks.schema.adapter.org.json.SimpleInterface\"", ex.getMessage());

}

@Test
public void testWriteAndReadJSONArrayString() throws JSONObjectAdapterException {
List<SimpleEntityStub> list = Arrays.asList(new SimpleEntityStub().withValue("1"), null,
Expand Down

0 comments on commit ef31311

Please sign in to comment.