Skip to content

Commit 944e7e8

Browse files
authored
fix: use scripted field name to populate entity.
Original Pull Request: spring-projects#3023 Closes: spring-projects#3022
1 parent 5f297f1 commit 944e7e8

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

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

+6-4
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

+65
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@
4949
import org.springframework.data.elasticsearch.annotations.Field;
5050
import org.springframework.data.elasticsearch.annotations.FieldType;
5151
import org.springframework.data.elasticsearch.annotations.GeoPointField;
52+
import org.springframework.data.elasticsearch.annotations.ScriptedField;
5253
import org.springframework.data.elasticsearch.annotations.ValueConverter;
5354
import org.springframework.data.elasticsearch.core.document.Document;
55+
import org.springframework.data.elasticsearch.core.document.SearchDocumentAdapter;
5456
import org.springframework.data.elasticsearch.core.geo.GeoJsonEntity;
5557
import org.springframework.data.elasticsearch.core.geo.GeoJsonGeometryCollection;
5658
import org.springframework.data.elasticsearch.core.geo.GeoJsonLineString;
@@ -83,6 +85,7 @@
8385
* @author Konrad Kurdej
8486
* @author Roman Puchkovskiy
8587
* @author Sascha Woo
88+
* @author llosimura
8689
*/
8790
public class MappingElasticsearchConverterUnitTests {
8891

@@ -1800,6 +1803,68 @@ void shouldReadASingleStringIntoAListPropertyImmutable() {
18001803
assertThat(entity.getStringList()).containsExactly("foo");
18011804
}
18021805

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

0 commit comments

Comments
 (0)