Rswag 模式不会因数组中对象的无效属性而失败
Rswag schema does not fail for invalid properties of object in array
我有以下 rswag 测试:
response '200', 'response with array of objects' do
schema type: :array,
items: {
type: :object,
properties: {
expected_id: { type: "integer", format: "int32" },
}
}
run_test!
end
我的 API 回复 JSON:
[
"invalid_key": 1,
"invalid_key": 2
]
换句话说,根据上述架构的无效响应。
但是,测试不会失败。这是出乎意料的。
另一方面,如果我的 API 以空值数组响应:"[null]"
我得到我期望的响应:
Failure/Error: test.run
Rswag::Specs::UnexpectedResponse:
Expected response body to match schema: The property '#/0' of type null did not match the following type: object in schema 19e6c168-1da4-58a6-93fc-a33d9d91233a
所以我的测试是检查是否存在对象数组,但不是检查嵌套对象中的属性是什么是期待。我如何让它也检查属性?
我的架构中缺少 required
子句:
schema type: :array,
items: {
type: :object,
properties: {
expected_id: { type: "integer", format: "int32" },
}
required: ['expected_id'] # <- this was missing
}
如果我的端点生成的响应不包含 expected_id
键,这会引发测试错误。
我有以下 rswag 测试:
response '200', 'response with array of objects' do
schema type: :array,
items: {
type: :object,
properties: {
expected_id: { type: "integer", format: "int32" },
}
}
run_test!
end
我的 API 回复 JSON:
[
"invalid_key": 1,
"invalid_key": 2
]
换句话说,根据上述架构的无效响应。
但是,测试不会失败。这是出乎意料的。
另一方面,如果我的 API 以空值数组响应:"[null]"
我得到我期望的响应:
Failure/Error: test.run
Rswag::Specs::UnexpectedResponse:
Expected response body to match schema: The property '#/0' of type null did not match the following type: object in schema 19e6c168-1da4-58a6-93fc-a33d9d91233a
所以我的测试是检查是否存在对象数组,但不是检查嵌套对象中的属性是什么是期待。我如何让它也检查属性?
我的架构中缺少 required
子句:
schema type: :array,
items: {
type: :object,
properties: {
expected_id: { type: "integer", format: "int32" },
}
required: ['expected_id'] # <- this was missing
}
如果我的端点生成的响应不包含 expected_id
键,这会引发测试错误。