Working with SwaggerUI & JAX-RS - Adding a custom header globally

Viewed 292

I was trying to inject a global header to my suite of requests. While studying the solution I found the annotation @ApiImplicitParam that has @Target(value=METHOD)

example:

@DeleteMapping(value = "/token/{version}", produces = "application/json; charset=UTF-8")
@ApiOperation(value = "delete actual token", produces = "application/json", response = Application.class)
@ApiImplicitParams(
  {
    @ApiImplicitParam(name = "My-custom-header", required = true, paramType = "header",
      dataType = "string", value = "custom header", defaultValue = "GOOD")
  }
)
public Application deleteAuthenticationToken(
  @ApiParam(required = true, value = "version", defaultValue = "v1") @PathVariable("version") String version,
  HttpServletRequest request) {

here the docs

https://docs.swagger.io/swagger-core/current/apidocs/io/swagger/annotations/ApiImplicitParam.html

This means that this annotation can't be set to the resource and being automatically applied to all the methods.

I need that every call has this custom header, is there a way of doing it globally?

EDIT1: I added a HeaderParam in the swagger config but it doesn't work

        private void inizializeSwagger(){
                            BeanConfig beanConfig = new BeanConfig();
                            beanConfig.setVersion(PropertiesConfig.getConfig().getString(PropertiesConst.P_SWAGGER_API_VERSION));
                            beanConfig.setSchemes(PropertiesConfig.getConfig().getStringArray(PropertiesConst.P_SWAGGER_PROTOCOL));
                            beanConfig.setHost(PropertiesConfig.getConfig().getString(PropertiesConst.P_SWAGGER_HOST));
                            beanConfig.setResourcePackage(PropertiesConfig.getConfig().getString(PropertiesConst.P_SWAGGER_RESOURCES_PACKAGE));
                            Swagger swagger = new Swagger(); 
                            swagger.securityDefinition("basic", new BasicAuthDefinition()); 
                            Parameter globalOperationParameter = new HeaderParameter();
                            globalOperationParameter.setName("System");
                            globalOperationParameter.setRequired(Boolean.TRUE);
                            globalOperationParameter.setDescription("definition of the calling system");
                            swagger.parameter("System", globalOperationParameter);
                            new SwaggerContextService().updateSwagger(swagger);
                            beanConfig.setScan(true);
       }

EDIT2:

call structure

It adds the parameters but SwaggerUI 2.0 doesn't show it

EDIT3:

I had to change the swaggerUI library implementation:

in the cycle of // operations (row 4695) I added the following

      for(name in spec.parameters) {
          var actualParam = spec.parameters[name];
          if(actualParam.in === 'header') {               
              parameters.unshift({"name":actualParam.name,
                                  "in":actualParam.in,
                                  "description":actualParam.description,
                                  "required":actualParam.required,
                                  "type":"string",
                                  "default":""})
          }
      }

In other words I added the params I needed in the cycling operation.

I leave this ticket opened for better and cleaner solutions

0 Answers
Related