我正在尝试使用Dredd来测试我的OpenAPI指定的API,但是我无法让Dredd识别我的POST请求的JSON主体,它会不断发送带有空主体的POST请求。 根据Dredd的documentation,它使用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,并且得到了相同的结果。
任何帮助将不胜感激。
答案 0 :(得分:1)
这个问题很老,但是问题仍然存在:如果缺少 consumps 字段,Dredd似乎会忽略body参数。
所以尝试:
/availableCounters:
post:
summary: Get the available counters for a specified time range
consumes:
- application/json
[...]