Swagger json模式重复代码
此模式在我的项目中重复出现(六次):Swagger json模式重复代码,swagger,jsonschema,json-schema-validator,Swagger,Jsonschema,Json Schema Validator,此模式在我的项目中重复出现(六次): type: object properties: total: type: integer description: the count of all items that match the query hits: type: array description:
type: object
properties:
total:
type: integer
description: the count of all items that match the query
hits:
type: array
description: a single page of results
items:
$ref: '#/definitions/{various schema}'
这种重复模式的内部部分({variable schema}
)因每次使用而不同。我想为每一个引用共享代码,而不是重复我自己。我通常会使用$ref
,但由于变量位的原因,这在这里似乎不起作用
我试图使中的任何一个对我有效,但它只帮助改变对象的属性
,但我试图改变数组的项
有什么我遗漏的吗?可能是一个小的重构,以使其适合可重用的模式?您可以定义重复的模式,除了项
约束,然后在每个变体中使用allOf
您的可重用架构如下所示:
reusable:
type: object
properties:
total:
type: integer
description: the count of all items that match the query
hits:
type: array
description: a single page of results
当您想要定义变体时,可以使用allOf
添加可重用模式和附加约束:
variation1:
allOf:
- reusable
- properties:
hits:
items:
$ref: '#/definitions/variation_schema'
这使我的十行减为六行,仍然重复了六次。这不是我所希望的,但仍然比我所想的要好。谢谢