mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-18 01:52:10 +00:00
Fix exception on property conversion with criteria exists/empty/non-empty.
Original Pull Request #2081 Closes #2080 (cherry picked from commit 32fa7391c42a64710dc073bb38cbf4611d9ac5aa)
This commit is contained in:
parent
14099970bb
commit
f66af53480
@ -1256,15 +1256,20 @@ public class MappingElasticsearchConverter
|
|||||||
PropertyValueConverter propertyValueConverter = Objects
|
PropertyValueConverter propertyValueConverter = Objects
|
||||||
.requireNonNull(persistentProperty.getPropertyValueConverter());
|
.requireNonNull(persistentProperty.getPropertyValueConverter());
|
||||||
criteria.getQueryCriteriaEntries().forEach(criteriaEntry -> {
|
criteria.getQueryCriteriaEntries().forEach(criteriaEntry -> {
|
||||||
|
|
||||||
|
if (criteriaEntry.getKey().hasValue()) {
|
||||||
Object value = criteriaEntry.getValue();
|
Object value = criteriaEntry.getValue();
|
||||||
|
|
||||||
if (value.getClass().isArray()) {
|
if (value.getClass().isArray()) {
|
||||||
Object[] objects = (Object[]) value;
|
Object[] objects = (Object[]) value;
|
||||||
|
|
||||||
for (int i = 0; i < objects.length; i++) {
|
for (int i = 0; i < objects.length; i++) {
|
||||||
objects[i] = propertyValueConverter.write(objects[i]);
|
objects[i] = propertyValueConverter.write(objects[i]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
criteriaEntry.setValue(propertyValueConverter.write(value));
|
criteriaEntry.setValue(propertyValueConverter.write(value));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -954,7 +954,23 @@ public class Criteria {
|
|||||||
/**
|
/**
|
||||||
* @since 4.3
|
* @since 4.3
|
||||||
*/
|
*/
|
||||||
NOT_EMPTY
|
NOT_EMPTY;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return true if this key does not have an associated value
|
||||||
|
* @since 4.4
|
||||||
|
*/
|
||||||
|
public boolean hasNoValue() {
|
||||||
|
return this == OperationKey.EXISTS || this == OperationKey.EMPTY || this == OperationKey.NOT_EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return true if this key does have an associated value
|
||||||
|
* @since 4.4
|
||||||
|
*/
|
||||||
|
public boolean hasValue() {
|
||||||
|
return !hasNoValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -967,9 +983,8 @@ public class Criteria {
|
|||||||
|
|
||||||
protected CriteriaEntry(OperationKey key) {
|
protected CriteriaEntry(OperationKey key) {
|
||||||
|
|
||||||
boolean keyIsValid = key == OperationKey.EXISTS || key == OperationKey.EMPTY || key == OperationKey.NOT_EMPTY;
|
Assert.isTrue(key.hasNoValue(),
|
||||||
Assert.isTrue(keyIsValid,
|
"key must be OperationKey.EXISTS, OperationKey.EMPTY or OperationKey.NOT_EMPTY for this call");
|
||||||
"key must be OperationKey.EXISTS, OperationKey.EMPTY or OperationKey.EMPTY for this call");
|
|
||||||
|
|
||||||
this.key = key;
|
this.key = key;
|
||||||
}
|
}
|
||||||
|
@ -66,6 +66,9 @@ import org.springframework.data.elasticsearch.core.geo.GeoJsonPolygon;
|
|||||||
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
|
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
|
||||||
import org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter;
|
import org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter;
|
||||||
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
|
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
|
||||||
|
import org.springframework.data.elasticsearch.core.query.Criteria;
|
||||||
|
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
|
||||||
|
import org.springframework.data.elasticsearch.core.query.Query;
|
||||||
import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
|
import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm;
|
||||||
import org.springframework.data.geo.Box;
|
import org.springframework.data.geo.Box;
|
||||||
import org.springframework.data.geo.Circle;
|
import org.springframework.data.geo.Circle;
|
||||||
@ -1496,6 +1499,16 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
assertThat(entity.getDontConvert()).isEqualTo("Monty Python's Flying Circus");
|
assertThat(entity.getDontConvert()).isEqualTo("Monty Python's Flying Circus");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // #2080
|
||||||
|
@DisplayName("should not try to call property converter on updating criteria exists")
|
||||||
|
void shouldNotTryToCallPropertyConverterOnUpdatingCriteriaExists() {
|
||||||
|
|
||||||
|
// don't care if the query makes no sense, we just add all criteria without values
|
||||||
|
Query query = new CriteriaQuery(Criteria.where("fieldWithClassBasedConverter").exists().empty().notEmpty());
|
||||||
|
|
||||||
|
mappingElasticsearchConverter.updateQuery(query, EntityWithCustomValueConverters.class);
|
||||||
|
}
|
||||||
|
|
||||||
private Map<String, Object> writeToMap(Object source) {
|
private Map<String, Object> writeToMap(Object source) {
|
||||||
|
|
||||||
Document sink = Document.create();
|
Document sink = Document.create();
|
||||||
@ -2429,6 +2442,7 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
return reverse(value);
|
return reverse(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// endregion
|
||||||
|
|
||||||
private static String reverse(Object o) {
|
private static String reverse(Object o) {
|
||||||
|
|
||||||
@ -2436,5 +2450,4 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
|
|
||||||
return new StringBuilder().append(o.toString()).reverse().toString();
|
return new StringBuilder().append(o.toString()).reverse().toString();
|
||||||
}
|
}
|
||||||
// endregion
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user