customer 对象返回 null 或包含如下所示的具有有效值的字段。在浏览页面 https://github.com/karatelabs/karate,可选字段部分并实现 JSON 模式中解释的逻辑时,代码不起作用
回复1:
{
"data": {
"itemDescription": "chocolate"
"itemAmount": 2.01
"customer":{
"id": "1234",
"firstName": "ABC",
"lastName": "XYZ"
}
"oId": "764328421"
}
}
回复2:
{
"data": {
"itemDescription": "chips"
"itemAmount": 4.99
"customer": null
"oId": "8394821"
}
}
Feature: <>
Scenario Outline: <>
Given
When method POST
Then status 200
* match response == read('classpath:com/<schemapath>/JSONSchema.json')
JSONSchema:
{
"data": {
"itemDescription": "#string"
"itemAmount": "#number"
"customer":"##({id:'#string',firstName:'#string',lastName:'#string'})"
"oId": "#string"
}
}
上面的代码对于响应 1 来说是通过的,但是对于响应 2 来说失败了,并显示以下错误消息 $.data.customer | $.data.customer数据类型不匹配(NULL:MAP) 无效的 {"id":"#string","firstName":"#string","lastName":"#string"}
重用模式和涉及可选字段时存在边缘情况。文档中对此进行了解释:https://github.com/karatelabs/karate#remove-if-null
所以试试这个:
* json expected = karate.readAsString(`classpath:com/${schemapath}/JSONSchema.json`)
* match response == expected