【问题标题】:Is the way to exclude property from body type (RAML)?是从体型(RAML)中排除属性的方法吗?
【发布时间】:2016-08-08 03:40:44
【问题描述】:

当您使用类型并使用 RAML 1.0 编写 API 时,有没有办法从请求正文中排除一个或多个属性

我会解释的。我有一个类型:带有一组属性的“订单”。我有一个资源/orders 和一个方法post,它允许用户创建新订单。 请求体是一个订单结构体json,响应也是一个订单结构体。

但我不希望用户在提交请求时指定订单 ID。但是该 id(以及更多的“仅响应”字段)将在响应中返回。我不想创建一个额外的类型,比如OrderRequest,然后用Order 类型继承它,也许有更优雅的解决方案?

所以我想有一种方法从请求正文中排除某些属性并保留其他属性以便使用它们的描述和示例。

感谢和抱歉我的英语:)

【问题讨论】:

    标签: api raml


    【解决方案1】:

    使用两种类型。第二个将是第一个的孩子。示例:

    #%RAML 1.0
    title: GitHub API
    version: v3
    baseUri: https://api.github.com
    mediaType:  application/json
    types:
      OrderCreating:
        type: object
        properties:
          products:
            description: List of product ids with amount.
            required: true
            type: object
            properties:
              []:
                type: number
          coupon?: string
      Order:
        type: OrderCreating
        properties:
          id: integer
          price: number
    ...
    /orders:
      post:
        body:
          application/json:
            type: OrderCreating
      /{orderId}:
        get:
          responses:
            200:
              body:
                application/json:
                  type: Order
    

    您还可以在类型声明中包含Library

    【讨论】:

      【解决方案2】:

      由于您不想创建具有继承的额外类型,您仍然可以将该字段标记为可选并记录它存在于响应中。

      【讨论】:

        猜你喜欢
        • 2018-06-21
        • 1970-01-01
        • 2018-10-29
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        相关资源
        最近更新 更多