Is there a way to use Swagger just for validation

2019-03-01 16:06发布

Suppose I have an existing Java service implementing a JSON HTTP API, and I want to add a Swagger schema and automatically validate requests and responses against it without retooling the service to use the Swagger framework / code generation. Is there anything providing a Java API that I can tie into and pass info about the requests / responses to validate?

(Just using a JSON schema validator would mean manually implementing a lot of the additional features in Swagger.)

1条回答
Anthone
2楼-- · 2019-03-01 16:38

I don't think there's anything ready to do this alone, but you can easily do this by the following:

  • Grab the SchemaValidator from the Swagger Inflector project. You can use this to validate inbound and outbound payloads
  • Assign a schema portion to your request/response definitions. That means you'll need to assign a specific section of the JSON schema to your operations
  • Create a filter for your API to grab the payloads and use the schema

That will let you easily see if the payloads match the expected structure.

Of course, this is all done for you automatically with Inflector but there should be enough of the raw components to help you do this inside your own implementation

查看更多
登录 后发表回答