Skip to content

Commit

Permalink
Support RestEasy's @ServerExceptionMapper method annotation (#2102)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <michael@xlate.io>
  • Loading branch information
MikeEdgar authored Dec 9, 2024
1 parent dda7cd6 commit 875dc7a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion extension-jaxrs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
</dependency>
<dependency>
<groupId>io.quarkus.resteasy.reactive</groupId>
<artifactId>resteasy-reactive-common</artifactId>
<artifactId>resteasy-reactive</artifactId>
<version>${version.quarkus}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -118,6 +119,7 @@ public boolean isFrameworkContextType(Type type) {
}

@Override
@SuppressWarnings("deprecation")
public boolean containsScannerAnnotations(List<AnnotationInstance> instances,
List<AnnotationScannerExtension> extensions) {

Expand Down Expand Up @@ -314,19 +316,27 @@ private void processResourceMethods(final ClassInfo resourceClass,
*
*/
private Map<DotName, Map<String, APIResponse>> processExceptionMappers() {
Collection<ClassInfo> exceptionMappers = new ArrayList<>();
Map<DotName, Map<String, APIResponse>> exceptionMappers = new HashMap<>();

for (DotName dn : JaxRsConstants.EXCEPTION_MAPPER) {
exceptionMappers.addAll(context.getIndex()
.getKnownDirectImplementors(dn));
for (DotName mapperName : JaxRsConstants.EXCEPTION_MAPPER) {
for (var mapper : context.getIndex().getKnownDirectImplementors(mapperName)) {
exceptionMappers.putAll(exceptionResponseAnnotations(mapper));
}
}

for (var mapperAnnotation : context.getIndex().getAnnotations(RestEasyConstants.SERVER_EXCEPTION_MAPPER)) {
// @ServerExceptionMapper only targets methods
MethodInfo mapperMethod = mapperAnnotation.target().asMethod();
if (mapperMethod.parametersCount() == 1) {
DotName exceptionName = mapperMethod.parameterType(0).name();
exceptionMappers.put(exceptionName, context.io().apiResponsesIO().readAll(mapperMethod));
}
}

return exceptionMappers.stream()
.flatMap(this::exceptionResponseAnnotations)
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
return exceptionMappers;
}

private Stream<Entry<DotName, Map<String, APIResponse>>> exceptionResponseAnnotations(ClassInfo classInfo) {
private Map<DotName, Map<String, APIResponse>> exceptionResponseAnnotations(ClassInfo classInfo) {

Type exceptionType = classInfo.interfaceTypes()
.stream()
Expand All @@ -338,7 +348,7 @@ private Stream<Entry<DotName, Map<String, APIResponse>>> exceptionResponseAnnota
.orElse(null);

if (exceptionType == null) {
return Stream.empty();
return Collections.emptyMap();
}

Stream<Entry<String, APIResponse>> methodAnnotations = Stream
Expand Down Expand Up @@ -366,9 +376,9 @@ private Stream<Entry<DotName, Map<String, APIResponse>>> exceptionResponseAnnota
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, latest, LinkedHashMap::new));

if (annotations.isEmpty()) {
return Stream.empty();
return Collections.emptyMap();
} else {
return Stream.of(entryOf(exceptionType.name(), annotations));
return Map.of(exceptionType.name(), annotations);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public class RestEasyConstants {
MULTIPART_FORM_DATA_OUTPUT,
MULTIPART_RELATED_OUTPUT)));

public static final DotName SERVER_EXCEPTION_MAPPER = DotName
.createSimple("org.jboss.resteasy.reactive.server.ServerExceptionMapper");

private RestEasyConstants() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ void testJavaxExceptionMapper() throws IOException, JSONException {
test("responses.exception-mapper-generation.json",
test.io.smallrye.openapi.runtime.scanner.javax.TestResource.class,
test.io.smallrye.openapi.runtime.scanner.javax.ExceptionHandler1.class,
test.io.smallrye.openapi.runtime.scanner.javax.ExceptionHandler2.class,
test.io.smallrye.openapi.runtime.scanner.javax.ResteasyReactiveExceptionMapper.class);
}

Expand All @@ -32,7 +31,6 @@ void testJakartaExceptionMapper() throws IOException, JSONException {
test("responses.exception-mapper-generation.json",
test.io.smallrye.openapi.runtime.scanner.jakarta.TestResource.class,
test.io.smallrye.openapi.runtime.scanner.jakarta.ExceptionHandler1.class,
test.io.smallrye.openapi.runtime.scanner.jakarta.ExceptionHandler2.class,
test.io.smallrye.openapi.runtime.scanner.jakarta.ResteasyReactiveExceptionMapper.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;

import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;

@Path(value = "/resources")
public class TestResource {
Expand All @@ -19,4 +23,9 @@ public String createResource() throws WebApplicationException {
return "OK";
}

@ServerExceptionMapper
@APIResponse(responseCode = "404", description = "Not Found")
public Response handleNotFound(NotFoundException exception) {
return exception.getResponse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;

@Path(value = "/resources")
public class TestResource {
Expand All @@ -19,4 +23,9 @@ public String createResource() throws WebApplicationException {
return "OK";
}

@ServerExceptionMapper
@APIResponse(responseCode = "404", description = "Not Found")
public Response handleNotFound(NotFoundException exception) {
return exception.getResponse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
}
}
}
},
"404" : {
"description" : "Not Found"
}
}
},
Expand Down

0 comments on commit 875dc7a

Please sign in to comment.