From 98b4f5ee3cde3f697b31cf6d2b317d10f123803a Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 9 Feb 2024 11:19:12 +0100 Subject: [PATCH] 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 --- .../jpa/repository/query/PartTreeJpaQuery.java | 14 +++++++++----- .../query/PartTreeJpaQueryIntegrationTests.java | 12 ++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/PartTreeJpaQuery.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/PartTreeJpaQuery.java index 3a33ae380..479dc4b52 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/PartTreeJpaQuery.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/PartTreeJpaQuery.java @@ -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); } diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java index b61350411..ddd71dbfa 100644 --- a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java @@ -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 findByAttributes(Set attributes); + + List findByAttributes(String... attributes); } }