Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
modified MongoUtil to ignore arrays within document DBObject instances instead of throwing an exception
  • Loading branch information
restjohn committed Jun 2, 2015
1 parent e173782 commit fb22510
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
11 changes: 8 additions & 3 deletions mongodb/src/main/java/org/geotools/data/mongodb/MongoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public static Object getDBOValue(DBObject dbo, Iterator<String> path) {
}

private static Object getDBOValueInternal(Iterator<String> path, Object current) {
if (current instanceof List) {
// TODO: add support for nested arrays in documents
return null;
}

if (path.hasNext()) {
if (current instanceof DBObject) {
String key = path.next();
Expand Down Expand Up @@ -115,12 +120,12 @@ private static Map<String, Class<?>> doFindMappableFields(DBObject dbo) {
if (k instanceof String) {
String field = (String)k;
Object v = e.getValue();
if (v instanceof DBObject) {
if (v instanceof List) {
// this is here as documentation/placeholder. no array/list support yet.
} else if (v instanceof DBObject) {
for (Map.Entry<String, Class<?>> childEntry : doFindMappableFields((DBObject)v).entrySet()) {
map.put(field + "." + childEntry.getKey(), childEntry.getValue());
}
} else if (v instanceof List) {
// this is here as documentation/placeholder. no array/list support yet.
} else {
Class<?> binding = mapBSONObjectToJavaType(v);
if (binding != null) {
Expand Down
29 changes: 29 additions & 0 deletions mongodb/src/test/java/org/geotools/data/mongodb/MongoUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

package org.geotools.data.mongodb;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import static org.hamcrest.CoreMatchers.*;
import org.hamcrest.CoreMatchers;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.matchers.JUnitMatchers;

import java.util.Arrays;
import java.util.Map;

/**
*
Expand Down Expand Up @@ -43,5 +48,29 @@ public void set() {
result = MongoUtil.getDBOValue(dbo, "bugusroot.neglectedchild");
assertThat(result, is(nullValue()));
}

@Test
public void findMappableFieldsSkipsArraysOfObjects() {
DBObject dbo = new BasicDBObject();
BasicDBList list = new BasicDBList();
list.add(new BasicDBObject("listItemKey", "value"));
dbo.put("unsupportedList", list);

Map<String, Class<?>> fields = MongoUtil.findMappableFields(dbo);

assertThat(fields.size(), equalTo(0));
}

@Test
public void getDBValueIgnoresArrayKeyPathComponents() {
DBObject dbo = new BasicDBObject();
BasicDBList list = new BasicDBList();
list.add(new BasicDBObject("listItemKey", "value"));
dbo.put("unsupportedList", list);

assertThat(MongoUtil.getDBOValue(dbo, "unsupportedList"), nullValue());
assertThat(MongoUtil.getDBOValue(dbo, "unsupportedList.objectInList"), nullValue());
assertThat(MongoUtil.getDBOValue(dbo, Arrays.asList("unsupportedList", "objectInList").iterator()), nullValue());
}

}

0 comments on commit fb22510

Please sign in to comment.