首页 > 解决方案 > OpenAPI 3 中等效的 XSD 选择

问题描述

我正在 OpenAPI 中构建一个 API,并希望构建与此 XSD 等效的东西:

<xs:complexType name="InputData"> 
    <xs:complexContent>
        <xs:sequence>
            <xs:element name="input1" type="string" minOccurs="1" maxOccurs="1"/>
            <xs:element name="input2" type="double" minOccurs="1" maxOccurs="1"/> 
            <xs:choice>
                <xs:element name="input3A" type="my:dataType" minOccurs="1" maxOccurs="1"/>
                <xs:element name="input3B" type="my:dataType" minOccurs="1" maxOccurs="1"/>
            </xs:choice>
        </xs:sequence>
    </xs:complexContent>
</xs:complexType>

我能来的最接近的是:

components:
  schemas:
    MyDataType:
      type: object
      properties:
        val1:
          type: string
        val2:
          type: number
          

    InputData:
      type: object
      properties:
        input1:
          type: string
        input2:
          type: string
        input3:
          oneOf:
            - $ref: '#/components/schemas/MyDataType'
            - $ref: '#/components/schemas/MyDataType'

但是,它不允许名称input3Ainput3B指定我提供的输入。

有什么帮助吗?

标签: yamlswaggeropenapi

解决方案


这里相当于你的 XSD:

components:
  schemas:
    MyDataType:
      type: object
      properties:
        val1:
          type: string
        val2:
          type: number
          

    InputData:
      allOf:
      - type: object
        properties:
          input1:
            type: string
          input2:
            type: string
        required: [input1, input2]
      - type: object
        oneOf:
          - properties:
              input3A:
                $ref: '#/components/schemas/MyDataType'
            required: [input3A]
          - properties:
              input3B:
                $ref: '#/components/schemas/MyDataType'
            required: [input3B]

推荐阅读