openapi - Openapi3 和 CSV 响应(用于 Dredd)
问题描述
我用 DREDD 对照它的规范测试我的 Api(考虑到用 Openapi3 编写,考虑到 Dredd 支持的痛苦限制)。不,我有一个端点,如果设置了 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'
当我使用 Dredd 运行测试时,测试失败
expected:
headers:
body:
[
{
"key": "summary",
"value": "csv table"
},
{
"key": "value",
"value": "cell1, cell2"
}
]
statusCode: 200
显然有些地方被误解了,Dredd 期望仍然是 JSON。此外,API 没有被告知生成 CSV 版本。如果我在代码 abvoe 中的 Accept 标头中提交,我会得到完全相同的结果 - 上面的预期结果和实际结果是 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'
我在这里和这里读到: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
andvalue
属性的对象。
将您的定义更改为:
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'
推荐阅读
- mysql - 为 Crystal Reports SQL 命令变量赋值
- mysql - 如何在创建新数据库时修复“SQLSTATE [HY000] [2002] 连接被拒绝”
- configuration - 分布式环境下如何配置spring-kafka的分区和复制?
- react-native - 构建应用程序后的屏幕旋转
- java - 当应用程序关闭或移动到其他应用程序时重新发送代码停止
- java - 如何在“onException”中设置标题以使其在骆驼路线结束之前可见?
- ios - 未调用 iOS 后台提取
- azure - 配置 Azure DevOps 电子邮件模板
- reactjs - 在 React 中拆分字符串
- spring - Spring Security:不同路径的多个 OpenID Connect 客户端?