是否有可能使用模板W /招摇。它是如何做。 我不想重复三个属性时,LEN每一次关闭。
看看这个帖子,我弥补了解释“模板”的结束。
更多详情:
我有一个JSON响应结构,该结构总是总是返回一个JSON具有相同属性但只有数据的内容是可能会改变。
数据可以是阵列,可以是一个字符串,一个数字,null或对象。 这取决于API的功能处理。
{
time: "2019-02-01T12:12:324",
off: 13,
len: 14,
data: [
"Last item in the row of 14 items :-)"
]
}
看到这个帖子对我招摇定义的例子结束。它是一种可以粘贴到在https://editor.swagger.io/招摇编辑一个YAML
在招摇文件(YAML)我不想重复静态reoccuring项目,在它们的功能对任何其他要求,这将不会改变。
让我知道,如果问题是不准确足够的了解。
swagger: "2.0"
info:
description: ""
version: 1.0.0
title: "Templating?"
contact:
email: "[email protected]"
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
基于安森@的回答它来到我的脑海里,这是有些我需要的结构。其实它是从一个“模板”继承:
...
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
...
您可能没有恢复到完全模板,有YAML内两件事情,以“undoubling”重复数据帮助:锚/别名和合并键。
an anchor(由&
引入)通过一个别名(*
)引用的一个例子是:
definitions:
Items:
type: object
description: ""
properties:
time:
type: string
format: date-time
description: "date-time of the request"
off: &index
type: integer
description: "index 0 based offset of list data"
default: 0
len: &amount
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: *index
len: *amount
data:
一个YAML解析器需要能够处理这个问题,但由于该别名指向加载后的同一个对象,使用数据的代码可能取决于加载的数据是如何处理不再起作用,因为在某些情况下的副作用是相同的。 你可以有多个别名指的是相同的锚。
所述merge key(<<
)处于映射的特殊键,使用它可以预先加载在那里它与一堆键值对发生映射。用锚/别名使用时,这是最有效的。随着您在一些更精细的控制,你可以这样做:
len: &altlen
type: integer
description: "overall amount of items returned"
default: -1
接着
len:
<<: &altlen
default: 42
然后将是相同做的:
len:
type: integer
description: "overall amount of items returned"
default: 42
合并密钥通常在解决由YAML解析器加载时间,所以有使用这些即使它们涉及锚和别名时,没有潜在的副作用。