Skip to content

Commit 46ea2df

Browse files
christophstroblmp911de
authored andcommitted
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
1 parent 764a3ed commit 46ea2df

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/PartTreeJpaQuery.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,14 @@ private static void throwExceptionOnArgumentMismatch(String methodName, Part par
166166

167167
JpaParameter parameter = parameters.getBindableParameter(index);
168168

169-
if (expectsCollection(type) && !parameterIsCollectionLike(parameter)) {
170-
throw new IllegalStateException(wrongParameterTypeMessage(methodName, property, type, "Collection", parameter));
171-
} else if (!expectsCollection(type) && !parameterIsScalarLike(parameter)) {
172-
throw new IllegalStateException(wrongParameterTypeMessage(methodName, property, type, "scalar", parameter));
169+
if (expectsCollection(type)) {
170+
if (!parameterIsCollectionLike(parameter)) {
171+
throw new IllegalStateException(wrongParameterTypeMessage(methodName, property, type, "Collection", parameter));
172+
}
173+
} else {
174+
if (!part.getProperty().isCollection() && !parameterIsScalarLike(parameter)) {
175+
throw new IllegalStateException(wrongParameterTypeMessage(methodName, property, type, "scalar", parameter));
176+
}
173177
}
174178
}
175179

@@ -319,7 +323,7 @@ protected JpaQueryCreator createCreator(@Nullable JpaParametersParameterAccessor
319323
returnedType = processor.getReturnedType();
320324
}
321325

322-
if (accessor != null && accessor.getScrollPosition()instanceof KeysetScrollPosition keyset) {
326+
if (accessor != null && accessor.getScrollPosition() instanceof KeysetScrollPosition keyset) {
323327
return new JpaKeysetScrollQueryCreator(tree, returnedType, builder, provider, entityInformation, keyset);
324328
}
325329

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Collection;
3131
import java.util.Date;
3232
import java.util.List;
33+
import java.util.Set;
3334

3435
import org.hibernate.Version;
3536
import org.junit.jupiter.api.BeforeEach;
@@ -228,6 +229,13 @@ void errorsDueToMissingPropertyContainNameOfMethodAndInterface() throws Exceptio
228229
.withMessageContaining("UserRepository"); // the repository
229230
}
230231

232+
@Test // GH-3356
233+
void allowsCollectionArgForCollectionProperty() throws Exception {
234+
235+
new PartTreeJpaQuery(getQueryMethod("findByAttributes", Set.class), entityManager);
236+
new PartTreeJpaQuery(getQueryMethod("findByAttributes", String[].class), entityManager);
237+
}
238+
231239
private void testIgnoreCase(String methodName, Object... values) throws Exception {
232240

233241
Class<?>[] parameterTypes = new Class[values.length];
@@ -297,6 +305,10 @@ interface UserRepository extends Repository<User, Integer> {
297305

298306
// Wrong property name
299307
User findByNoSuchProperty(String x);
308+
309+
List<User> findByAttributes(Set<String> attributes);
310+
311+
List<User> findByAttributes(String... attributes);
300312
}
301313

302314
}

0 commit comments

Comments
 (0)