mappingbuilder circular reference issue
I found that by trying to save a spatial4j Point object using a repository that MappingBuilder threw a StackOverFlow exception because it contains a circular reference to a field Point class called 'center'. mapEntity method recursively calls itself and has no way to avoid infinite loops. Commited a test with dummy object CircularObject that contains a reference to itself as a field. First commit was to build a list of previously visited classes. Second commit was to change this to be an property on the Field annotation with idea that users can specify the fields that should be ignored when mapping the objects. This doesn't stop lazy users being stung but should be more configurable.
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||
|
||||
/**
|
||||
* @author Stuart Stevenson
|
||||
*/
|
||||
@Document(indexName = "circular-objects", type = "circular-object" , indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
public class CircularObject {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
@Field(type = FieldType.Object, ignoreFields = {"circularObject"})
|
||||
private CircularObject circularObject;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Stuart Stevenson
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
|
||||
public class MappingBuilderTests {
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Test
|
||||
public void shouldNotFailOnCircularReference() {
|
||||
elasticsearchTemplate.createIndex(CircularObject.class);
|
||||
elasticsearchTemplate.putMapping(CircularObject.class);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user