问题
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
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.
So, is it possible to validate a Swagger 2.0 JSON file, offline?
回答1:
I'm very satisfied with this Validator from Atlassian: https://bitbucket.org/atlassian/swagger-request-validator
There is still active development, so I guess they will also provide something for OpenAPI 3.
回答2:
I have created a Maven project that validates swagger JSON documents if you ever decide to use Maven for running your tests.
You can clone the project here: https://github.com/navidsh/maven.swagger.validator
回答3:
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();
}
// [...]
}
Hope this helps.
来源:https://stackoverflow.com/questions/35978671/swagger-2-0-offline-validation