Search code examples
springspring-bootswaggerspringdoc

Global Parameter set using Open API configurations?


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;
        }
}

Solution

  • 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