我根据 DREDD 的规范测试我的 Api(考虑到Dredd 支持的痛苦限制,用 Openapi3 编写)。不,我有一个端点,如果 Accept-header 设置为如此,它会生成 CSV 数据。
'/my-endpoint':
summary: ...
description: ...
get:
# parameters:
# -
# in: header
# name: Accept
# description: "Response format: application/json or text/csv"
# example: "text/csv"
responses:
'200':
description: ...
content:
text/csv:
schema:
type: string
example:
summary: 'csv table'
value: 'cell1, cell2'
Run Code Online (Sandbox Code Playgroud)
当我使用 Dredd 运行测试时,测试失败并显示
expected:
headers:
body:
[
{
"key": "summary",
"value": "csv table"
},
{
"key": "value",
"value": "cell1, cell2"
}
]
statusCode: 200
Run Code Online (Sandbox Code Playgroud)
显然存在一些误解,Dredd 期望的仍然是 JSON。此外,API 并未被告知生成 CSV 版本。如果我在代码 abvoe 中的 Accept 标头中提交,我会得到完全相同的结果 - 上面的expecetd 结果和实际结果 my-endpoint-data 的 JSON 版本以及广告警告:
warn: API description parser warning in .../tmp/transformed.specs.yml: 'Parameter Object' 'name' in location 'header' should not be 'Accept', 'Content-Type' or 'Authorization'
Run Code Online (Sandbox Code Playgroud)
我在这里和这里读到:Header parameters named Accept, Content-Type and Authorization are not allowed. To describe these headers, use the corresponding OpenAPI keywords-但它们是什么?根据this和this page 似乎足以指定给定类型的响应,但这显然不足以告诉 Dredd 生成这样的标头。
您收到错误,因为example键的值应该是字面示例值。summary因此,在您的情况下,它被视为具有和属性的对象value。
将您的定义更改为:
content:
text/csv:
schema:
type: string
example: 'cell1, cell2'
Run Code Online (Sandbox Code Playgroud)
或者,如果您想提供示例的摘要/描述,请改用examples:
content:
text/csv:
schema:
type: string
examples:
csv table:
summary: A CSV table with 2 cells
value: 'cell1, cell2'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3486 次 |
| 最近记录: |