在 openapi yaml 中指定动态键

Specify Dynamic keys in openapi yaml

我想创建这样的响应结构,其中 fieldProfile(配置文件类型)或 summaryProfile 是可选的。即外键是动态的和可选的

{
    "fieldProfile": [
        "metric1",
        "metric2",
        "metric3"
    ],
    "summaryProfile": [
        "metric1",
        "metric2",
        "metric3"
    ]
}

或者

{
    "fieldProfile": [
        "metric1",
        "metric2",
        "metric3"
    ]
} // Here Summary Profile is removed, similarly we can have a response of summaryProfile or have both in response

我正在创建这样的响应模式,但在这里我希望 metricType (fieldProfile/summaryProfile) 成为指标(指标列表)的键。

ProfileMetrics:
  description: List of metrics available in Field Profile
  type: object
  required:
    - metricType
    - metrics
  properties:
    metrics:
      type: array
      items:
        type: object
        required:
          - metricName
        properties:
          metricName:
            type: string

感谢任何帮助

您的架构可以定义如下:

MySchema:
  type: object
  properties:
    fieldProfile:
      type: array
      items:
        type: string
    summaryProfile:
      type: array
      items:
        type: string

  # At least fieldProfile or summaryProfile (or both) must be present
  minProperties: 1

代替minProperties: 1,你可以使用这个anyOf + required结构,在这个例子中它会达到同样的效果:

  anyOf:
    - required: [fieldProfile]
    - required: [summaryProfile]