代码之家  ›  专栏  ›  技术社区  ›  Stan Reduta

尽管post请求中缺少字段,swagegerhub模拟响应返回201

  •  1
  • Stan Reduta  · 技术社区  · 6 年前

    我正试图根据以下定义在SwaggerHub模拟一个帖子请求:

        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"
    

    每次我试图发布带有缺失字段的坏JSON,或者即使在主体中没有JSON,我仍然会收到201代码,这绝对不应该是201。

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   Helen    6 年前

    mock不检查输入中的必需字段,它只是从为您的示例status 201中的操作定义的字段返回最低的HTTP状态代码。

    SwaggerHub documentation :

    注意,mock不支持业务逻辑,也就是说,它不能基于输入发送特定的响应。

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

    如果一个操作有多个响应代码,那么mock将返回状态代码最低的响应。例如,如果一个操作有响应201、202和400,那么mock将返回201响应。

    您可能想用swaggerhub devs提交一个功能请求。