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

Server exception mapper method of the Rest-Interface implementation class is not registered for resource defined in the interface. #36872

Closed
andrejpetras opened this issue Nov 4, 2023 · 0 comments · Fixed by #36873
Labels
Milestone

Comments

@andrejpetras
Copy link
Contributor

Describe the bug

Hi, I am not sure if this is a bug or a feature.

Server exception mapper method of the Rest-Interface implementation class is not registered for resource defined in the interface.

Motivation:
We generate server endpoints for our Rest-Interfaces from openapi.yaml files. Generator generates interfaces, and we need to write only the implementation classes. We do have multiple versions (v1, v2, ...) of rest interface (multiple openapi.yaml files) in one application. We would like to have a custom server exception mapper for each version, which will create a valid response to the corresponding version.

Example project: https://github.com/onecx/onecx-tenant-svc/blob/main/src/main/java/org/onecx/tenant/rs/internal/controllers/TenantControllerInternal.java

Current, but not the best, workaround:
To activate the server exception mapper for the implementation class, we need to copy @Path annotation from the interface to our implementation class.

Expected behavior

Server exception mapper method in implementation class of Rest-Interface should be registered only for resource defined in interface.

Actual behavior

Server exception mapper method of the Rest-Interface implementation class is not registered for a resource defined in the interface, or it is registered as a global server exception mapper.

How to Reproduce?

I create a simple example project: https://github.com/andrejpetras/quarkus-ex-mapper

  • start application mvn clean compile quarkus:dev
  • shell exec curl http://localhost:8080/v1?error=1 output ServerExceptionMapperV1: Custom error V1
  • shell exec curl http://localhost:8080/v2?error=1 output ServerExceptionMapperV1: Custom error V2 excepted: ServerExceptionMapperV2: Custom error V2

In both cases, the same server exception mapper is used as a global server exception mapper.

@Path("/v2")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public interface PingRestApiV2 {
    @GET
    Response ping(@QueryParam("error") Long error);
}
@ApplicationScoped
public class PingRestControllerV2 implements PingRestApiV2 {

    @Override
    public Response ping(Long error) {
        if (error == null) {
            return Response.ok().build();
        }
        throw new RuntimeException("Custom error V2");
    }

    @ServerExceptionMapper
    public Response exception(Exception ex) {
        Log.info("Error V2");
        return Response.serverError().entity("ServerExceptionMapperV2: " + ex.getMessage()).build();
    }
}

Method exception of the class PingRestControllerV2 will be ignored.
Method exception of the class PingRestControllerV1 will be registered as a global server exception mapper.

Output of uname -a or ver

Linux andrej 6.2.0-36-generic #37~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Oct 9 15:34:04 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Output of java -version

openjdk version "17.0.5" 2022-10-18 LTS OpenJDK Runtime Environment Zulu17.38+21-CA (build 17.0.5+8-LTS) OpenJDK 64-Bit Server VM Zulu17.38+21-CA (build 17.0.5+8-LTS, mixed mode, sharing)

Quarkus version or git rev

main

Build tool (ie. output of mvnw --version or gradlew --version)

Java version: 17.0.5, vendor: Azul Systems, Inc., runtime: /home/andrej/programs/java/zulu17.38.21-ca-jdk17.0.5-linux_x64 Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "6.2.0-36-generic", arch: "amd64", family: "unix"

Additional information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants