Global Parameter set using Open API configurations?

笑着哭i 提交于 2020-05-13 14:37:27

问题


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

回答1:


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




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


来源:https://stackoverflow.com/questions/60006991/global-parameter-set-using-open-api-configurations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!