Json Rswag架构不会因数组中对象的无效属性而失败
我有以下rswag测试:Json Rswag架构不会因数组中对象的无效属性而失败,json,ruby,swagger,rswag,Json,Ruby,Swagger,Rswag,我有以下rswag测试: response '200', 'response with array of objects' do schema type: :array, items: { type: :object, properties: { expected_id: { type: "integer", format: "int32" }, } } run_test!
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
}
架构类型::数组,
项目:{
类型::对象,
特性:{
应为_id:{type:“integer”,format:“int32”},
}
必需:[“预期的\u id”]#