fix: use scripted field name to populate entity.

Original Pull Request: #3023
Closes: #3022
This commit is contained in:
Alfonso 2024-12-14 09:04:06 -08:00 committed by GitHub
parent 5f297f1dc3
commit 944e7e81dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 71 additions and 4 deletions

View File

@ -85,6 +85,7 @@ import org.springframework.util.ObjectUtils;
* @author Anton Naydenov
* @author vdisk
* @author Junghoon Ban
* @author llosimura
* @since 3.2
*/
public class MappingElasticsearchConverter
@ -653,14 +654,15 @@ public class MappingElasticsearchConverter
SearchDocument searchDocument) {
Map<String, List<Object>> 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();
if (fields.containsKey(name)) {
Object value = searchDocument.getFieldValue(name);
entity.getPropertyAccessor(result).setProperty(property, value);
}
}
});
}

View File

@ -49,8 +49,10 @@ import org.springframework.data.elasticsearch.annotations.DateFormat;
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 @@ import org.springframework.util.Assert;
* @author Konrad Kurdej
* @author Roman Puchkovskiy
* @author Sascha Woo
* @author llosimura
*/
public class MappingElasticsearchConverterUnitTests {
@ -1800,6 +1803,68 @@ public class MappingElasticsearchConverterUnitTests {
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() {