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

标签 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 数组?

最佳答案

如下更改您的架构。 EmployeesInfo应该定义为一个数组并具有 xml.wrapped = true .还要确保每个模式都指定了 xml.name带有相应的 XML 标记名称。

components:
  ...

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

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

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

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

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

components:
  responses:
    employeesAPI:
      description: This will return information about employees
      content:
        application/xml:
          schema:
            $ref: '#/components/schemas/EmployeesInfo'
          # Custom example of response XML
          example: |-
            <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>

关于swagger - 如何在 OpenAPI 中定义对象的 XML 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58008645/

相关文章:

swagger - 在 OpenAPI 3 中将参数传递给 `$ref`

openapi - Open API 生成器的身份验证

spring-boot - 如何使 Swagger 3 UI 以 DDMMYYYY 格式显示示例日期?

java - OpenAPI3 通过 Spring Boot 显示基于基本身份验证的方法

rest - 如何为 YAML 对象中的几个键设置一个值

java - Springfox Swagger 'Whitelabel Error Page' :com. google.common.base.Predicate 无法解析

swagger - 使用 Swashbuckle Asp.Net Core for ReDoc 添加 x-logo 供应商扩展

java - OffsetDateTime 在 GET 方法中产生 "No injection source found for a parameter of type public javax.ws.rs.core.response"