【发布时间】:2019-02-07 10:56:25
【问题描述】:
是否可以使用带有 swagger 的模板。
它是如何完成的。
我不想每次都重复 time、len 和 off 这三个属性。
看看这篇文章的结尾,我制作了一个“模板”来进行解释。
更多细节:
我有一个 JSON 响应结构,它总是返回一个始终具有相同属性的 JSON,但只有 data 的内容会发生变化。
data 可以是数组、字符串、数字、null 或对象。
这取决于 Api 的函数处理。
{
time: "2019-02-01T12:12:324",
off: 13,
len: 14,
data: [
"Last item in the row of 14 items :-)"
]
}
请参阅本文末尾的 Swagger 定义示例。 它是一个yaml,可以粘贴到https://editor.swagger.io/的swagger编辑器中
在 swagger 文档 (yaml) 中,我不想重复静态重复出现的项目,这不会改变任何其他请求的功能。
如果问题不够准确,请告诉我。
swagger: "2.0"
info:
description: ""
version: 1.0.0
title: "Templating?"
contact:
email: "someone@somewhere.com"
host: localhost
basePath: /api
paths:
/items:
get:
summary: "list of items"
produces:
- application/json
responses:
200:
description: "successful operation"
schema:
$ref: "#/definitions/Items"
/item/{id}:
get:
summary: "specific item"
produces:
- application/json
parameters:
- name: id
in: path
description: "ID of the demanded item"
required: true
responses:
200:
description: "successful operation"
schema:
$ref: "#/definitions/Item"
definitions:
Items:
type: object
description: ""
properties:
time:
type: string
format: date-time
description: "date-time of the request"
off:
type: integer
description: "index 0 based offset of list data"
default: 0
len:
type: integer
description: "overall amount of items returned"
default: -1
data:
type: array
items:
$ref: "#/definitions/ListingItem"
Item:
type: object
description: ""
properties:
time:
type: string
format: date-time
description: "date-time of the request"
off:
type: integer
description: "index 0 based offset of list data"
default: 0
len:
type: integer
description: "overall amount of items returned"
default: -1
data:
$ref: "#/definitions/InfoItem"
ListingItem:
type: integer
description: "ID of the referenced item"
InfoItem:
type: object
properties:
id:
type: string
text:
type: string
根据@Anthon 的回答,我想到这在某种程度上是我需要的构造。实际上它是从“模板”继承的:
...
templates:
AbstractBasicResponse:
properties:
time:
type: string
format: date-time
description: "date-time of the request"
off:
type: integer
description: "index 0 based offset of list data"
default: 0
len:
type: integer
description: "overall amount of items returned"
default: -1
definitions:
Items:
type: object
extends: AbstractBasicResponse
properties:
data:
type: array
items:
$ref: "#/definitions/ListingItem"
Item:
type: object
extends: AbstractBasicResponse
properties:
data:
$ref: "#/definitions/InfoItem"
ListingItem:
type: integer
description: "ID of the referenced item"
InfoItem:
type: object
properties:
id:
type: string
text:
type: string
...
【问题讨论】:
-
你能把代码贴在这里而不是 pastebin 吗?谢谢
-
我现在确实将 swagger yaml 粘贴到了这篇文章中。 (在第一次运行时,我不想抱怨代码太多而文本太少。)
-
是的,这很正常,您可以发布相关代码吗?导致问题的实际部分?
-
没有导致“运行”问题的部件。我只想部分模板化我的文档中一些重复出现的部分。到目前为止,我的解决方法是在 swagger 规范中一直额外复制这些东西。
-
得到了你。您想删除重复项吗?
标签: json yaml swagger definition