【问题标题】:How to reuse objects in schema of API BluePrint?如何重用 API BluePrint 架构中的对象?
【发布时间】:2015-07-13 12:01:01
【问题描述】:

我创建了一个 API,它提供名为“缩略图”(/thumbnails) 的资源

{
    "name": "toto",
    "uri": "http://xxx"
}

另一方面,我有多个资源,其中包含该缩略图资源(例如 /articles、/videos ...):

{
    "name": "playlist",
    "thumbnail": 
    {
        "name": "toto",
        "uri": "http://xxx"
    }
}

当我在 BluePrint markdown 中编写这些 Web 服务的架构时,我希望能够重用我为缩略图架构创建的架构,以免重复架构中的代码。 我很喜欢 Trait 功能 (https://github.com/apiaryio/api-blueprint/issues/47),但我不知道它是否符合我的需求,以及它是否适用于 aglio 和 dredd。

你知道对我来说最好的办法吗?

【问题讨论】:

    标签: markdown apiblueprint apiary.io dredd aglio


    【解决方案1】:

    您可以使用 API 蓝图的 Attributes 部分中的 MSON 来定义可重用对象。

    您可以在hereherehere 中看到一些示例。

    【讨论】:

    • 请注意,目前要在 Aglio 中渲染这些内容需要您使用 beta 版本,可以通过 npm install -g aglio@beta 安装。很快它将作为 2.0 版发布。
    • 并且在dredd支持
    • 嗯,但我可以在我的 WS 架构中使用这些属性吗?我喜欢 Dredd 对模式进行测试。
    【解决方案2】:

    所以我更新了我的 dredd 并测试了 MSON。它看起来很酷而且很有效率。我对这个 md 有一个问题:

    # Data Structures
    
    ## Video (object)
    Definition of a video
    + id: 11111 (number) - The unique key
    + description: "my video" (string) - Free text of video
    + truc: "ffff" (string)
    
    # Group VideosTest
    
    ## Videos List [/videos]
    List of videos
    
    ### List of videos [GET]
    
    + Response 200 (application/json)
    
        + Attributes (array[Video])
    

    测试没有失败,但我的真实 API 中没有属性“truc”,并且我有一个属性 URL。所以我预计测试会失败。我错了吗 ?

    【讨论】:

    • 你能粘贴你真正的 API 所期望的吗?
    • [{"id": "1", "description": "desc", "url": "http://xxx"},{"id": "2", "description": "desc2", "url": "http://xxx"}}
    • 不幸的是,MSON 不支持数组中的多个元素。 :(
    • 我只有一个视频也有同样的问题,我回答并发布示例
    【解决方案3】:

    我举个例子:

    我的医学博士:

    # Data Structures
    
    ## Video (object)
    Definition of a video
        + id: 11111 (number, required) - The unique key
        + description: "my video" (string, required) - Free text of video
        + truc: "ffff" (string, required)
    
    ## A video [/videos/{id}]
    See a video
    
    + Parameters
    
        + id (required, string, `a-la-decouverte-des-metiers-projection-du-film`) ... L'id ou le slug de la video
    
    ### Select a video [GET]
    
    + Response 200 (application/json)
    
        + Attributes (Video)
    

    还有我的 API:

    {
    "id": "1111",
    "description": "description of the video",
    "uri": "http://"
    }
    

    测试没问题,但是我要dredd报错!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多