Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

swagger - Smallrye open api interceptor

I am developing a rest application.

Some endpoints require a custom header parameter, not related to authorisation. I created a custom annotation using jax-rs NameBinding. Here is an usage example:

@GET
@RequiresBankHeader
public int get(
        @HeaderParam("bank")
        @Parameter(ref = "#/components/parameters/banks")
                String bank) {        
    return someService.getSomeInformation();
}

There is a provider that intercepts this call and do some routine using the information in the header parameter.

The problem is that I have to repeat '@HeaderParam("bank") @Parameter(ref = "#/components/parameters/banks") String bank' everywhere, just so it appears in Swagger, even though the service classes do not need it. I was able to at least reuse the parameter definition with ref = "#/components/parameters/banks", and declaring it in the OpenAPI.yml file, that Quarkus merges with generated code very nicely.

But I also want to create and interceptor to dynamically add this do the OpenApi definition whenever RequiresBankHeader annotation is present.

Is there a way to do it?

question from:https://stackoverflow.com/questions/65860644/smallrye-open-api-interceptor

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I dont think you can use interceptors to modify the generated Openapi schema output. If all methods on a given endpoint require some parameter, you can specify it on class level like so:

@Path("/someendpoint")
public class MyEndpoint {
  
    @HeaderParam("bank")
    @Parameter(name = "bank")
    String bank;

    @GET
    public Response getAll() {return Response.ok().build()}

    @GET
    @Path("{id}")
    public Response someMethod(@PathParam("id") String id) {return Response.ok().build();}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...