quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.82k stars 2.69k forks source link

smallrye-openapi scan for @ServerExceptionMapper annotation #44035

Open indiealexh opened 3 weeks ago

indiealexh commented 3 weeks ago

Description

This is a reopening of #31828, but I think it is worth consideration.

smallrye-openapi can scan for @Provider ExceptionMappers and add them to endpoints that throw that exception.

e.g.

@Path("/bundles")
@Produces({MediaType.APPLICATION_JSON})
@Consumes(MediaType.APPLICATION_JSON)
@Tag(name = "Bundles")
public class BundlesResource {
    [...]
    @POST
    @Path("/{bundleId}/actions/render")
    @APIResponses({
            @APIResponse(responseCode = "200", description = "Render bundle"),
    })
    public Uni<BundleRender> renderBundle(@RestPath long bundleId, BundleRenderRequest bundleRenderRequest) throws BundleRenderValidationException {
        return bundleRenderService.renderBundle(bundleId, bundleRenderRequest);
    }
   [...]
}

public class BundleRenderValidationException extends WebApplicationException {
    public BundleRenderValidationException(String exceptionMessage) {
        super(
                Response
                        .status(Response.Status.BAD_REQUEST)
                        .entity(new ErrorResponse("Bundle Render Request failed validation", List.of(new String[]{exceptionMessage})))
                        .build()
        );
    }
}

@Provider
public class BundleRenderValidationExceptionMapper implements ExceptionMapper<BundleRenderValidationException> {
    @Override
    @APIResponse(
            responseCode = "400",
            description = "Validation of bundle failed",
            content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))
    )
    public Response toResponse(BundleRenderValidationException exception) {
        return exception.getResponse();
    }
}

Image

The same should be possible for exceptions mapped by using @ServerExceptionMapper, happy to open this ticker with https://github.com/smallrye/smallrye-open-api if that is the better place to do so, but it seems like it should be part of the Quarkus extension given the annotation is RestEasy specific.

Implementation ideas

No response

quarkus-bot[bot] commented 3 weeks ago

/cc @EricWittmann (openapi), @Ladicek (smallrye), @MikeEdgar (openapi), @jmartisk (smallrye), @phillip-kruger (openapi,smallrye), @radcortez (smallrye)

phillip-kruger commented 3 weeks ago

I think you can open this in SmallRye OpenAPI. We are already doing RESTEasy things there, see https://github.com/smallrye/smallrye-open-api/blob/main/extension-jaxrs/src/main/java/io/smallrye/openapi/jaxrs/RestEasyConstants.java for example

indiealexh commented 3 weeks ago

I think you can open this in SmallRye OpenAPI. We are already doing RESTEasy things there, see https://github.com/smallrye/smallrye-open-api/blob/main/extension-jaxrs/src/main/java/io/smallrye/openapi/jaxrs/RestEasyConstants.java for example

Thanks, I will do that.