根据给定的OpenApi/Swagger规范验证请求负载和响应 [英] Validate request payloads and responses against a given OpenApi/Swagger specification

查看:107
本文介绍了根据给定的OpenApi/Swagger规范验证请求负载和响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个HTTP API,该API使用 Python的拥抱框架编写,但是未在其中设置石头,将来可以用其他东西代替.

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.

在它旁边,我们在YAML中手动编写了 OpenApi/Swagger 2.0规范文件该API以便提供交互式文档.但是,通过Swagger生态系统中的任何工具任何工具都无法相互连接-当我们在API中进行更改时,我们也会在规范中进行相同的更改,反之亦然(这并不理想,但我们稍后可能会对此进行改进).

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).

现在,为了进行集成测试,我们希望获取一些静态请求及其有效负载(JSON和multipart/form-data)以及es响应(JSON),并验证它们是否符合我们规范中的模型文件.

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.

推荐答案

对于Open Api 3文档,我和我的同事有相同的要求,最终我编写了自己的命令行工具来实现这一目标,我将其开源在这里: https://github.com/hevans90/oa3-api-defender

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

它将动态调用规范的 paths 数组中指定的所有端点,当前支持GET,DELETE,POST,PATCH,PUT请求.它将根据规范中提供的示例值动态创建请求.

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.

查询和路径示例参数值将包含在生成的请求中.请求主体(例如在POST中)是根据规范中的requestBody对象中的示例值动态生成的.

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

这篇关于根据给定的OpenApi/Swagger规范验证请求负载和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆