Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support RestEasy's @ServerExceptionMapper method annotation #2102

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading