本文介绍了Swagger 2.0离线验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一种工具可以进行在线验证:

I know that there's a tool that is able to do an online validation:

http://online. swagger.io/validator?url=http://petstore.swagger.io/v2/swagger.json

我正在编写一个JUnit测试,以验证项目的swagger.json文件.重要的是,此验证可以离线完成,因为该测试以localhost运行,并且该验证工具无法访问本地主机服务器.

I'm writing a JUnit test that validates the project's swagger.json file. It's important that this validation can be done offline, because the test runs as localhost, and that validation tool can't reach a localhost server.

那么,是否可以离线验证Swagger 2.0 JSON文件?

推荐答案

好吧,我使用 fge/json-schema-validator 和杰克逊.它使用Swagger 2.0模式进行验证.

Well, I finished a Swagger validator using fge/json-schema-validator and Jackson. It uses the Swagger 2.0 schema to validate.

https://gist.github.com/mariosotil/e1219d4e946c643fe0e5

@Singleton
public class SwaggerValidator {

    public ArrayNode validate(JsonNode jsonNode) {
        return Optional.of(jsonNode)
                .map(this::validateWithinSwaggerSchema)
                .map(this::getMessagesAsJsonArray)
                .get();
    }

    // [...]
}

希望这会有所帮助.

这篇关于Swagger 2.0离线验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 05:35