1

这种模式在我的项目中反复出现(六次):

        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}'

这种重复图案的内部 ( {various schema}) 因每次使用而异。我想为其中的每一个引用共享代码,而不是重复自己。我通常会使用$ref,但由于变量位,这似乎在这里不起作用。

我试图anyOf为我工作,但它只有助于改变propertiesa object,但我试图改变itemsa array

有什么我想念的吗?可能是一个小的重构以使其适合可重用的模式?

4

1 回答 1

1

您可以定义您的重复模式,除了items约束,然后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'
于 2016-08-22T08:53:34.693 回答