Skip to content

Commit f66af53

Browse files
committed
Fix exception on property conversion with criteria exists/empty/non-empty.
Original Pull Request #2081 Closes #2080 (cherry picked from commit 32fa739)
1 parent 1409997 commit f66af53

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
@@ -1256,14 +1256,19 @@ private void updateCriteria(Criteria criteria, ElasticsearchPersistentEntity<?>
12561256
PropertyValueConverter propertyValueConverter = Objects
12571257
.requireNonNull(persistentProperty.getPropertyValueConverter());
12581258
criteria.getQueryCriteriaEntries().forEach(criteriaEntry -> {
1259-
Object value = criteriaEntry.getValue();
1260-
if (value.getClass().isArray()) {
1261-
Object[] objects = (Object[]) value;
1262-
for (int i = 0; i < objects.length; i++) {
1263-
objects[i] = propertyValueConverter.write(objects[i]);
1259+
1260+
if (criteriaEntry.getKey().hasValue()) {
1261+
Object value = criteriaEntry.getValue();
1262+
1263+
if (value.getClass().isArray()) {
1264+
Object[] objects = (Object[]) value;
1265+
1266+
for (int i = 0; i < objects.length; i++) {
1267+
objects[i] = propertyValueConverter.write(objects[i]);
1268+
}
1269+
} else {
1270+
criteriaEntry.setValue(propertyValueConverter.write(value));
12641271
}
1265-
} else {
1266-
criteriaEntry.setValue(propertyValueConverter.write(value));
12671272
}
12681273
});
12691274
}

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();
@@ -2429,12 +2442,12 @@ public Object read(Object value) {
24292442
return reverse(value);
24302443
}
24312444
}
2445+
// endregion
24322446

24332447
private static String reverse(Object o) {
24342448

24352449
Assert.notNull(o, "o must not be null");
24362450

24372451
return new StringBuilder().append(o.toString()).reverse().toString();
24382452
}
2439-
// endregion
24402453
}

0 commit comments

Comments
 (0)