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.MultiField;
import org.springframework.data.elasticsearch.annotations.ValueConverter; import org.springframework.data.elasticsearch.annotations.ValueConverter;
import org.springframework.data.elasticsearch.annotations.WriteOnlyProperty; 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.DatePropertyValueConverter;
import org.springframework.data.elasticsearch.core.convert.DateRangePropertyValueConverter; import org.springframework.data.elasticsearch.core.convert.DateRangePropertyValueConverter;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchDateConverter; import org.springframework.data.elasticsearch.core.convert.ElasticsearchDateConverter;
@ -250,7 +251,11 @@ public class SimpleElasticsearchPersistentProperty extends
} }
propertyValueConverter = enumConstants[0]; propertyValueConverter = enumConstants[0];
} else { } else {
propertyValueConverter = BeanUtils.instantiateClass(clazz); if (AbstractPropertyValueConverter.class.isAssignableFrom(clazz)) {
propertyValueConverter = BeanUtils.instantiateClass(BeanUtils.getResolvableConstructor(clazz), this);
} else {
propertyValueConverter = BeanUtils.instantiateClass(clazz);
}
} }
} }
} }

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.InnerField;
import org.springframework.data.elasticsearch.annotations.MultiField; import org.springframework.data.elasticsearch.annotations.MultiField;
import org.springframework.data.elasticsearch.annotations.ValueConverter; 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.elasticsearch.core.query.SeqNoPrimaryTerm;
import org.springframework.data.mapping.MappingException; 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.FieldNamingStrategy;
import org.springframework.data.mapping.model.Property; import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy; import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
@ -258,6 +260,8 @@ public class SimpleElasticsearchPersistentPropertyUnitTests {
assertThat( assertThat(
persistentEntity.getRequiredPersistentProperty("fieldWithClassBasedConverter").getPropertyValueConverter()) persistentEntity.getRequiredPersistentProperty("fieldWithClassBasedConverter").getPropertyValueConverter())
.isInstanceOf(ClassBasedValueConverter.class); .isInstanceOf(ClassBasedValueConverter.class);
assertThat(persistentEntity.getRequiredPersistentProperty("fieldWithClassBasedDerivedFromAbstractValueConverter")
.getPropertyValueConverter()).isInstanceOf(ClassBasedDerivedFromAbstractValueConverter.class);
assertThat( assertThat(
persistentEntity.getRequiredPersistentProperty("fieldWithEnumBasedConverter").getPropertyValueConverter()) persistentEntity.getRequiredPersistentProperty("fieldWithEnumBasedConverter").getPropertyValueConverter())
.isInstanceOf(EnumBasedValueConverter.class); .isInstanceOf(EnumBasedValueConverter.class);
@ -354,6 +358,8 @@ public class SimpleElasticsearchPersistentPropertyUnitTests {
@Nullable @Nullable
@ValueConverter(ClassBasedValueConverter.class) private String fieldWithClassBasedConverter; @ValueConverter(ClassBasedValueConverter.class) private String fieldWithClassBasedConverter;
@Nullable @Nullable
@ValueConverter(ClassBasedDerivedFromAbstractValueConverter.class) private String fieldWithClassBasedDerivedFromAbstractValueConverter;
@Nullable
@ValueConverter(EnumBasedValueConverter.class) private String fieldWithEnumBasedConverter; @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 { private enum EnumBasedValueConverter implements PropertyValueConverter {
INSTANCE; 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 * 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 * @{@link org.springframework.data.elasticsearch.core.query.Query} methods we don't know which parameters map to which
* property. * property.