package com.ruoyi.common.springdoc;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * SpringDoc API文档相关配置
 * @author  数据小王子 on 2023/07/24.
 */
@Configuration
public class SpringDocConfig {
    @Bean
    public OpenAPI openAPI() {
        SecurityRequirement securityRequirement = new SecurityRequirement().addList("Authorization"); // 名字和创建的SecuritySchemes一致
        List<SecurityRequirement> list = new ArrayList<>();
        list.add(securityRequirement);
        return new OpenAPI()
                .components(new Components().addSecuritySchemes("Authorization", // key值
                        new SecurityScheme()
                                .type(SecurityScheme.Type.APIKEY) //请求认证类型
                                .scheme("bearer").bearerFormat("JWT")
                                .name("Authorization") //API key参数名
                                .description("token令牌") //API key描述
                                .in(SecurityScheme.In.HEADER))) //设置API key的存放位置
                .security(list)
                .info(new Info().title("Ruoyi-Flex API Doc")
                        .description("Ruoyi-Flex SrpingDoc demo")
                        .version("v4.1.2")
                        .license(new License().name("MIT").url("https://opensource.org/licenses/MIT")))
                .externalDocs(new ExternalDocumentation()
                        .description("Ruoyi-Flex Wiki Documentation")
                        .url("https://gitee.com/dataprince/ruoyi-flex"));
    }

}
