diff --git a/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java b/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java index 0ae0e8928..606c5b945 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java @@ -85,6 +85,7 @@ * @author Anton Naydenov * @author vdisk * @author Junghoon Ban + * @author llosimura * @since 3.2 */ public class MappingElasticsearchConverter @@ -653,13 +654,14 @@ private void populateScriptFields(ElasticsearchPersistentEntity entity, T SearchDocument searchDocument) { Map> fields = searchDocument.getFields(); entity.doWithProperties((SimplePropertyHandler) property -> { - if (property.isAnnotationPresent(ScriptedField.class) && fields.containsKey(property.getName())) { + if (property.isAnnotationPresent(ScriptedField.class)) { ScriptedField scriptedField = property.findAnnotation(ScriptedField.class); // noinspection ConstantConditions String name = scriptedField.name().isEmpty() ? property.getName() : scriptedField.name(); - Object value = searchDocument.getFieldValue(name); - - entity.getPropertyAccessor(result).setProperty(property, value); + if (fields.containsKey(name)) { + Object value = searchDocument.getFieldValue(name); + entity.getPropertyAccessor(result).setProperty(property, value); + } } }); } diff --git a/src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java b/src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java index be1ba7b0c..b2575f6ad 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java @@ -49,8 +49,10 @@ import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; import org.springframework.data.elasticsearch.annotations.GeoPointField; +import org.springframework.data.elasticsearch.annotations.ScriptedField; import org.springframework.data.elasticsearch.annotations.ValueConverter; import org.springframework.data.elasticsearch.core.document.Document; +import org.springframework.data.elasticsearch.core.document.SearchDocumentAdapter; import org.springframework.data.elasticsearch.core.geo.GeoJsonEntity; import org.springframework.data.elasticsearch.core.geo.GeoJsonGeometryCollection; import org.springframework.data.elasticsearch.core.geo.GeoJsonLineString; @@ -83,6 +85,7 @@ * @author Konrad Kurdej * @author Roman Puchkovskiy * @author Sascha Woo + * @author llosimura */ public class MappingElasticsearchConverterUnitTests { @@ -1800,6 +1803,68 @@ void shouldReadASingleStringIntoAListPropertyImmutable() { assertThat(entity.getStringList()).containsExactly("foo"); } + @Test + void shouldPopulateScriptedFields() { + SearchDocumentAdapter document = new SearchDocumentAdapter(Document.create(), + 0.0f, + new Object[]{}, + Map.of( + "scriptedField" , List.of("scriptedField"), + "custom-name-scripted-field" , List.of("custom-name-scripted-field") + ), + emptyMap(), + emptyMap(), + null, + null, + null, + null + ); + // Create a SearchDocument instance + var entity = mappingElasticsearchConverter.read(ScriptedEntity.class, document); + assertThat(entity.customScriptedField).isEqualTo("custom-name-scripted-field"); + assertThat(entity.scriptedField).isEqualTo("scriptedField"); + } + + static class ScriptedEntity { + @ScriptedField + private String scriptedField; + @ScriptedField(name = "custom-name-scripted-field") String customScriptedField; + + ScriptedEntity() { + customScriptedField = ""; + scriptedField = ""; + } + + public String getScriptedField() { + return scriptedField; + } + + public void setScriptedField(String scriptedField) { + this.scriptedField = scriptedField; + } + + public String getCustomScriptedField() { + return customScriptedField; + } + + public void setCustomScriptedField(String customScriptedField) { + this.customScriptedField = customScriptedField; + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) return false; + ScriptedEntity that = (ScriptedEntity) o; + return Objects.equals(scriptedField, that.scriptedField) && Objects.equals(customScriptedField, that.customScriptedField); + } + + @Override + public int hashCode() { + return Objects.hash(scriptedField, customScriptedField); + } + } + + @Test // #2280 @DisplayName("should read a String array into a List property immutable") void shouldReadAStringArrayIntoAListPropertyImmutable() {