Skip to content

Fix exception on property conversion with criteria exists/empty/non-empty. #2081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1248,14 +1248,19 @@ private void updateCriteria(Criteria criteria, ElasticsearchPersistentEntity<?>
PropertyValueConverter propertyValueConverter = Objects
.requireNonNull(persistentProperty.getPropertyValueConverter());
criteria.getQueryCriteriaEntries().forEach(criteriaEntry -> {
Object value = criteriaEntry.getValue();
if (value.getClass().isArray()) {
Object[] objects = (Object[]) value;
for (int i = 0; i < objects.length; i++) {
objects[i] = propertyValueConverter.write(objects[i]);

if (criteriaEntry.getKey().hasValue()) {
Object value = criteriaEntry.getValue();

if (value.getClass().isArray()) {
Object[] objects = (Object[]) value;

for (int i = 0; i < objects.length; i++) {
objects[i] = propertyValueConverter.write(objects[i]);
}
} else {
criteriaEntry.setValue(propertyValueConverter.write(value));
}
} else {
criteriaEntry.setValue(propertyValueConverter.write(value));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,23 @@ public enum OperationKey { //
/**
* @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();
}
}

/**
Expand All @@ -967,9 +983,8 @@ public static class CriteriaEntry {

protected CriteriaEntry(OperationKey key) {

boolean keyIsValid = key == OperationKey.EXISTS || key == OperationKey.EMPTY || key == OperationKey.NOT_EMPTY;
Assert.isTrue(keyIsValid,
"key must be OperationKey.EXISTS, OperationKey.EMPTY or OperationKey.EMPTY for this call");
Assert.isTrue(key.hasNoValue(),
"key must be OperationKey.EXISTS, OperationKey.EMPTY or OperationKey.NOT_EMPTY for this call");

this.key = key;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter;
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.geo.Box;
import org.springframework.data.geo.Circle;
Expand Down Expand Up @@ -1496,6 +1499,16 @@ void shouldReadUsingValueConverters() throws JSONException {
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) {

Document sink = Document.create();
Expand Down Expand Up @@ -2449,12 +2462,12 @@ public Object read(Object value) {
return reverse(value);
}
}
// endregion

private static String reverse(Object o) {

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

return new StringBuilder().append(o.toString()).reverse().toString();
}
// endregion
}