Validate request payloads and responses against a given OpenApi/Swagger specification

非 Y 不嫁゛ 提交于 2020-05-15 04:29:41

问题


We have an HTTP API which is written with the hug framework for Python but this is not set in stone and could be replaced with something else in the future.

Next to it, we have manually written an OpenApi/Swagger 2.0 specification file in YAML for that API in order to provide interactive documentation. However, both are not connected to each other by any of the tooling from the Swagger ecosystem - when we make changes in the API, we make the same changes in the specification and vice versa (which is not ideal but we might improve on this later).

Now, for the sake of integration testing, we would like to take a few static requests and their payloads (JSON and multipart/form-data) as well es responses (JSON) and validate if they fit the models from our specification file.

What could be an easy way to achieve this? I am open to any technology or programming language as long as complexity stays reasonable.


回答1:


For Open Api 3 documents, my colleagues and I had the same requirement and ended up writing our own command-line tooling to achieve this, I've open sourced it here: https://github.com/hevans90/oa3-api-defender

It will dynamically call any endpoints specified in the paths array of a specification, currently supporting GET, DELETE, POST, PATCH, PUT requests. It will dynamically create requests based on example values provided in the spec.

Query & Path example parameter values will be included in generated requests. Request bodies (in POSTs for example) are dynamically generated from example values in the requestBody object, in the spec.

To provide valid examples in your spec, see this article



来源:https://stackoverflow.com/questions/48338422/validate-request-payloads-and-responses-against-a-given-openapi-swagger-specific

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!