我正在Swagger 2.0的帮助下编写API文档。我已经生成了一个API,其中响应是在书籍的数组中工作正常。
[{
"id": 1,
"book_name": "The Complete Reference Java8",
"author": "Herbert Schidt",
"genre": "Technology"
}, {
"id": 2,
"book_name": "C Programming",
"author": "Dennis Ritchie",
"genre": "Technology"
}]
Swagger
/fetchBooks:
get:
description: |
Returns an array of book objects.
responses:
200:
description: Successful response
schema:
title: ArrayOfBooks
type: array
items:
type: object
properties:
id:
type: integer
book_name:
type: string
author:
type: string
genre:
type: string
好吧,我想在JSONObject
中只在一个API中发送一个书籍详细信息,我应该采用什么样的模式类型,因为我试过对象不起作用。
{
"id": 1,
"book_name": "The Complete Reference Java8",
"author": "Herbert Schidt",
"genre": "Technology"
}
扬鞭
/fetchBook:
get:
description: |
Returns a book object
parameters:
- name: id
in: query
description: Books Id's
reqrequired: true
type: integer
format: int
responses:
200:
description: Successful response
schema:
type: object <-- What type should I specify for JSONObject here
items:
type: object
properties:
id:
type: integer
book_name:
type: string
author:
type: string
genre:
type: string
由于对象不起作用,swagger未显示JSON
格式。
现状:
预期国家:
答案 0 :(得分:2)
/fetchBook:
get:
description: |
Returns a book object
parameters:
- name: id
in: query
description: Books Id's
required: true
type: integer
format: int
responses:
'200':
description: Successful response
schema:
type: object
properties:
id:
type: integer
book_name:
type: string
author:
type: string
genre:
type: string
你遇到的问题是所需文件中的拼写错误
,以及单个对象响应的正确语法
答案 1 :(得分:2)
提示:如果您想在多个操作中重复使用相同的模式 - 例如只有一个Book
和一个ArrayOfBooks
,您可以在definitions
部分定义架构,在其他地方$ref
定义架构。
paths:
/fetchBooks:
get:
...
responses:
200:
description: Successful response
schema:
$ref: '#/definitions/ArrayOfBooks' # <--------
/fetchBook:
get:
...
responses:
200:
description: Successful response
schema:
$ref: '#/definitions/Book' # <--------
definitions:
Book:
type: object
properties:
id:
type: integer
book_name:
type: string
author:
type: string
genre:
type: string
ArrayOfBooks:
type: array
items:
$ref: '#/definitions/Book' # <--------
此外,如果这是一个正在开发的新API而不是现有的API,GET /fetchBooks
中的“fetch”是多余的(GET = fetch)。考虑删除“抓取”并仅使用GET /books
和GET /book?id=...
。