【问题标题】:Api blueprint failing with dredd on a realtime api?实时api上的dredd导致api蓝图失败?
【发布时间】:2017-11-17 11:59:39
【问题描述】:

我正在将 dredd 从 1.08 升级到最新版本,同时我正在尝试验证我们的 api 文档,它是用实时测试 api 用蓝图编写的,但它失败了。

由于测试是针对实时 api 运行的,因此从 api 返回的响应包含与模糊打印文档中指定的值不同的值。我从 dredd 收到以下错误。

有人可以帮我弄清楚吗? :)

body: At '/data/email' No enum match for: "dredd_testzz@keyflow.se"

body: At '/data/firstName' No enum match for: "Sniper"

body: At '/data/lastName' No enum match for: "Wolf"

body: At '/data/verified' No enum match for: false

## `ResponseSchema` (object)

+ email: `john.doe@example.com` (string, required) - Email address 
+ firstName: John (string, required) - First name 
+ lastName: Doe (string, required) - Last name 
+ verified: true (boolean, required) - True 

# Group Account

## Login [/login/?]
Login user

### Login [POST]
Authentication required.

+ Request (application/json)

    + Attribute (LoginInputSchema)

+ Response 200 (application/json; charset=UTF-8)

+ Attribute
    + status: 200 (number, required, fixed)
    + data (ResponseSchema, required, fixed)

dredd 生成的 JSON Schema 如下

bodySchema: {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "status": {
      "type": "number",
      "enum": [
        200
      ]
    },
    "data": {
      "type": "object",
      "properties": {
        
        "email": {
          "type": "string",
          "enum": [
            "john.doe@example.com"
          ],
          "description": "Email address of the guest."
    },
    "firstName": {
      "type": "string",
      "enum": [
        "John"
      ],
      "description": "First name of the guest."
    },
    "lastName": {
      "type": "string",
      "enum": [
        "Doe"
      ],
      "description": "Last name of the guest."
    },
    "verified": {
      "type": "boolean",
      "enum": [
        true
      ],
      "description": "The user is verified or not"
    },
    
  },
      "required": [
        "email",
        "firstName",
        "lastName",
        "verified",
      ],
      "additionalProperties": false
    }
  },
  "required": [
    "status",
    "data"
  ]
}

【问题讨论】:

    标签: oracle apiblueprint apiary.io dredd mson


    【解决方案1】:

    TL;DR: 尝试在 API 蓝图文档中使用 fixed-type 而不是 fixedfixed 要求样本值是实际值。


    更详细的解释:

    body: At '/data/email' No enum match for: "dredd_testzz@keyflow.se"
    

    这意味着被测服务器返回的响应包含可正确解析的 JSON 正文,但根据 API 描述提供的架构,该正文无效。

    错误指向/data/email,表示{"data": {"email": ...属性有问题。此外,它提到enum 的值是预期的,并且实际响应包含dredd_testzz@keyflow.se,这是枚举所不允许的。其他错误类似。

    查看 API 描述,响应中预期内容的规范如下:

    + Attribute
        + status: 200 (number, required, fixed)
        + data (ResponseSchema, required, fixed)
    

    fixed 属性,如 MSON 规范的 4.3 Nested Member Types 部分所述,不仅修复了结构,还修复了所有值,并进一步向下传播到数据结构:

    ...可以指定 fixed 来指示一个“值对象”,其中所有属性都必须存在,并且属性的值必须是在其嵌套成员类型中指定的值(如果有) .此外,这样的对象类型结构不得包含任何其他属性。

    我想你想改用fixed-type,它只修复了结构。 Dredd 文档的Making Dredd Validation Stricter 部分也进一步解释了这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      相关资源
      最近更新 更多