Skip to content

Commit 680e077

Browse files
author
Jakub Vavrik
committed
Added simple fix for infinite recursion in entity mapping if class contains a field of same type as the class. In such case will skip that field.
1 parent 61d8114 commit 680e077

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/main/java/org/springframework/data/elasticsearch/core/MappingBuilder.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import org.springframework.data.util.TypeInformation;
2626

2727
import java.io.IOException;
28+
import java.util.HashSet;
2829
import java.util.Map;
30+
import java.util.Set;
2931

3032
import static org.apache.commons.lang.StringUtils.EMPTY;
3133
import static org.apache.commons.lang.StringUtils.isNotBlank;
@@ -57,14 +59,13 @@ class MappingBuilder {
5759
static XContentBuilder buildMapping(Class clazz, String indexType, String idFieldName) throws IOException {
5860
XContentBuilder xContentBuilder = jsonBuilder().startObject().startObject(indexType).startObject(FIELD_PROPERTIES);
5961

60-
mapEntity(xContentBuilder, clazz, true, idFieldName, EMPTY);
62+
mapEntity(xContentBuilder, clazz, true, idFieldName, EMPTY, new HashSet<Class>());
6163

6264
return xContentBuilder.endObject().endObject().endObject();
6365
}
6466

6567
private static void mapEntity(XContentBuilder xContentBuilder, Class clazz, boolean isRootObject, String idFieldName,
66-
String nestedObjectFieldName) throws IOException {
67-
68+
String nestedObjectFieldName, Set<Class> ancestorClasses) throws IOException {
6869
java.lang.reflect.Field[] fields = clazz.getDeclaredFields();
6970

7071
if (!isRootObject && isAnyPropertyAnnotatedAsField(fields)) {
@@ -78,7 +79,12 @@ private static void mapEntity(XContentBuilder xContentBuilder, Class clazz, bool
7879
}
7980

8081
if (isEntity(field)) {
81-
mapEntity(xContentBuilder, field.getType(), false, EMPTY, field.getName());
82+
if (ancestorClasses.contains(field.getType())) {
83+
continue;
84+
} else {
85+
ancestorClasses.add(field.getType());
86+
mapEntity(xContentBuilder, field.getType(), false, EMPTY, field.getName(), ancestorClasses);
87+
}
8288
}
8389

8490
Field singleField = field.getAnnotation(Field.class);

0 commit comments

Comments
 (0)