6

I am using Spring Boot REST OpenAPI 3 specification. In this example, I am looking to globally set the headers (Custom-Header-Version=v1) which I want to pass while making a request to each endpoint(s).

Now issue is that I've 100 of REST endpoint and for each endpoint I need to keep adding @Parameter(in = ParameterIn.HEADER ....., this configuration, instead I was looking to set it globally. Is there any way if we can do it in OpenAPI?

Is there any way to remove SmartBear logo from Spring doc ui ?

@RestController
@RequestMapping("/api")
@Tag(name = "contact", description = "the Contact API")
public class HelloController {

    @Operation(summary = "Find Contacts by name", description = "Name search by %name% format", tags = {"contact"})
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "successful operation", content = @Content(array = @ArraySchema(schema = @Schema(implementation = PersonDTO.class))))})
    @Parameter(in = ParameterIn.HEADER, description = "Custom Header To be Pass", name = "Accept-version"
            , content = @Content(schema = @Schema(type = "string", defaultValue = "v1", allowableValues = {"v1"}, implementation = PersonDTO.class)))
    @GetMapping(value = "/contacts", headers = {"Custom-Header-Version=v1"})
    public ResponseEntity<List<PersonDTO>> findAll(
            @Parameter(description = "Page number, default is 1") @RequestParam(value = "page", defaultValue = "1") int pageNumber,
            @Parameter(description = "Name of the contact for search.") @RequestParam(required = false) String name) {

            return null;
        }
}
PAA
  • 1
  • 46
  • 174
  • 282

2 Answers2

8

you can try the following code. Added .example("v1") in the code mentioned above by ouled saber

@Component
public class GlobalHeaderOperationCustomizer implements OperationCustomizer {
    @Override
    public Operation customize(Operation operation, HandlerMethod handlerMethod) {

        Parameter customHeaderVersion = new Parameter().in(ParameterIn.HEADER.toString()).name("Custom-Header-Version")
                .description("Custom Header Version)").schema(new StringSchema()).example("v1").required(false);

        operation.addParametersItem(customHeaderVersion);       
        return operation;
    }

I have same requirement and on swagger I am getting like below

Image from swagger ui

SSK
  • 3,444
  • 6
  • 32
  • 59
2

You can just define a OperationCustomizer.

@Component
public class GlobalHeaderOperationCustomizer implements OperationCustomizer {
    @Override
    public Operation customize(Operation operation, HandlerMethod handlerMethod) {
        Parameter parameterHeader = new Parameter()
                .in(ParameterIn.HEADER.toString())
                .schema(new StringSchema().addEnumItem("v1")._default("v1").name("Accept-version"))
                .description("Custom Header To be Pass");;
        operation.addParametersItem(parameterHeader);
        return operation;
    }
}
  • With this code I dont see default value is working correctly, its taking no default value. – PAA Feb 01 '20 at 16:41
  • 1
    Could you please reply to above comment? – PAA Feb 04 '20 at 14:17
  • Could you please reply here as its pending since so long ? – PAA Mar 02 '20 at 14:15
  • This does indeed work if you only have one OpenAPi config (~ one old springfox 'Docket') and are doing the config through `springdoc.packagesToScan` and `springdoc.pathsToMatch` spring properties. If you have a config with multiple `GroupedOpenApi`, then you need to add the customizer to each GroupedOpenApi through `GroupedOpenApi.builder().packagesToScan(...).addOpenApiCustomiser(globalHeaderCustomizer()).build();` – Stijn V Jun 19 '23 at 14:15