Make sure reactive update/delete return a Uni of Integer or Void

This commit is contained in:
Stéphane Épardaud 2023-12-01 17:27:55 +01:00 committed by Gavin King
parent 06136ba5ec
commit 5b184caf9b
3 changed files with 38 additions and 8 deletions

View File

@ -204,7 +204,15 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
void chainSessionEnd(boolean isUpdate, StringBuilder declaration) { void chainSessionEnd(boolean isUpdate, StringBuilder declaration) {
if ( isReactiveSession() ) { if ( isReactiveSession() ) {
declaration.append("\n\t});"); declaration.append("\n\t})");
// here we're checking for a boxed void and not Uni<Void> because the returnType has already
// been checked, and is ununi-ed
if ( isUpdate && returnTypeName != null && returnTypeName.equals(BOXED_VOID) ) {
declaration.append(".replaceWithVoid();");
}
else {
declaration.append(";");
}
} }
} }
@ -445,7 +453,7 @@ public abstract class AbstractQueryMethod implements MetaAttribute {
.append(" _results = "); .append(" _results = ");
} }
else { else {
if ( !"void".equals(returnTypeName) ) { if ( !"void".equals(returnTypeName) || isReactiveSession() ) {
declaration declaration
.append("return "); .append("return ");
} }

View File

@ -1558,16 +1558,33 @@ public class AnnotationMetaEntity extends AnnotationMeta {
@Nullable TypeMirror returnType, @Nullable TypeMirror returnType,
AnnotationMirror mirror, AnnotationMirror mirror,
AnnotationValue value) { AnnotationValue value) {
if ( returnType == null boolean reactive = usingReactiveSession( sessionType );
|| returnType.getKind() != TypeKind.VOID if ( !isValidUpdateReturnType( returnType, method, reactive ) ) {
&& returnType.getKind() != TypeKind.BOOLEAN
&& returnType.getKind() != TypeKind.INT ) {
context.message( method, mirror, value, context.message( method, mirror, value,
"return type of mutation query method must be 'int', 'boolean', or 'void'", "return type of mutation query method must be " + (!reactive ? "'int', 'boolean' or 'void'" : "'Uni<Integer>', 'Uni<Boolean>' or 'Uni<Void>'"),
Diagnostic.Kind.ERROR ); Diagnostic.Kind.ERROR );
} }
} }
private boolean isValidUpdateReturnType(@Nullable TypeMirror returnType, ExecutableElement method, boolean reactive) {
if ( returnType == null ) {
return false;
}
if ( reactive ) {
// for reactive calls, don't use the returnType param, which has been ununi-ed, we want to check the full one
return method.getReturnType().toString().equals( Constants.UNI_VOID )
|| method.getReturnType().toString().equals( Constants.UNI_BOOLEAN )
|| method.getReturnType().toString().equals( Constants.UNI_INTEGER );
}
else {
// non-reactive
return returnType.getKind() == TypeKind.VOID
|| returnType.getKind() == TypeKind.BOOLEAN
|| returnType.getKind() == TypeKind.INT;
}
}
private void validateSelectHql( private void validateSelectHql(
ExecutableElement method, ExecutableElement method,
@Nullable TypeMirror returnType, @Nullable TypeMirror returnType,
@ -1955,7 +1972,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
} }
private boolean usingReactiveSession(String sessionType) { private boolean usingReactiveSession(String sessionType) {
return Constants.MUTINY_SESSION.equals(sessionType); return Constants.MUTINY_SESSION.equals(sessionType)
|| Constants.UNI_MUTINY_SESSION.equals(sessionType);
} }
private boolean usingStatelessSession(String sessionType) { private boolean usingStatelessSession(String sessionType) {

View File

@ -99,6 +99,10 @@ public final class Constants {
public static final String UNI = "io.smallrye.mutiny.Uni"; public static final String UNI = "io.smallrye.mutiny.Uni";
public static final String UNI_MUTINY_SESSION = UNI+"<org.hibernate.reactive.mutiny.Mutiny.Session>"; public static final String UNI_MUTINY_SESSION = UNI+"<org.hibernate.reactive.mutiny.Mutiny.Session>";
public static final String UNI_INTEGER = UNI+"<java.lang.Integer>";
public static final String UNI_VOID = UNI+"<java.lang.Void>";
public static final String UNI_BOOLEAN = UNI+"<java.lang.Boolean>";
public static final String BOXED_VOID = Void.class.getName();
public static final String SINGULAR_ATTRIBUTE = "jakarta.persistence.metamodel.SingularAttribute"; public static final String SINGULAR_ATTRIBUTE = "jakarta.persistence.metamodel.SingularAttribute";
public static final String COLLECTION_ATTRIBUTE = "jakarta.persistence.metamodel.CollectionAttribute"; public static final String COLLECTION_ATTRIBUTE = "jakarta.persistence.metamodel.CollectionAttribute";