Skip to content

Commit

Permalink
fix for Bean Validation annotations on @query method parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Gavin King <gavin@hibernate.org>
  • Loading branch information
gavinking committed Mar 30, 2024
1 parent f6add9d commit dd7aa94
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Expand Up @@ -5,6 +5,8 @@
import jakarta.data.repository.Query;
import jakarta.data.repository.Repository;
import jakarta.transaction.Transactional;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

import java.util.List;

Expand All @@ -15,10 +17,10 @@ public interface Bookshop extends CrudRepository<Book,String> {
List<Book> byPublisher(String publisher_name);

@Query("select isbn where title like ?1 order by isbn")
String[] ssns(String title);
String[] ssns(@NotBlank String title);

@Query("select count(this) where title like ?1 order by isbn")
long count1(String title);
long count1(@NotNull String title);

@Query("select count(this) where this.title like ?1 order by this.isbn")
long count2(String title);
Expand Down
Expand Up @@ -2562,6 +2562,16 @@ private void checkOrdinalParameter(
}
}

private static String stripTypeAnnotations(String argType) {
while ( argType.startsWith("@") ) {
int index = argType.indexOf(' ');
if (index>0) {
argType = argType.substring(index+1);
}
}
return argType;
}

private void checkNamedParameter(
SqmParameter<?> param, List<String> paramNames, List<String> paramTypes, ExecutableElement method,
AnnotationMirror mirror, AnnotationValue value, String queryParamType) {
Expand All @@ -2584,7 +2594,8 @@ private void checkNamedParameter(
}
}

private static boolean isLegalAssignment(SqmParameter<?> param, String argType, String queryParamType) {
private static boolean isLegalAssignment(SqmParameter<?> param, String argumentType, String queryParamType) {
final String argType = stripTypeAnnotations(argumentType);
return param.allowMultiValuedBinding()
? isLegalAssignment(argType, LIST + '<' + queryParamType + '>')
: isLegalAssignment(argType, queryParamType);
Expand Down Expand Up @@ -2636,7 +2647,12 @@ private List<String> parameterTypes(ExecutableElement method) {
private String typeAsString(TypeMirror type) {
String result = type.toString();
for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) {
result = result.replace(annotation.toString(), "");
final String annotationString = annotation.toString();
result = result
// if it has a space after it, we need to remove that too
.replace(annotationString + ' ', "")
// just in case it did not have a space after it
.replace(annotationString, "");
}
for ( AnnotationMirror annotation : type.getAnnotationMirrors() ) {
result = annotation.toString() + ' ' + result;
Expand Down

0 comments on commit dd7aa94

Please sign in to comment.