Skip to content

Commit 07b18b5

Browse files
committed
PLFM-6595: Handle unexpected concreteType
1 parent bdb4d73 commit 07b18b5

File tree

9 files changed

+32
-13
lines changed

9 files changed

+32
-13
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>org.sagebionetworks</groupId>
66
<artifactId>schema-to-pojo</artifactId>
7-
<version>0.6.8</version>
7+
<version>0.6.9</version>
88
<packaging>pom</packaging>
99
<name>schema-to-pojo</name>
1010
<description>Container for the rest of the sub-projects</description>

schema-to-pojo-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>schema-to-pojo</artifactId>
77
<groupId>org.sagebionetworks</groupId>
8-
<version>0.6.8</version>
8+
<version>0.6.9</version>
99
</parent>
1010
<artifactId>schema-to-pojo-core</artifactId>
1111
<name>schema-to-pojo-core</name>

schema-to-pojo-gwt/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<artifactId>schema-to-pojo</artifactId>
66
<groupId>org.sagebionetworks</groupId>
7-
<version>0.6.8</version>
7+
<version>0.6.9</version>
88
</parent>
99
<artifactId>schema-to-pojo-gwt</artifactId>
1010
<packaging>jar</packaging>

schema-to-pojo-integration-tests/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>schema-to-pojo</artifactId>
77
<groupId>org.sagebionetworks</groupId>
8-
<version>0.6.8</version>
8+
<version>0.6.9</version>
99
</parent>
1010
<artifactId>schema-to-pojo-integration-tests</artifactId>
1111
<name>schema-to-pojo-integration-tests</name>

schema-to-pojo-lib/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>schema-to-pojo</artifactId>
77
<groupId>org.sagebionetworks</groupId>
8-
<version>0.6.8</version>
8+
<version>0.6.9</version>
99
</parent>
1010
<artifactId>schema-to-pojo-lib</artifactId>
1111
<name>schema-to-pojo-lib</name>

schema-to-pojo-maven-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<artifactId>schema-to-pojo</artifactId>
66
<groupId>org.sagebionetworks</groupId>
7-
<version>0.6.8</version>
7+
<version>0.6.9</version>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
1010
<artifactId>schema-to-pojo-maven-plugin</artifactId>

schema-to-pojo-org-json/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>schema-to-pojo</artifactId>
77
<groupId>org.sagebionetworks</groupId>
8-
<version>0.6.8</version>
8+
<version>0.6.9</version>
99
</parent>
1010
<artifactId>schema-to-pojo-org-json</artifactId>
1111
<name>schema-to-pojo-org-json</name>

schema-to-pojo-org-json/src/main/java/org/sagebionetworks/schema/adapter/org/json/EntityFactory.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.ArrayList;
55
import java.util.Collection;
66
import java.util.Iterator;
7-
import java.util.List;
87

98
import org.json.JSONObject;
109
import org.sagebionetworks.schema.ObjectSchema;
@@ -156,20 +155,25 @@ private static <T extends JSONEntity> T createEntityFromAdapter(Class<? extends
156155
// Now create a new instance of the class
157156
try {
158157
T newInstance = null;
159-
if(clazz.isInterface()){
158+
if (clazz.isInterface()) {
160159
String concreteType = extractConcreteType(adapter, clazz);
160+
Class<T> newInstanceClass;
161161
// Use the concrete type to instantiate the object.
162162
try {
163-
newInstance = (T) Class.forName(concreteType).newInstance();
163+
newInstanceClass = (Class<T>) Class.forName(concreteType);
164164
} catch (ClassNotFoundException e) {
165-
throw new IllegalArgumentException(String.format("Unknown %s : '%s'",ObjectSchema.CONCRETE_TYPE, concreteType), e);
165+
throw new IllegalArgumentException(String.format("Unknown %s : '%s'", ObjectSchema.CONCRETE_TYPE, concreteType), e);
166166
}
167-
}else{
167+
if (!clazz.isAssignableFrom(newInstanceClass)) {
168+
throw new IllegalArgumentException(String.format("Unexpected concreteType: \"%s\" is not of type \"%s\"", concreteType, clazz.getName()));
169+
}
170+
newInstance = newInstanceClass.newInstance();
171+
} else {
168172
newInstance = clazz.newInstance();
169173
}
170174
newInstance.initializeFromJSONObject(adapter);
171175
return newInstance;
172-
} catch (IllegalArgumentException e) {
176+
} catch (IllegalArgumentException e) {
173177
throw e;
174178
} catch (Exception e) {
175179
throw new JSONObjectAdapterException(e);

schema-to-pojo-org-json/src/test/java/org/sagebionetworks/schema/adapter/org/json/EntityFactoryTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ public void testCreateEntityFromJSONStringWithMissingConcreteType() throws JSONO
140140

141141
}
142142

143+
@Test
144+
public void testCreateEntityFromJSONStringWithUnexpectedConcreteType() throws JSONObjectAdapterException {
145+
146+
// A json string without concrete type,
147+
String json = "{\"value\":\"some value\", \"concreteType\": \"java.lang.String\"}";
148+
149+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> {
150+
// Call under test
151+
EntityFactory.createEntityFromJSONString(json, SimpleInterface.class);
152+
});
153+
154+
assertEquals("Unexpected concreteType: \"java.lang.String\" is not of type \"org.sagebionetworks.schema.adapter.org.json.SimpleInterface\"", ex.getMessage());
155+
156+
}
157+
143158
@Test
144159
public void testWriteAndReadJSONArrayString() throws JSONObjectAdapterException {
145160
List<SimpleEntityStub> list = Arrays.asList(new SimpleEntityStub().withValue("1"), null,

0 commit comments

Comments
 (0)