Skip to content

Commit be9a34c

Browse files
author
Jakub Vavrik
committed
Added test for infinite recursion mapping.
1 parent 680e077 commit be9a34c

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch;
17+
18+
import org.springframework.data.annotation.Id;
19+
import org.springframework.data.elasticsearch.annotations.Document;
20+
import org.springframework.data.elasticsearch.annotations.Field;
21+
22+
import static org.springframework.data.elasticsearch.annotations.FieldIndex.not_analyzed;
23+
import static org.springframework.data.elasticsearch.annotations.FieldType.String;
24+
25+
/**
26+
* @author Jakub Vavrik
27+
*/
28+
@Document(indexName = "test-recursive-mapping", type = "mapping", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
29+
public class SampleRecursiveMappingEntity {
30+
31+
@Id
32+
private String id;
33+
34+
@Field(type = String, index = not_analyzed, store = true, searchAnalyzer = "standard", indexAnalyzer = "standard")
35+
private String message;
36+
37+
@Field
38+
private NestedEntity nested;
39+
40+
public String getId() {
41+
return id;
42+
}
43+
44+
public void setId(String id) {
45+
this.id = id;
46+
}
47+
48+
public String getMessage() {
49+
return message;
50+
}
51+
52+
public void setMessage(String message) {
53+
this.message = message;
54+
}
55+
56+
static class NestedEntity {
57+
@Field
58+
private static NestedEntity someField = new NestedEntity();
59+
@Field
60+
private Boolean something;
61+
62+
public NestedEntity getSomeField() {
63+
return someField;
64+
}
65+
66+
public void setSomeField(NestedEntity someField) {
67+
this.someField = someField;
68+
}
69+
70+
public Boolean getSomething() { return something; }
71+
72+
public void setSomething(Boolean something) { this.something = something; }
73+
}
74+
75+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.springframework.data.elasticsearch.core;
2+
3+
import junit.framework.Assert;
4+
import org.elasticsearch.common.xcontent.XContentBuilder;
5+
import org.junit.Test;
6+
import org.springframework.data.elasticsearch.SampleRecursiveMappingEntity;
7+
8+
import java.io.IOException;
9+
10+
/**
11+
* Test that classes that have fields of same type do not end in infinite loop when mapping.
12+
*/
13+
public class SimpleRecursiveMappingTest {
14+
15+
private static final String EXPECTED = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true,\"" +
16+
"type\":\"string\",\"index\":\"not_analyzed\",\"search_analyzer\":\"standard\"," +
17+
"\"index_analyzer\":\"standard\"},\"nested\":{\"type\":\"object\",\"properties\":{\"" +
18+
"something\":{\"store\":false}}},\"nested\":{\"store\":false}}}}";
19+
20+
@Test
21+
public void testInfiniteLoopAvoidance() throws IOException {
22+
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleRecursiveMappingEntity.class, "mapping", "id");
23+
Assert.assertEquals(EXPECTED, xContentBuilder.string());
24+
}
25+
}

0 commit comments

Comments
 (0)