【发布时间】:2017-03-10 13:04:38
【问题描述】:
任何人都可以为控制器编写 JSON 模式的示例吗 1) 使用 CRUD 动作 2) 使用自定义操作
我有一个 api,我需要用 JSON 模式来描述它,但我不知道如何描述控制器。
我的模型 JSON 架构
{
"$schema": "http://json-schema.org/draft-04/hyper-schema",
"title": "Story",
"description": "Story",
"type": "object",
"definitions": {
"id": {
"description": "Unique identifier",
"type": "integer",
"example": 1
},
"user": { "$ref": "#/definitions/user" },
"title": {
"description": "Title",
"type": "string",
"example": "Example Title"
},
"updated_at": {
"description": "Updated date",
"type": "string",
"example": "Mon, 06 Mar 2017 11:07:34 UTC +00:00"
},
"created_at": {
"description": "Created date",
"type": "string",
"example": "Mon, 06 Mar 2017 11:07:34 UTC +00:00"
}
},
"properties": {
"id": {
"$ref": "#/definitions/id"
},
"user": {
"$ref": "#/definitions/user"
},
"title": {
"$ref": "#/definitions/title"
},
"updated_at": {
"$ref": "#/definitions/updated_at"
},
"created_at": {
"$ref": "#/definitions/created_at"
}
},
"required": [
"id",
"title",
"user"
],
"links": [
{
"title": "Index",
"description": "List of stories",
"href": "/api/stories",
"method": "GET",
"rel": "index",
"targetSchema": {
"type": "array",
"items": { "rel": "self" }
}
},
{
"title": "Show",
"description": "Show story",
"href": "/api/stories/:id",
"method": "GET",
"rel": "show",
"schema": {
"$ref": "#"
},
"targetSchema": {
"$ref": "#"
}
},
{
"title": "Create",
"description": "Create a story",
"href": "/api/stories",
"method": "POST",
"rel": "create",
"schema": {
"properties": {
"title": {
"$ref": "#/definitions/title"
}
}
}
},
{
"title": "Update",
"description": "Update a story",
"href": "/api/stories/:id",
"method": "PUT",
"rel": "update",
"schema": {
"properties": {
"title": {
"$ref": "#/definitions/title"
}
}
},
"targetSchema": {
"$ref": "#"
}
},
{
"title": "Destroy",
"description": "Destroy a story",
"href": "/api/stories/:id",
"method": "DELETE",
"rel": "destroy",
"schema": {
"$ref": "#"
},
"targetSchema": {
"$ref": "#"
}
}
]
}
【问题讨论】:
-
如果我没记错的话,API 文档工具似乎就是您正在寻找的东西,如果是这样,请查看 Swagger github.com/richhollis/swagger-docs
标签: ruby-on-rails json api jsonschema