尽管 POST 请求中缺少字段,但 SwaggerHub 模拟响应返回 201 [英] SwaggerHub mock response returns 201 despite missing fields in POST request

查看:50
本文介绍了尽管 POST 请求中缺少字段,但 SwaggerHub 模拟响应返回 201的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据以下定义在 SwaggerHub 模拟 POST 请求:

I'm trying to mock a POST request at SwaggerHub based on the following definition:

    post:
      summary: "Creates order"
      description: ""
      consumes:
      - application/json
      parameters:
      - name: "order"
        in: body
        description: "New order"
        schema:
          $ref: "#/definitions/Order"
      responses:
        201:
          description: "Order succesfully created."
        400:
          description: "Order can't be created"

模型定义为:

definitions:
  Order:
    type: object
    properties:
      id:
        type: string
        format: uuid
        example: d290f1ee-6c54-4b01-90e6-d701748f0851
      marketPair:
        type: integer
        format: "int64"
        example: "BTC_TRY"
      amount:
        type: number
        format: "int64"
        example: "1.3"
      price:
        type: integer
        format: "int32"
        example: "467"
      operationType:
        type: string
        description: "Type of operation"
        enum: 
        - "buy"
        - "sell"
        example: "buy"
      orderType:
        type: string
        description: "Order Type"
        enum:
        - "limit"
        - "market"
        - "stop"
        default: "limit"
        example: "limit"
    xml:
      name: "Order"

每次我尝试 POST 缺少字段的错误 JSON 甚至正文中根本没有 JSON 时,我仍然收到 201 代码,这绝对不应该是 201.

Every time I'm trying to POST bad JSON with missing fields or even with no JSON in body at all I'm still receiving 201 code which absolutely shouldn't be 201.

我的配置中是否缺少某些内容,或者 SwaggerHub 需要进行哪些更改才能识别我的规范并开始检查负载是否符合此端点的规范要求?

Is there something missing in my configuration or what changes does it need for SwaggerHub to recognise my specification and start checking if the payload matches the specification requirements for this endpoint?

推荐答案

模拟不检查输入中的必填字段,它只返回为操作定义的最低 HTTP 状态代码 - 在您的示例中,状态 201.

The mock does not check for required fields in the input, it simply returns the lowest HTTP status code from those defined for the operation – in your example, status 201.

来自 SwaggerHub 文档:

请注意,mock 不支持业务逻辑,即无法根据输入发送特定响应.

Note that the mock does not support business logic, that is, it cannot send specific responses based on the input.

...

模拟根据其响应和规范中定义的响应媒体类型为每个 API 操作生成静态响应.

The mock generates static responses for each API operation based on its responses and the response media types defined in the spec.

如果一个操作有多个响应代码,模拟返回状态代码最低的响应.例如,如果操作具有响应 201、202 和 400,则模拟返回 201 响应.

If an operation has multiple response codes, the mock returns the response with the lowest status code. For example, if an operation has responses 201, 202 and 400, the mock returns the 201 response.

您可能想向 SwaggerHub 开发人员提交功能请求.

You might want to file a feature request with SwaggerHub devs.

这篇关于尽管 POST 请求中缺少字段,但 SwaggerHub 模拟响应返回 201的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆