Relax query parameter validation.

This change allows to use collection/array arguments with scalar operators when targeting a collection like property.

Closes #3356
Original pull request: #3359
This commit is contained in:
Christoph Strobl
2024-02-09 11:19:12 +01:00
committed by Mark Paluch
parent a4f23b5fc1
commit 005c507eaa
2 changed files with 21 additions and 5 deletions

View File

@@ -166,10 +166,14 @@ public class PartTreeJpaQuery extends AbstractJpaQuery {
JpaParameter parameter = parameters.getBindableParameter(index);
if (expectsCollection(type) && !parameterIsCollectionLike(parameter)) {
throw new IllegalStateException(wrongParameterTypeMessage(methodName, property, type, "Collection", parameter));
} else if (!expectsCollection(type) && !parameterIsScalarLike(parameter)) {
throw new IllegalStateException(wrongParameterTypeMessage(methodName, property, type, "scalar", parameter));
if (expectsCollection(type)) {
if (!parameterIsCollectionLike(parameter)) {
throw new IllegalStateException(wrongParameterTypeMessage(methodName, property, type, "Collection", parameter));
}
} else {
if (!part.getProperty().isCollection() && !parameterIsScalarLike(parameter)) {
throw new IllegalStateException(wrongParameterTypeMessage(methodName, property, type, "scalar", parameter));
}
}
}
@@ -319,7 +323,7 @@ public class PartTreeJpaQuery extends AbstractJpaQuery {
returnedType = processor.getReturnedType();
}
if (accessor != null && accessor.getScrollPosition()instanceof KeysetScrollPosition keyset) {
if (accessor != null && accessor.getScrollPosition() instanceof KeysetScrollPosition keyset) {
return new JpaKeysetScrollQueryCreator(tree, returnedType, builder, provider, entityInformation, keyset);
}

View File

@@ -30,6 +30,7 @@ import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import org.hibernate.Version;
import org.junit.jupiter.api.BeforeEach;
@@ -228,6 +229,13 @@ class PartTreeJpaQueryIntegrationTests {
.withMessageContaining("UserRepository"); // the repository
}
@Test // GH-3356
void allowsCollectionArgForCollectionProperty() throws Exception {
new PartTreeJpaQuery(getQueryMethod("findByAttributes", Set.class), entityManager);
new PartTreeJpaQuery(getQueryMethod("findByAttributes", String[].class), entityManager);
}
private void testIgnoreCase(String methodName, Object... values) throws Exception {
Class<?>[] parameterTypes = new Class[values.length];
@@ -297,6 +305,10 @@ class PartTreeJpaQueryIntegrationTests {
// Wrong property name
User findByNoSuchProperty(String x);
List<User> findByAttributes(Set<String> attributes);
List<User> findByAttributes(String... attributes);
}
}