Skip to content

Commit 32fa739

Browse files
authored
Fix exception on property conversion with criteria exists/empty/non-empty.
Original Pull Request #2081 Closes #2080
1 parent e54c03d commit 32fa739

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,14 +1248,19 @@ private void updateCriteria(Criteria criteria, ElasticsearchPersistentEntity<?>
12481248
PropertyValueConverter propertyValueConverter = Objects
12491249
.requireNonNull(persistentProperty.getPropertyValueConverter());
12501250
criteria.getQueryCriteriaEntries().forEach(criteriaEntry -> {
1251-
Object value = criteriaEntry.getValue();
1252-
if (value.getClass().isArray()) {
1253-
Object[] objects = (Object[]) value;
1254-
for (int i = 0; i < objects.length; i++) {
1255-
objects[i] = propertyValueConverter.write(objects[i]);
1251+
1252+
if (criteriaEntry.getKey().hasValue()) {
1253+
Object value = criteriaEntry.getValue();
1254+
1255+
if (value.getClass().isArray()) {
1256+
Object[] objects = (Object[]) value;
1257+
1258+
for (int i = 0; i < objects.length; i++) {
1259+
objects[i] = propertyValueConverter.write(objects[i]);
1260+
}
1261+
} else {
1262+
criteriaEntry.setValue(propertyValueConverter.write(value));
12561263
}
1257-
} else {
1258-
criteriaEntry.setValue(propertyValueConverter.write(value));
12591264
}
12601265
});
12611266
}

src/main/java/org/springframework/data/elasticsearch/core/query/Criteria.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,23 @@ public enum OperationKey { //
954954
/**
955955
* @since 4.3
956956
*/
957-
NOT_EMPTY
957+
NOT_EMPTY;
958+
959+
/**
960+
* @return true if this key does not have an associated value
961+
* @since 4.4
962+
*/
963+
public boolean hasNoValue() {
964+
return this == OperationKey.EXISTS || this == OperationKey.EMPTY || this == OperationKey.NOT_EMPTY;
965+
}
966+
967+
/**
968+
* @return true if this key does have an associated value
969+
* @since 4.4
970+
*/
971+
public boolean hasValue() {
972+
return !hasNoValue();
973+
}
958974
}
959975

960976
/**
@@ -967,9 +983,8 @@ public static class CriteriaEntry {
967983

968984
protected CriteriaEntry(OperationKey key) {
969985

970-
boolean keyIsValid = key == OperationKey.EXISTS || key == OperationKey.EMPTY || key == OperationKey.NOT_EMPTY;
971-
Assert.isTrue(keyIsValid,
972-
"key must be OperationKey.EXISTS, OperationKey.EMPTY or OperationKey.EMPTY for this call");
986+
Assert.isTrue(key.hasNoValue(),
987+
"key must be OperationKey.EXISTS, OperationKey.EMPTY or OperationKey.NOT_EMPTY for this call");
973988

974989
this.key = key;
975990
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
6767
import org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter;
6868
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
69+
import org.springframework.data.elasticsearch.core.query.Criteria;
70+
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
71+
import org.springframework.data.elasticsearch.core.query.Query;
6972
import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
7073
import org.springframework.data.geo.Box;
7174
import org.springframework.data.geo.Circle;
@@ -1496,6 +1499,16 @@ void shouldReadUsingValueConverters() throws JSONException {
14961499
assertThat(entity.getDontConvert()).isEqualTo("Monty Python's Flying Circus");
14971500
}
14981501

1502+
@Test // #2080
1503+
@DisplayName("should not try to call property converter on updating criteria exists")
1504+
void shouldNotTryToCallPropertyConverterOnUpdatingCriteriaExists() {
1505+
1506+
// don't care if the query makes no sense, we just add all criteria without values
1507+
Query query = new CriteriaQuery(Criteria.where("fieldWithClassBasedConverter").exists().empty().notEmpty());
1508+
1509+
mappingElasticsearchConverter.updateQuery(query, EntityWithCustomValueConverters.class);
1510+
}
1511+
14991512
private Map<String, Object> writeToMap(Object source) {
15001513

15011514
Document sink = Document.create();
@@ -2449,12 +2462,12 @@ public Object read(Object value) {
24492462
return reverse(value);
24502463
}
24512464
}
2465+
// endregion
24522466

24532467
private static String reverse(Object o) {
24542468

24552469
Assert.notNull(o, "o must not be null");
24562470

24572471
return new StringBuilder().append(o.toString()).reverse().toString();
24582472
}
2459-
// endregion
24602473
}

0 commit comments

Comments
 (0)