spring boot swagger库-可以将OpenAPI 3转换为swagger 2吗?

jchrr9hc  于 2021-07-23  发布在  Java
关注(0)|答案(0)|浏览(520)

我有一个场景,在这个场景中,我需要openapi3和swagger 2格式的api文档。我目前使用gradle io.swagger.v3模型生成openapi3文档,并分别使用openapicustomiser和securityscheme提供相关的模式约束和oauth2定义。
我意识到我可以使用springfox.documentation中的docket类来返回swagger 2文档,但是我想包括我从openapicustomiser获得的oauth定义和模式约束,所有这些都可以翻译到swagger 2。这可能吗?
如果有帮助的话,我可以这样使用io.swagger库来定义openapi文档:
招摇过市

@EnableSwagger2
@Configuration
public class SwaggerConfig {
...

    private final SwaggerConfigUtil util = new SwaggerConfigUtil();

    private SecurityScheme applicationIdSecurityScheme() {
        return new SecurityScheme().type(SecurityScheme.Type.OAUTH2)
                .flows(new OAuthFlows()
                        .implicit(new OAuthFlow()
                                .authorizationUrl(adV2TokenUrl)
                                .scopes(new Scopes()
                                        .addString("openid", "openid")
                                )
                        )
                );
    }
...

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(apiInfo())
                .schemaRequirement("OAuth2", applicationIdSecurityScheme())
                .addSecurityItem(new SecurityRequirement().addList("OAuth2"));
    }

    @Bean
    public OpenApiCustomiser openApiCustomiser() {

        return openApi -> {
            openApi.getPaths().forEach((s, p) -> {
                util.operationConstraints(p.getGet());
                util.operationConstraints(p.getDelete());
                util.operationConstraints(p.getHead());
                util.operationConstraints(p.getOptions());
                util.operationConstraints(p.getPatch());
                util.operationConstraints(p.getPost());
                util.operationConstraints(p.getPut());
                util.operationConstraints(p.getTrace());
            });
            openApi.getComponents().getSchemas().values().forEach(s -> {
                s.setAdditionalProperties(false);
                util.propertyContraints(s.getProperties());
            });

        };
    }
}

招摇过市

public class SwaggerConfigUtil {
    private final int INT_MIN = Integer.MIN_VALUE;
    private final int INT_MAX = Integer.MAX_VALUE;
    private final int DEFAULT_LENGTH = 255;
    private final String DEFAULT_PATTERN = "^.*$";
    private  final Map<Class<?>, Function<Schema, Integer>> SET_SCHEMA_CONTRAINTS =
            Stream.of(
                    new AbstractMap.SimpleEntry<Class<?>, Function<Schema, Integer>>(StringSchema.class, this::setStringContraints),
                    new AbstractMap.SimpleEntry<Class<?>, Function<Schema, Integer>>(DateSchema.class, this::setStringContraints),
                    new AbstractMap.SimpleEntry<Class<?>, Function<Schema, Integer>>(DateTimeSchema.class, this::setStringContraints),
                    new AbstractMap.SimpleEntry<Class<?>, Function<Schema, Integer>>(ArraySchema.class, this::setArrayContraints),
                    new AbstractMap.SimpleEntry<Class<?>, Function<Schema, Integer>>(IntegerSchema.class, this::setIntegerConstraints),
                    new AbstractMap.SimpleEntry<Class<?>, Function<Schema, Integer>>(NumberSchema.class, this::setIntegerConstraints))
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    private final ApiResponse responseDefault = new ApiResponse();
    private final ApiResponses defaultApiResponses = new ApiResponses().addApiResponse("default", new ApiResponse());

    public void operationConstraints(Operation operation){
        if(operation != null){
            setApiResponses(operation.getResponses());
            if(operation.getParameters() != null){
                paramConstraints(operation.getParameters());
            }
        }
    }
    public void setApiResponses(ApiResponses responses){
            if(responses != null){
                responseContraints(responses);
            }
    }
    public void paramConstraints(List<Parameter> parameters){
        parameters.forEach(p -> {
            Schema s = p.getSchema();
            if(s != null){
                schemaConstraints(s);
            }
        });
    }
    public void contentConstraints(Content content){
        MediaType m = content.get("application/json");
        System.out.println(m);
        if(m != null) {
            Schema s = m.getSchema();
            schemaConstraints(s);
        }
    }
    public void schemaConstraints(Schema s){
        Class c = s.getClass();
        SET_SCHEMA_CONTRAINTS.getOrDefault(c, (v)->1).apply(s);
    }

    public void propertyContraints(Map properties){
        Iterator<Map.Entry> iter = properties.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry s = iter.next();
            Schema value = (Schema)s.getValue();
            schemaConstraints(value);
        }
    }
    public void responseContraints(ApiResponses responses){
        Iterator<Map.Entry<String,ApiResponse>> iter = responses.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry a = iter.next();
            ApiResponse val = (ApiResponse)a.getValue();
            Content c = val.getContent();
            contentConstraints(c);
        }
    }

    public int setStringContraints(Schema schema){
        if(schema.getMaxLength() == null){
            schema.setMaxLength(DEFAULT_LENGTH);
        }
        if(schema.getPattern() == null){
            schema.setPattern(DEFAULT_PATTERN);
        }
        return 0;
    }
    public int setArrayContraints(Schema schema){
        ArraySchema arraySchema = (ArraySchema) schema;
        if(arraySchema.getMaxItems() == null){
            arraySchema.setMaxItems(DEFAULT_LENGTH);
        }
        Schema items = arraySchema.getItems();
        setItemConstraints(items);
        return 0;
    }
    public int setItemConstraints(Schema items){
        schemaConstraints(items);
        Map properties = items.getProperties();
        if(properties != null){
            propertyContraints(properties);
        }
        return 0;
    }
    public int setIntegerConstraints(Schema schema){
        if(schema.getMinimum() == null){
            schema.setMinimum((BigDecimal.valueOf(INT_MIN)));
        }
        if(schema.getMaximum() == null){
            schema.setMaximum((BigDecimal.valueOf(INT_MAX)));
        }
        return 0;
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题