2014-03-28 40 views
1

我有一个rails应用程序,使用活动记录序列化器来响应json或html。我正在使用它来创建一个公共API。我正在使用设计简单的http进行基本身份验证。如何使用rails和swagger文档为嵌套资源创建API方法

我使用Swagger文档,通过以下宝石。

gem 'swagger-docs' #for creating the swagger json format gem 'swagger-ui_rails' #for generating the swanky active docs UI

我已经能够成功创建JSON我的主顶级资源,通过控制器。例如,在“products_controller.rb”中:

swagger_controller :products, "Product Management" 

    swagger_api :index do 
    summary "Fetches all Products" 
    param :query, :page, :integer, :optional, "Page number" 
    response :unauthorized 
    response :success 
    end 

问题是如何设置嵌套资源。

因此,在我的架构中,Products has_many Slots/Slots属于Product。所以,在我的“slots_controller.rb”,我把它以同样的方式设置:

swagger_controller :slots, "Slot Management" 

    swagger_api :index do 
    summary "Fetches all Slots for a Product" 
    param :query, :page, :integer, :optional, "Page number" 
    param :form, :product_id, :integer, :required, "Product id" 
    response :unauthorized 
    response :success 
    end 

我认为这是一厢情愿的想法,与控制器动作与产品ID,以工作一起提供的:product_id帕拉姆找到给定产品的插槽,然后可以自动魔术般地将其解释为嵌套资源。这似乎不是,而是我在扬鞭插槽API方法寻找:中

/api/v1/slots.json 

代替

/api/v1/products/#{product.id}/slots 

如何设置我的swagger_controller产生的插槽正确的嵌套的URL结构?

回答

1

不知道你已经解决了这一个,但你可以使用:path param_type像这样:

swagger_api :index do 
    summary "Fetches all Slots for a Product" 
    param :path, :product_id, :integer, :required, "Product id" 
    param :query, :page, :integer, :optional, "Page number" 
    response :unauthorized 
    response :success 
end 
+1

感谢马克。事实上,我最终放弃了放荡不羁,而是使用[Slate](https://github.com/tripit/slate)来记录我的API。我可能会回头大举提供活跃的文档,但与Slate迭代的方式暂时更快。 –

+0

授权内容如何?你如何管理? –

相关问题