7

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

2 Answers 2

9

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

Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

With this code I dont see default value is working correctly, its taking no default value.
Could you please reply to above comment?
Could you please reply here as its pending since so long ?
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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.