Skip to content

Commit ade9032

Browse files
authored
ValueConverters can be derived from AbstractPropertyValueConverter.
Original Pull Request #2490 Closes #2489
1 parent ec77b3a commit ade9032

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.data.elasticsearch.annotations.MultiField;
3636
import org.springframework.data.elasticsearch.annotations.ValueConverter;
3737
import org.springframework.data.elasticsearch.annotations.WriteOnlyProperty;
38+
import org.springframework.data.elasticsearch.core.convert.AbstractPropertyValueConverter;
3839
import org.springframework.data.elasticsearch.core.convert.DatePropertyValueConverter;
3940
import org.springframework.data.elasticsearch.core.convert.DateRangePropertyValueConverter;
4041
import org.springframework.data.elasticsearch.core.convert.ElasticsearchDateConverter;
@@ -250,7 +251,11 @@ private void initPropertyValueConverterFromAnnotation() {
250251
}
251252
propertyValueConverter = enumConstants[0];
252253
} else {
253-
propertyValueConverter = BeanUtils.instantiateClass(clazz);
254+
if (AbstractPropertyValueConverter.class.isAssignableFrom(clazz)) {
255+
propertyValueConverter = BeanUtils.instantiateClass(BeanUtils.getResolvableConstructor(clazz), this);
256+
} else {
257+
propertyValueConverter = BeanUtils.instantiateClass(clazz);
258+
}
254259
}
255260
}
256261
}

src/test/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentPropertyUnitTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
import org.springframework.data.elasticsearch.annotations.InnerField;
3535
import org.springframework.data.elasticsearch.annotations.MultiField;
3636
import org.springframework.data.elasticsearch.annotations.ValueConverter;
37+
import org.springframework.data.elasticsearch.core.convert.AbstractPropertyValueConverter;
3738
import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
3839
import org.springframework.data.mapping.MappingException;
40+
import org.springframework.data.mapping.PersistentProperty;
3941
import org.springframework.data.mapping.model.FieldNamingStrategy;
4042
import org.springframework.data.mapping.model.Property;
4143
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
@@ -258,6 +260,8 @@ void shouldUseValueConverterAnnotation() {
258260
assertThat(
259261
persistentEntity.getRequiredPersistentProperty("fieldWithClassBasedConverter").getPropertyValueConverter())
260262
.isInstanceOf(ClassBasedValueConverter.class);
263+
assertThat(persistentEntity.getRequiredPersistentProperty("fieldWithClassBasedDerivedFromAbstractValueConverter")
264+
.getPropertyValueConverter()).isInstanceOf(ClassBasedDerivedFromAbstractValueConverter.class);
261265
assertThat(
262266
persistentEntity.getRequiredPersistentProperty("fieldWithEnumBasedConverter").getPropertyValueConverter())
263267
.isInstanceOf(EnumBasedValueConverter.class);
@@ -354,6 +358,8 @@ private static class EntityWithCustomValueConverters {
354358
@Nullable
355359
@ValueConverter(ClassBasedValueConverter.class) private String fieldWithClassBasedConverter;
356360
@Nullable
361+
@ValueConverter(ClassBasedDerivedFromAbstractValueConverter.class) private String fieldWithClassBasedDerivedFromAbstractValueConverter;
362+
@Nullable
357363
@ValueConverter(EnumBasedValueConverter.class) private String fieldWithEnumBasedConverter;
358364
}
359365

@@ -370,6 +376,23 @@ public Object read(Object value) {
370376
}
371377
}
372378

379+
private static class ClassBasedDerivedFromAbstractValueConverter extends AbstractPropertyValueConverter {
380+
381+
public ClassBasedDerivedFromAbstractValueConverter(PersistentProperty<?> property) {
382+
super(property);
383+
}
384+
385+
@Override
386+
public Object write(Object value) {
387+
return value;
388+
}
389+
390+
@Override
391+
public Object read(Object value) {
392+
return value;
393+
}
394+
}
395+
373396
private enum EnumBasedValueConverter implements PropertyValueConverter {
374397
INSTANCE;
375398

src/test/java/org/springframework/data/elasticsearch/repository/query/valueconverter/ValueConverterIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
/**
4141
* Integration tests to check that {@link org.springframework.data.elasticsearch.annotations.ValueConverter} annotated
42-
* properties are handle correctly (method name derived queries, for
42+
* properties are handled correctly (method name derived queries, for
4343
*
4444
* @{@link org.springframework.data.elasticsearch.core.query.Query} methods we don't know which parameters map to which
4545
* property.

0 commit comments

Comments
 (0)