Skip to content

Commit ac78c4b

Browse files
committed
Merge pull request #17 from shipunyc/master
Fixes bug: POJO doesn't need to be a member class.
2 parents c2b5a99 + b3b860e commit ac78c4b

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/com/microsoft/azure/documentdb/JsonSerializable.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,16 @@ public <T extends Object> T getObject(String propertyName, Class<T> c) {
303303
}
304304
} else {
305305
// POJO.
306-
if (!c.isMemberClass() || !Modifier.isStatic(c.getModifiers())) {
306+
if (c.isAnonymousClass() || c.isLocalClass()) {
307+
throw new IllegalArgumentException(
308+
String.format("%s can't be an anonymous or local class.", c.getName()));
309+
}
310+
311+
if (c.isMemberClass() && !Modifier.isStatic(c.getModifiers())) {
307312
throw new IllegalArgumentException(
308-
"c must be a member (not an anonymous or local) and static class.");
313+
String.format("%s must be static if it's a member class.", c.getName()));
309314
}
315+
310316
try {
311317
return new ObjectMapper().readValue(jsonObj.toString(), c);
312318
} catch (IOException e) {

src/com/microsoft/azure/documentdb/test/GatewayTests.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
import com.microsoft.azure.documentdb.User;
5757
import com.microsoft.azure.documentdb.UserDefinedFunction;
5858

59+
final class AnotherPOJO {
60+
public String pojoProp = "789";
61+
}
62+
5963
public final class GatewayTests {
6064
static final String HOST = "[YOUR_ENDPOINT_HERE]";
6165
static final String MASTER_KEY = "[YOUR_KEY_HERE]";
@@ -193,7 +197,7 @@ private static String getStringFromInputStream(InputStream is) {
193197
}
194198

195199
static class StaticPOJOForTest {
196-
// Jackson's readValue method only supports static and non-local POJO.
200+
// Jackson's readValue method supports member class only if it's static.
197201
public String pojoProp = "456";
198202
}
199203

@@ -207,6 +211,7 @@ public void testJsonSerialization() {
207211
document.set("child2", new JSONObject("{'child2Prop1': '800'}"));
208212

209213
document.set("child3", new StaticPOJOForTest());
214+
document.set("child4", new AnotherPOJO());
210215
// Collection of numbers.
211216
Collection<Integer> collection1 = new ArrayList<Integer>();
212217
collection1.add(101);
@@ -242,6 +247,9 @@ public void testJsonSerialization() {
242247
" 'child3': {" +
243248
" 'pojoProp': '456'" +
244249
" }," +
250+
" 'child4': {" +
251+
" 'pojoProp': '789'" +
252+
" }," +
245253
" 'collection1': [101, 102]," +
246254
" 'collection2': [{'foo': 'bar'}]," +
247255
" 'collection3': [{'pojoProp': '456'}]," +
@@ -250,6 +258,7 @@ public void testJsonSerialization() {
250258
Assert.assertEquals(expectedDocument.toString(), document.toString());
251259

252260
Assert.assertEquals("456", document.getObject("child3", StaticPOJOForTest.class).pojoProp);
261+
Assert.assertEquals("789", document.getObject("child4", AnotherPOJO.class).pojoProp);
253262
Assert.assertEquals("456", document.getCollection("collection3",
254263
StaticPOJOForTest.class).iterator().next().pojoProp);
255264

@@ -1242,7 +1251,7 @@ public void testOfferReadAndQuery() throws DocumentClientException {
12421251

12431252
// Modify the SelfLink
12441253
String offerLink = expectedOffer.getSelfLink().substring(
1245-
0, expectedOffer.getSelfLink().length() - 1) + "x";
1254+
0, expectedOffer.getSelfLink().length() - 1) + "x";
12461255

12471256
// Read the offer
12481257
try {

0 commit comments

Comments
 (0)