Skip to content

Commit

Permalink
Make sure reactive update/delete return a Uni of Integer or Void
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage authored and gavinking committed Mar 1, 2024
1 parent 06136ba commit 5b184ca
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,15 @@ void chainSession(StringBuilder declaration) {

void chainSessionEnd(boolean isUpdate, StringBuilder declaration) {
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(";");
}
}
}

Expand Down Expand Up @@ -445,7 +453,7 @@ void tryReturn(StringBuilder declaration, List<String> paramTypes, @Nullable Str
.append(" _results = ");
}
else {
if ( !"void".equals(returnTypeName) ) {
if ( !"void".equals(returnTypeName) || isReactiveSession() ) {
declaration
.append("return ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1558,16 +1558,33 @@ private void validateUpdateHql(
@Nullable TypeMirror returnType,
AnnotationMirror mirror,
AnnotationValue value) {
if ( returnType == null
|| returnType.getKind() != TypeKind.VOID
&& returnType.getKind() != TypeKind.BOOLEAN
&& returnType.getKind() != TypeKind.INT ) {
boolean reactive = usingReactiveSession( sessionType );
if ( !isValidUpdateReturnType( returnType, method, reactive ) ) {
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 );
}
}

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(
ExecutableElement method,
@Nullable TypeMirror returnType,
Expand Down Expand Up @@ -1955,7 +1972,8 @@ private static boolean hasParameter(String hql, int i, String param) {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public final class Constants {

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_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 COLLECTION_ATTRIBUTE = "jakarta.persistence.metamodel.CollectionAttribute";
Expand Down

0 comments on commit 5b184ca

Please sign in to comment.