空手道框架的可选字段表达不起作用

问题描述 投票:0回答:1

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"}

karate
1个回答
0
投票

重用模式和涉及可选字段时存在边缘情况。文档中对此进行了解释:https://github.com/karatelabs/karate#remove-if-null

所以试试这个:

* json expected = karate.readAsString(`classpath:com/${schemapath}/JSONSchema.json`)
* match response == expected
© www.soinside.com 2019 - 2024. All rights reserved.