mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-08 05:02:11 +00:00
ValueConverters can be derived from AbstractPropertyValueConverter.
Original Pull Request #2490 Closes #2489
This commit is contained in:
parent
ec77b3a082
commit
ade90328d3
@ -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) {
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user