Skip to content

Commit 3330d65

Browse files
authored
Fix converting of Range<?> in Lists.
Original Pull Request #2707 Closes #2706
1 parent bbfb6ff commit 3330d65

File tree

7 files changed

+64
-22
lines changed

7 files changed

+64
-22
lines changed

src/main/java/org/springframework/data/elasticsearch/core/convert/AbstractRangePropertyValueConverter.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,19 @@ public abstract class AbstractRangePropertyValueConverter<T> extends AbstractPro
3232
protected static final String LTE_FIELD = "lte";
3333
protected static final String GT_FIELD = "gt";
3434
protected static final String GTE_FIELD = "gte";
35+
private final Class<?> genericType;
3536

36-
public AbstractRangePropertyValueConverter(PersistentProperty<?> property) {
37+
/**
38+
* @param property the property this convertrer belongs to
39+
* @param genericType the generic type of the Range
40+
*/
41+
public AbstractRangePropertyValueConverter(PersistentProperty<?> property, Class<?> genericType) {
3742
super(property);
43+
this.genericType = genericType;
44+
}
45+
46+
public Class<?> getGenericType() {
47+
return genericType;
3848
}
3949

4050
@Override
@@ -117,10 +127,6 @@ public Object write(Object value) {
117127

118128
protected abstract String format(T value);
119129

120-
protected Class<?> getGenericType() {
121-
return getProperty().getTypeInformation().getTypeArguments().get(0).getType();
122-
}
123-
124130
protected abstract T parse(String value);
125131

126132
}

src/main/java/org/springframework/data/elasticsearch/core/convert/DateRangePropertyValueConverter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public class DateRangePropertyValueConverter extends AbstractRangePropertyValueC
3333
private final List<ElasticsearchDateConverter> dateConverters;
3434

3535
public DateRangePropertyValueConverter(PersistentProperty<?> property,
36-
List<ElasticsearchDateConverter> dateConverters) {
36+
Class<?> genericType, List<ElasticsearchDateConverter> dateConverters) {
3737

38-
super(property);
38+
super(property, genericType);
3939
this.dateConverters = dateConverters;
4040
}
4141

src/main/java/org/springframework/data/elasticsearch/core/convert/NumberRangePropertyValueConverter.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@
2323
*/
2424
public class NumberRangePropertyValueConverter extends AbstractRangePropertyValueConverter<Number> {
2525

26-
public NumberRangePropertyValueConverter(PersistentProperty<?> property) {
27-
super(property);
26+
/**
27+
* @param property the property this convertrer belongs to
28+
* @param genericType the generic type of the Range
29+
*/
30+
public NumberRangePropertyValueConverter(PersistentProperty<?> property, Class<?> genericType) {
31+
super(property, genericType);
2832
}
2933

3034
@Override

src/main/java/org/springframework/data/elasticsearch/core/convert/TemporalRangePropertyValueConverter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public class TemporalRangePropertyValueConverter extends AbstractRangePropertyVa
3434
private final List<ElasticsearchDateConverter> dateConverters;
3535

3636
public TemporalRangePropertyValueConverter(PersistentProperty<?> property,
37-
List<ElasticsearchDateConverter> dateConverters) {
37+
Class<?> genericType, List<ElasticsearchDateConverter> dateConverters) {
3838

39-
super(property);
39+
super(property, genericType);
4040

4141
Assert.notEmpty(dateConverters, "dateConverters must not be empty.");
4242
this.dateConverters = dateConverters;

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

+19-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Arrays;
2121
import java.util.Date;
2222
import java.util.List;
23+
import java.util.function.Supplier;
2324

2425
import org.apache.commons.logging.Log;
2526
import org.apache.commons.logging.LogFactory;
@@ -56,6 +57,7 @@
5657
import org.springframework.data.mapping.model.Property;
5758
import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
5859
import org.springframework.data.mapping.model.SimpleTypeHolder;
60+
import org.springframework.data.util.TypeInformation;
5961
import org.springframework.lang.Nullable;
6062
import org.springframework.util.StringUtils;
6163

@@ -168,6 +170,18 @@ private void initPropertyValueConverter() {
168170
return;
169171
}
170172

173+
Supplier<Class<?>> getGenericType = () -> {
174+
TypeInformation<?> typeInformation = getTypeInformation();
175+
176+
if (typeInformation.isCollectionLike()) {
177+
// we have a collection of Range<?>
178+
typeInformation = typeInformation.getComponentType();
179+
}
180+
181+
Class<?> genericType = typeInformation.getTypeArguments().get(0).getType();
182+
return genericType;
183+
};
184+
171185
switch (field.type()) {
172186
case Date:
173187
case Date_Nanos: {
@@ -197,11 +211,11 @@ private void initPropertyValueConverter() {
197211
return;
198212
}
199213

200-
Class<?> genericType = getTypeInformation().getTypeArguments().get(0).getType();
214+
var genericType = getGenericType.get();
201215
if (TemporalAccessor.class.isAssignableFrom(genericType)) {
202-
propertyValueConverter = new TemporalRangePropertyValueConverter(this, dateConverters);
216+
propertyValueConverter = new TemporalRangePropertyValueConverter(this, genericType, dateConverters);
203217
} else if (Date.class.isAssignableFrom(genericType)) {
204-
propertyValueConverter = new DateRangePropertyValueConverter(this, dateConverters);
218+
propertyValueConverter = new DateRangePropertyValueConverter(this, genericType, dateConverters);
205219
} else {
206220
LOGGER.warn(
207221
String.format("Unsupported generic type '{%s' for date range property '%s'.", genericType, getName()));
@@ -216,7 +230,7 @@ private void initPropertyValueConverter() {
216230
return;
217231
}
218232

219-
Class<?> genericType = getTypeInformation().getTypeArguments().get(0).getType();
233+
var genericType = getGenericType.get();
220234
if ((field.type() == FieldType.Integer_Range && !Integer.class.isAssignableFrom(genericType))
221235
|| (field.type() == FieldType.Float_Range && !Float.class.isAssignableFrom(genericType))
222236
|| (field.type() == FieldType.Long_Range && !Long.class.isAssignableFrom(genericType))
@@ -226,7 +240,7 @@ private void initPropertyValueConverter() {
226240
return;
227241
}
228242

229-
propertyValueConverter = new NumberRangePropertyValueConverter(this);
243+
propertyValueConverter = new NumberRangePropertyValueConverter(this, genericType);
230244
break;
231245
}
232246
case Ip_Range: {

src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,13 @@ class RangeTests {
990990
"gte": "2021-01-01T00:30:00.000+02:00",
991991
"lte": "2021-01-01T00:30:00.000+02:00"
992992
},
993-
"nullRange": null
993+
"nullRange": null,
994+
"integerRangeList": [
995+
{
996+
"gte": "2",
997+
"lte": "5"
998+
}
999+
]
9941000
}
9951001
""";
9961002

@@ -1020,6 +1026,7 @@ public void shouldReadRanges() throws JSONException {
10201026
assertThat(e.getZonedDateTimeRange()).isEqualTo(
10211027
Range.just(ZonedDateTime.of(LocalDate.of(2021, 1, 1), LocalTime.of(0, 30), ZoneOffset.ofHours(2))));
10221028
assertThat(e.getNullRange()).isNull();
1029+
assertThat(e.getIntegerRangeList()).containsExactly(Range.closed(2, 5));
10231030
});
10241031
}
10251032

@@ -1042,7 +1049,7 @@ public void shouldWriteRanges() throws JSONException {
10421049
entity.setZonedDateTimeRange(
10431050
Range.just(ZonedDateTime.of(LocalDate.of(2021, 1, 1), LocalTime.of(0, 30), ZoneOffset.ofHours(2))));
10441051
entity.setNullRange(null);
1045-
1052+
entity.setIntegerRangeList(List.of(Range.closed(2, 5)));
10461053
Document document = mappingElasticsearchConverter.mapObject(entity);
10471054

10481055
assertThat(document).isEqualTo(source);
@@ -1066,6 +1073,8 @@ class RangeEntity {
10661073
@Field(type = FieldType.Date_Range) private Range<ZonedDateTime> zonedDateTimeRange;
10671074
@Field(type = FieldType.Date_Range, storeNullValue = true) private Range<ZonedDateTime> nullRange;
10681075

1076+
@Field(type = FieldType.Integer_Range) private List<Range<Integer>> integerRangeList;
1077+
10691078
public String getId() {
10701079
return id;
10711080
}
@@ -1162,6 +1171,13 @@ public void setNullRange(Range<ZonedDateTime> nullRange) {
11621171
this.nullRange = nullRange;
11631172
}
11641173

1174+
public List<Range<Integer>> getIntegerRangeList() {
1175+
return integerRangeList;
1176+
}
1177+
1178+
public void setIntegerRangeList(List<Range<Integer>> integerRangeList) {
1179+
this.integerRangeList = integerRangeList;
1180+
}
11651181
}
11661182
}
11671183

@@ -2055,7 +2071,8 @@ void shouldReadEntityWithDottedFieldName() {
20552071
void shouldMapPropertyPathToFieldNames() {
20562072

20572073
var propertyPath = "level1Entries.level2Entries.keyWord";
2058-
ElasticsearchPersistentEntity<?> persistentEntity = mappingElasticsearchConverter.getMappingContext().getPersistentEntity(NestedEntity.class);
2074+
ElasticsearchPersistentEntity<?> persistentEntity = mappingElasticsearchConverter.getMappingContext()
2075+
.getPersistentEntity(NestedEntity.class);
20592076
var mappedNames = mappingElasticsearchConverter.updateFieldNames(propertyPath, persistentEntity);
20602077

20612078
assertThat(mappedNames).isEqualTo("level-one.level-two.key-word");

src/test/java/org/springframework/data/elasticsearch/core/convert/PropertyValueConvertersUnitTests.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@ static Stream<Arguments> propertyValueConverters() {
6363

6464
converters.add(new DatePropertyValueConverter(persistentProperty,
6565
Collections.singletonList(ElasticsearchDateConverter.of(DateFormat.basic_date))));
66+
Class<?> genericType = Object.class;
6667
converters.add(new DateRangePropertyValueConverter(persistentProperty,
67-
Collections.singletonList(ElasticsearchDateConverter.of(DateFormat.basic_date))));
68-
converters.add(new NumberRangePropertyValueConverter(persistentProperty));
68+
genericType, Collections.singletonList(ElasticsearchDateConverter.of(DateFormat.basic_date))));
69+
converters.add(new NumberRangePropertyValueConverter(persistentProperty, genericType));
6970
converters.add(new TemporalPropertyValueConverter(persistentProperty,
7071
Collections.singletonList(ElasticsearchDateConverter.of(DateFormat.basic_date))));
7172
converters.add(new TemporalRangePropertyValueConverter(persistentProperty,
72-
Collections.singletonList(ElasticsearchDateConverter.of(DateFormat.basic_date))));
73+
genericType, Collections.singletonList(ElasticsearchDateConverter.of(DateFormat.basic_date))));
7374

7475
return converters.stream().map(propertyValueConverter -> arguments(
7576
Named.of(propertyValueConverter.getClass().getSimpleName(), propertyValueConverter)));

0 commit comments

Comments
 (0)