首页 > 解决方案 > 如何区分 OpenAPI 模式中的多个属性?

问题描述

这里的文档解释了当 OpenAPI 模式依赖于属性的值时如何定义鉴别器。在我自己的项目中,我有一个依赖于两个属性值的模式,而不仅仅是一个。我想知道是否有任何方法可以在 OpenAPI 文件中对其进行建模?

假设我们有一个请求列表,其中包含两个必需的枚举属性:

其余属性的值在某种程度上取决于这两个字段的值。因此,我们希望根据这 2 个字段来区分 4 种可能的模式。我想到的一种可能的解决方案是使用这样的嵌套结构:

ListResponse: 
      type: array
      items:
        oneOf:
          - $ref: '#/components/schemas/NewRequestResponse'
          - $ref: '#/components/schemas/OldRequestResponse'
        discriminator:
          propertyName: requestType
          mapping:
            'New':
              oneOf:
                - $ref: '#/components/schemas/NewTemporaryRequestResponse'
                - $ref: '#/components/schemas/NewPermanentRequestResponse'
              discriminator:
                propertyName: periodType
                mapping:
                  'Temporary': '#/components/schemas/NewTemporaryRequestResponse'
                  'Permanent': '#/components/schemas/NewPermanentRequestResponse'
            'Old':
              oneOf:
                - $ref: '#/components/schemas/OldTemporaryRequestResponse'
                - $ref: '#/components/schemas/OldPermanentRequestResponse'
              discriminator:
                propertyName: periodType
                mapping:
                  'Temporary': '#/components/schemas/OldTemporaryRequestResponse'
                  'Permanent': '#/components/schemas/OldPermanentRequestResponse'
      uniqueItems: true

但似乎这不是一个有效的模式。那么,怎么可能做到呢?

标签: openapi

解决方案


推荐阅读