官方只支持 Node.js 10/12,用新版 Node 可能出问题;代理默认监听 8888 端口,需用 -t 指定目标 API 的完整 base path;生产环境校验开销大,建议开 --silent 只加校验头
openapi-cop
EXXETAMIT活跃维护一个 OpenAPI 代理,按接口文档实时校验请求和响应,帮你在开发和测试阶段发现契约不一致的问题。
A proxy that validates responses and requests against an OpenAPI document. https://www.npmjs.com/package/openapi-cop https://hub.docker.com/r/lxlu/openapi-cop
npm install -g openapi-cop
openapi-cop -s ./openapi.yaml -t http://localhost:8889README 未说明硬件要求;仅说明在 Node.js 10 和 12 上测试通过,更高版本可能可用但不受支持
把代理放在客户端和服务端之间,按 OpenAPI 文档校验请求和响应,开发期抓出不合规的调用
一个 OpenAPI 代理,按接口文档实时校验请求和响应,帮你在开发和测试阶段发现契约不一致的问题。
核心能力
- 作为独立 HTTP 代理双向校验请求与响应
- 校验失败默认返回 500 并附详细错误 JSON
- 响应头回传校验结果和原始请求摘要
- --silent 模式只加校验头、原样转发响应体
- 支持 CLI、Node 模块和 Docker 三种启动方式
容易踩的坑
- v1.3.2Release 1.3.2最新正式版2022-10-28 · 2022-10-28 06:12 UTC
Changelog
- Fix: CLI version now matches npm package version.
- v1.3.1Release 1.3.12022-10-27 · 2022-10-27 13:56 UTC
查看更新说明
- Fixes npm installation
- Fixes Docker build
Changelog (1.3.0)
- Support target URIs with https: protocol #392
- Fix bug that prevents global installation using npm (#398).
- Allow 5-digit port numbers in target and proxy.
- v1.3.0Release 1.3.02022-10-27 · 2022-10-27 13:15 UTC
查看更新说明
Changelog
- Support target URIs with https: protocol #392
- Fix bug that prevents global installation using npm (#398).
- Allow 5-digit port numbers in target and proxy.
- v1.2.0Release 1.2.02022-09-09 · 2022-09-09 09:09 UTC
查看更新说明
Changelog
- Fix: in the docker image, environment variables
SILENTandVERBOSEnow work properly. - In the docker image, the host "0.0.0.0" is set instead of "localhost".
- The proxy will not attempt to start anymore when setting "https://" in the target.
- Target port will fall back to 80 when protocol is "http://" in the target.
- Client failures to send the request forward to the target now produce a readable error response.
- Fix: in the docker image, environment variables
- v1.1.2Release 1.1.22022-07-26 · 2022-07-26 10:20 UTC
查看更新说明
No functional changes.
Version number was increased to trigger automated publishing Docker image to Docker Hub.
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
Requirements
We run all tests with Node.js versions 10 and 12. Higher versions could possibly work but are not currently supported.
Installation
To install the CLI globally:
npm install -g openapi-cop
To install the package locally (inside an existing NPM package) and run the proxy programatically:
npm install openapi-cop
Usage
There are three ways to run openapi-cop:
- Start it with the CLI (1).
- Run it programatically inside Node.js (2).
- Start a container based on the Docker image (3).
CLI Usage
The openapi-cop node package installs itself as an executable linked as openapi-cop. Run the command with
the --help flag to get information about the CLI:
Usage: openapi-cop [options]
Options:
-s, --file <file> path to the OpenAPI definition file
-h, --host <host> the host of the proxy server (default: "localhost")
-p, --port <port> port number on which to run the proxy (default: 8888)
-t, --target <target> full base path of the target API (format: http(s)://host:port/basePath)
--default-forbid-additional-properties disallow additional properties when not explicitly specified
--silent do not send responses with validation errors, just set validation headers
-w, --watch [watchLocation] watch for changes in a file or directory (falls back to the OpenAPI file)
and restart server accordingly
-v, --verbose show verbose output
-V, --version output the version number
-h, --help output usage information
The proxy validates the requests and responses in the communication with a target server. By default, the proxy will respond with a 500 status code when the validation fails.
{
"error": {
"message": "openapi-cop Proxy validation failed",
"request": {
"method": "POST",
"path": "/pets",
"headers": {
"host": "localhost:8888",
"user-agent": "curl/7.59.0",
"accept": "*/*",
"content-type": "application/json",
"content-length": "16"
},
"query": {},
"body": {
"data": "sent"
}
},
"response": {
"statusCode": 201,
"body": "{}",
"headers": {
"x-powered-by": "Express",
"openapi-cop-openapi-file": "7-petstore.yaml",
"content-type": "application/json; charset=utf-8",
"content-length": "2",
"etag": "W/\"2-vyGp6PvFo4RvsFtPoIWeCReyIC8\"",
"date": "Thu, 25 Jul 2019 13:39:58 GMT",
"connection": "close"
},
"request": {
"uri": {
"protocol": "http:",
"slashes": true,
"auth": null,
"host": "localhost:8889",
"port": "8889",
"hostname": "localhost",
"hash": null,
"search": null,
"query": null,
"pathname": "/pets",
"path": "/pets",
"href": "http://localhost:8889/pets"
},
"method": "POST",
"headers": {
"host": "localhost:8888",
"user-agent": "curl/7.59.0",
"accept": "*/*",
"content-type": "application/json",
"content-length": "16",
"accept-encoding": "gzip, deflate"
}
}
},
"validationResults": {
"request": {
"valid": true,
"errors": null
},
"response": {
"valid": false,
"errors": [
{
"keyword": "required",
"dataPath": "",
"schemaPath": "#/required",
"params": {
"missingProperty": "code"
},
"message": "should have required property 'code'"
}
]
},
"responseHeaders": {
"valid": true,
"errors": null
}
}
}
}
Two headers are added to the response:
-
openapi-cop-validation-result: contains the validation results as JSON.{ request: { valid: boolean; errors?: Ajv.ErrorObject[] | null; }, response: { valid: boolean; errors?: Ajv.ErrorObject[] | null; }, responseHeaders: { valid: boolean; errors?: Ajv.ErrorObject[] | null; } } -
openapi-cop-source-request: contains a simplified version of the original request sent by the client as JSON.{ method: string; path: string; headers: { [key: string]: string | string[]; }; query?: { [key: string]: string | string[]; } | string; body?: any; }
See the references of OpenAPI Backend and Ajv for more information.
When the --silent is provided, the proxy will forward the server's response body without modification. In this case,
the validation headers are still added.
Module Usage
To run the proxy programatically use runProxy, which returns a Promise<http.Server>:
import {runProxy} from 'openapi-cop';
const server = await runProxy({
port: 8888,
host: 'proxyhost',
targetUrl: 'http://targethost:8989',
apiDocPath: '/path/to/openapi-file.yaml',
defaultForbidAdditionalProperties: false,
silent: false
});
Docker Image Usage
We publish a Docker image lxlu/openapi-cop that you can use for your convenience. This means you can also run openapi-cop with something like
docker run --rm -p 8888:8888 --env TARGET=https://some-host-name:1234 --env FILE=some-openapi-document.json lxlu/openapi-cop
Read more information about the usage here.
FAQ
Contributing
If you want to contribute to openapi-cop, be sure to check the Contributing guidelines and the Contributing wiki page.