ValueConverters can be derived from AbstractPropertyValueConverter.

Original Pull Request #2490
Closes #2489
This commit is contained in:
Peter-Josef Meisch 2023-03-05 20:54:20 +01:00 committed by GitHub
parent ec77b3a082
commit ade90328d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 2 deletions

View File

@ -35,6 +35,7 @@ import org.springframework.data.elasticsearch.annotations.IndexedIndexName;
import org.springframework.data.elasticsearch.annotations.MultiField;
import org.springframework.data.elasticsearch.annotations.ValueConverter;
import org.springframework.data.elasticsearch.annotations.WriteOnlyProperty;
import org.springframework.data.elasticsearch.core.convert.AbstractPropertyValueConverter;
import org.springframework.data.elasticsearch.core.convert.DatePropertyValueConverter;
import org.springframework.data.elasticsearch.core.convert.DateRangePropertyValueConverter;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchDateConverter;
@ -249,11 +250,15 @@ public class SimpleElasticsearchPersistentProperty extends
throw new IllegalArgumentException(clazz + " is an enum with more than 1 constant and cannot be used here");
}
propertyValueConverter = enumConstants[0];
} else {
if (AbstractPropertyValueConverter.class.isAssignableFrom(clazz)) {
propertyValueConverter = BeanUtils.instantiateClass(BeanUtils.getResolvableConstructor(clazz), this);
} else {
propertyValueConverter = BeanUtils.instantiateClass(clazz);
}
}
}
}
private List<ElasticsearchDateConverter> getDateConverters(Field field, Class<?> actualType) {

View File

@ -34,8 +34,10 @@ import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.InnerField;
import org.springframework.data.elasticsearch.annotations.MultiField;
import org.springframework.data.elasticsearch.annotations.ValueConverter;
import org.springframework.data.elasticsearch.core.convert.AbstractPropertyValueConverter;
import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.model.FieldNamingStrategy;
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
@ -258,6 +260,8 @@ public class SimpleElasticsearchPersistentPropertyUnitTests {
assertThat(
persistentEntity.getRequiredPersistentProperty("fieldWithClassBasedConverter").getPropertyValueConverter())
.isInstanceOf(ClassBasedValueConverter.class);
assertThat(persistentEntity.getRequiredPersistentProperty("fieldWithClassBasedDerivedFromAbstractValueConverter")
.getPropertyValueConverter()).isInstanceOf(ClassBasedDerivedFromAbstractValueConverter.class);
assertThat(
persistentEntity.getRequiredPersistentProperty("fieldWithEnumBasedConverter").getPropertyValueConverter())
.isInstanceOf(EnumBasedValueConverter.class);
@ -354,6 +358,8 @@ public class SimpleElasticsearchPersistentPropertyUnitTests {
@Nullable
@ValueConverter(ClassBasedValueConverter.class) private String fieldWithClassBasedConverter;
@Nullable
@ValueConverter(ClassBasedDerivedFromAbstractValueConverter.class) private String fieldWithClassBasedDerivedFromAbstractValueConverter;
@Nullable
@ValueConverter(EnumBasedValueConverter.class) private String fieldWithEnumBasedConverter;
}
@ -370,6 +376,23 @@ public class SimpleElasticsearchPersistentPropertyUnitTests {
}
}
private static class ClassBasedDerivedFromAbstractValueConverter extends AbstractPropertyValueConverter {
public ClassBasedDerivedFromAbstractValueConverter(PersistentProperty<?> property) {
super(property);
}
@Override
public Object write(Object value) {
return value;
}
@Override
public Object read(Object value) {
return value;
}
}
private enum EnumBasedValueConverter implements PropertyValueConverter {
INSTANCE;

View File

@ -39,7 +39,7 @@ import org.springframework.lang.Nullable;
/**
* Integration tests to check that {@link org.springframework.data.elasticsearch.annotations.ValueConverter} annotated
* properties are handle correctly (method name derived queries, for
* properties are handled correctly (method name derived queries, for
*
* @{@link org.springframework.data.elasticsearch.core.query.Query} methods we don't know which parameters map to which
* property.