2

我正在尝试使用 Dredd 来测试我的 OpenAPI 指定的 API,但我无法让 Dredd 识别我的 POST 请求的 JSON 正文,它不断发送我的 POST 请求的正文为空。根据 Dredd 的文档,它使用 schema.example 来表示“in”:“body”,这正是我正在做的事情,但 Dredd 不断发出带有空正文的 POST。

我已经尝试过 OpenAPI3 和 OpenAPI2,结果相同。我在 OpenAPI2 规范中的 POST 操作如下所示:

  /availableCounters:
    post:
      summary: Get the available counters for a specified time range
      description: This API returns the available counters for the specific time range requested.
      responses:
        '200':
          description: OK
          schema:
            type: object
            properties:
              properties:
                type: array
                items:
                  $ref: '#/definitions/property_spec'
        '400':
          description: 'Could not retrieve available Counters: ${error}'
      parameters:
        - required: true
          name: body
          in: body
          schema:
            example: {"searchSpan": {"from": {"dateTime": "2019-01-20T21:50:37.349Z"},"to": {"dateTime": "2019-01-22T21:50:37.349Z"}}}
            type: object
            properties:
              searchSpan:
                $ref: '#/definitions/from_to'

但是当我使用 Dredd 来测试这个 OpenAPI 定义时,对于这个操作它不会发送它应该发送的正文:

request:
method: POST
uri: /availableCounters
headers:
    User-Agent: Dredd/8.0.0 (Windows_NT 10.0.17134; x64)

body:



expected:
headers:

statusCode: 200
bodySchema: {"type":"object","properties":{"properties":{"type":"array","items":{"$ref":"#/definitions/property_spec"}}},"definitions":{"property_spec":{"type":"object","properties":{"name":{"type":"string"},"type":{"type":"string","enum":["Double","String","DateTime"]}}}}}


actual:
statusCode: 400
headers:
    connection: close
    date: Tue, 12 Feb 2019 23:22:09 GMT
    content-type: text/plain; charset=utf-8
    server: Kestrel
    content-length: 96

bodyEncoding: utf-8
body:
Could not retrieve available Counters: TypeError: Cannot read property 'searchSpan' of undefined

我试过同时使用 schema.example 和 schema.x-example 但 Dredd 不会发送正文。正如我之前所说,我也尝试过 OpenAPI3,我得到了相同的结果。

任何帮助将不胜感激。

4

1 回答 1

2

问题很老,但问题仍然存在:如果缺少consumes字段,Dredd 似乎忽略了 body 参数。

所以试试:

/availableCounters:
  post:
    summary: Get the available counters for a specified time range
    consumes:
     - application/json
[...]
于 2019-02-28T12:45:06.003 回答