Openapi3和CSV响应(针对Dredd)



我用DREDD根据其规范测试我的Api(考虑到DREDD对支持的痛苦限制,在Openapi3中编写(。不,我有一个端点,如果Accept标头设置为这样,它会生成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'

当我用Dredd运行测试时,测试失败


expected: 
headers: 
body: 
[
{
"key": "summary",
"value": "csv table"
},
{
"key": "value",
"value": "cell1, cell2"
}
]
statusCode: 200

显然有一些误解,Dredd仍然期望JSON。此外,API没有被告知要生产CSV版本。若我在代码abvoe的Accept头中提交,我会得到完全相同的结果——上面的expected结果和实际结果是我的端点数据的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'

我在这里和这里读:Header parameters named Accept, Content-Type and Authorization are not allowed. To describe these headers, use the corresponding OpenAPI keywords-但它们是什么?根据这个和这个页面,指定给定类型的响应似乎已经足够了,但这显然不足以告诉Dredd生成这样的头。

您得到了一个错误,因为example键的值是一个文本示例值。因此,在您的情况下,它被视为具有summaryvalue属性的对象。

将您的定义更改为:

content:
text/csv:
schema:
type: string
example: 'cell1, cell2'

或者,如果您想提供示例的摘要/描述,请使用examples

content:
text/csv:
schema:
type: string
examples:
csv table:
summary: A CSV table with 2 cells
value: 'cell1, cell2'

最新更新