7

在 API 蓝图中,我希望为 POST 消息指定一个可选的 json 元素。例如;

    ### Add a new User [POST]
    To add a User send a JSON .....

    + Request (application/json)

    {
         "name": "A name",
         "age": 30
    }

我如何向 API 的读者表明年龄在 API 调用中是可选的,但仍显示它是一个整数?

~科林

4

2 回答 2

4

我推荐像这样的文档字段表。

### Source Fields

| field      | required | type | example                                | description |
|------------|:-:|-------------|----------------------------------------|-------------|
| name       | * | string      | `average-lead-time-frontend`           | Unique name for machines, used as reference key and for url usage. |
| title      | * | string      | `Average Lead Time Frontend`           | Title for humans. |
| sourcetype | * | string      | `team-avgLeadTime`                     | Source type identificator. |
| groups     |   | list        | `["engagement-analytics", "builder"]`  | List of groups where entity is member. |
| options    |   | object      | `{ "team": 21926 }`                    | Addition options for sourcetype. |
| data       |   | list        | `[70.3, 31.8]`                         | Actual data held by Source. |

生成的字段表:http: //i.stack.imgur.com/cxocx.png 在此处输入图像描述

于 2014-04-09T09:03:33.790 回答
2

目前没有专门的支持这样做。但是,实现这一目标的方法很少。

我更喜欢使用您喜欢的降价格式在请求描述中讨论它,例如:

### Add a new User [POST]
To add a User send a JSON .....

+ Request (application/json)

    Data about user being created. Where age attribute is optional.

    + Body

            {
                 "name": "A name",
                 "age": 30
            }

也许:

To add a User send a JSON .....

+ Request (application/json)

    Data about user being created. With following attributes are:

    + `name` ... name of the user
    + `age` (optional) ... age of the user

    + Body

            {
                 "name": "A name",
                 "age": 30
            }

有关更多示例,请参阅http://docs.gtdtodoapi.apiary.io文件夹集合资源。

您还可以始终指定描述正文有效负载的 JSON 模式。注意讨论消息体属性的专门支持正在制作中(https://github.com/apiaryio/api-blueprint/issues/25

于 2014-04-01T05:03:54.530 回答