如何在OpenAPI中定义对象的XML数组?

时间:2019-09-19 10:10:10

标签: swagger openapi swaggerhub

我正在使用OpenAPI 3.0和SwaggerHub设计API。我的API具有一个GET端点,该端点以XML格式返回一组雇员:

<Employees>
  <Employee>
    <EmpId>001</EmpId>
    <Name>Steven</Name>
    <Mobile>1-541-754-3010</Mobile>
    <EmailId>steven@yourcomany.com</EmailId>
  </Employee>
  <Employee>
    <EmpId>002</EmpId>
    <Name>Mark</Name>
    <Mobile>1-551-754-3010</Mobile>
    <EmailId>mark@yourcomany.com</EmailId>
  </Employee>
</Employees>

这是到目前为止我的OpenAPI YAML文件:

openapi: 3.0.0
info:
  title: General Document
  version: "1.0"
  contact:
    email: developer@email.com
  description: >
    # Introduction 

    This document describes a list of API's available. \

paths:
  /employees:
    get:
      description: This will return employees information in JSON and XML formats
      responses:
        200:
          $ref: '#/components/responses/employeesAPI'

components:
  responses:
    employeesAPI:
      description: This will return information about employees
      content:
        application/xml:
          schema:
            $ref: '#/components/schemas/EmployeesInfo'

  schemas:
    Employee:
      type: object
      required:
        - EmpId
        - Name
        - Mobile
        - EmailId
      properties:
        EmpId:
          type: string
          example: Employee id goes here
          description: Employee id
        Name:
          type: string
          example: Employee name goes here
          description: Employee name
        Mobile:
          type: string
          example: Employee mobile goes here
          description: Employee mobile
        EmailId:
          type: string
          example: Employee email goes here
          description: Employee email

    EmployeesInfo:
      type: object
      required:
        - Employee
      properties:
        Employee:
          $ref: '#/components/schemas/Employee'
      xml:
        name: EmployeesInfo

# Added by API Auto Mocking Plugin
servers:
  - description: SwaggerHub API Auto Mocking
    url: https://virtserver.swaggerhub.com/name2200/test/1.0

问题是SwaggerHub中显示的XML响应示例与预期的响应XML不匹配。

如何正确定义对象的XML数组?

1 个答案:

答案 0 :(得分:0)

如下更改您的架构。 rpy2.rinterface.R_BUILD_VERSION应该定义为数组,并且具有xml.wrapped = EmployeesInfo。另外,请确保每个架构都使用相应的XML标签名称指定true

xml.name


Swagger UI将显示如下响应示例(此示例是根据响应架构自动生成的):

components:
  ...

  schemas:
    Employee:
      type: object
      ...
      xml:
        name: Employee

    EmployeesInfo:
      type: array
      items:
        $ref: '#/components/schemas/Employee'
      xml:
        name: Employees
        wrapped: true

如果要显示自定义示例,例如一个有2名员工的数组,添加一个自定义响应示例:

<EmployeesInfo>
    <Employee>
        <EmpId>Employee id goes here</EmpId>
        <Name>Employee name goes here</Name>
        <Mobile>Employee mobile goes here</Mobile>
        <EmailId>Employee email goes here</EmailId>
    </Employee>
</EmployeesInfo>