Skip to content

Commit faae21f

Browse files
committed
fix: use scripted field name to populate entity
1 parent 5f297f1 commit faae21f

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
* @author Anton Naydenov
8686
* @author vdisk
8787
* @author Junghoon Ban
88+
* @author llosimura
8889
* @since 3.2
8990
*/
9091
public class MappingElasticsearchConverter
@@ -653,13 +654,14 @@ private <T> void populateScriptFields(ElasticsearchPersistentEntity<?> entity, T
653654
SearchDocument searchDocument) {
654655
Map<String, List<Object>> fields = searchDocument.getFields();
655656
entity.doWithProperties((SimplePropertyHandler) property -> {
656-
if (property.isAnnotationPresent(ScriptedField.class) && fields.containsKey(property.getName())) {
657+
if (property.isAnnotationPresent(ScriptedField.class)) {
657658
ScriptedField scriptedField = property.findAnnotation(ScriptedField.class);
658659
// noinspection ConstantConditions
659660
String name = scriptedField.name().isEmpty() ? property.getName() : scriptedField.name();
660-
Object value = searchDocument.getFieldValue(name);
661-
662-
entity.getPropertyAccessor(result).setProperty(property, value);
661+
if (fields.containsKey(name)) {
662+
Object value = searchDocument.getFieldValue(name);
663+
entity.getPropertyAccessor(result).setProperty(property, value);
664+
}
663665
}
664666
});
665667
}

src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,9 @@
4545
import org.springframework.data.convert.ReadingConverter;
4646
import org.springframework.data.convert.WritingConverter;
4747
import org.springframework.data.domain.Range;
48-
import org.springframework.data.elasticsearch.annotations.DateFormat;
49-
import org.springframework.data.elasticsearch.annotations.Field;
50-
import org.springframework.data.elasticsearch.annotations.FieldType;
51-
import org.springframework.data.elasticsearch.annotations.GeoPointField;
52-
import org.springframework.data.elasticsearch.annotations.ValueConverter;
48+
import org.springframework.data.elasticsearch.annotations.*;
5349
import org.springframework.data.elasticsearch.core.document.Document;
50+
import org.springframework.data.elasticsearch.core.document.SearchDocumentAdapter;
5451
import org.springframework.data.elasticsearch.core.geo.GeoJsonEntity;
5552
import org.springframework.data.elasticsearch.core.geo.GeoJsonGeometryCollection;
5653
import org.springframework.data.elasticsearch.core.geo.GeoJsonLineString;
@@ -83,6 +80,7 @@
8380
* @author Konrad Kurdej
8481
* @author Roman Puchkovskiy
8582
* @author Sascha Woo
83+
* @author llosimura
8684
*/
8785
public class MappingElasticsearchConverterUnitTests {
8886

@@ -1800,6 +1798,68 @@ void shouldReadASingleStringIntoAListPropertyImmutable() {
18001798
assertThat(entity.getStringList()).containsExactly("foo");
18011799
}
18021800

1801+
@Test
1802+
void shouldPopulateScriptedFields() {
1803+
SearchDocumentAdapter document = new SearchDocumentAdapter(Document.create(),
1804+
0.0f,
1805+
new Object[]{},
1806+
Map.of(
1807+
"scriptedField" , List.of("scriptedField"),
1808+
"custom-name-scripted-field" , List.of("custom-name-scripted-field")
1809+
),
1810+
emptyMap(),
1811+
emptyMap(),
1812+
null,
1813+
null,
1814+
null,
1815+
null
1816+
);
1817+
// Create a SearchDocument instance
1818+
var entity = mappingElasticsearchConverter.read(ScriptedEntity.class, document);
1819+
assertThat(entity.customScriptedField).isEqualTo("custom-name-scripted-field");
1820+
assertThat(entity.scriptedField).isEqualTo("scriptedField");
1821+
}
1822+
1823+
static class ScriptedEntity {
1824+
@ScriptedField
1825+
private String scriptedField;
1826+
@ScriptedField(name = "custom-name-scripted-field") String customScriptedField;
1827+
1828+
ScriptedEntity() {
1829+
customScriptedField = "";
1830+
scriptedField = "";
1831+
}
1832+
1833+
public String getScriptedField() {
1834+
return scriptedField;
1835+
}
1836+
1837+
public void setScriptedField(String scriptedField) {
1838+
this.scriptedField = scriptedField;
1839+
}
1840+
1841+
public String getCustomScriptedField() {
1842+
return customScriptedField;
1843+
}
1844+
1845+
public void setCustomScriptedField(String customScriptedField) {
1846+
this.customScriptedField = customScriptedField;
1847+
}
1848+
1849+
@Override
1850+
public boolean equals(Object o) {
1851+
if (o == null || getClass() != o.getClass()) return false;
1852+
ScriptedEntity that = (ScriptedEntity) o;
1853+
return Objects.equals(scriptedField, that.scriptedField) && Objects.equals(customScriptedField, that.customScriptedField);
1854+
}
1855+
1856+
@Override
1857+
public int hashCode() {
1858+
return Objects.hash(scriptedField, customScriptedField);
1859+
}
1860+
}
1861+
1862+
18031863
@Test // #2280
18041864
@DisplayName("should read a String array into a List property immutable")
18051865
void shouldReadAStringArrayIntoAListPropertyImmutable() {

0 commit comments

Comments
 (0)