OpenAPI 规范 v3.1.0
OpenAPI 规范 v3.1.0
1. OpenAPI 规范
2. 简介
3. 定义
4. 规范
4.1 版本
4.2 格式
4.3 文档结构
4.4 数据类型
4.5 富文本格式化
4.6 URI 中的相对引用
4.7 URL 中的相对引用
4.8 模式
4.9 规范扩展
4.10 安全过滤
4.8.26 XML 对象
创建时间: 2025-05-29 16:38

一个元数据对象,允许更精细的 XML 模型定义。使用数组时,不会推断 XML 元素名称(对于单数/复数形式),并且应该使用 name 属性添加该信息。请参阅示例以了解预期行为。

4.8.26.1 固定字段
字段名称类型描述
name字符串替换用于描述的模式属性的元素/属性的名称。在 items 中定义时,它将影响列表中各个 XML 元素的名称。在 type 为 array(在 items 之外)时定义时,它将影响包装元素,并且仅当 wrapped 为 true 时才会生效。如果 wrapped 为 false,则将被忽略。
namespace字符串命名空间定义的 URI。此 URI必须采用绝对 URI 的形式。
prefix字符串要用于name的前缀。
attribute布尔值声明属性定义是否转换为属性而不是元素。默认值为 false
wrapped布尔值可以仅用于数组定义。表示数组是否已包装(例如,<books><book/><book/></books>)或未包装(<book/><book/>)。默认值为 false。仅当与 type 为 array(在 items 之外)一起定义时,此定义才生效。

此对象*可以使用 规范扩展 进行扩展。

4.8.26.2 XML 对象示例

XML 对象定义的示例包含在Schema 对象的属性定义中,并附带其 XML 表示形式的示例。###### 4.8.26.2.1 无 XML 元素基本字符串属性

{
    "animals": {
        "type": "string"
    }
}
animals:
  type: string
<animals>...</animals>

基本字符串数组属性(wrapped 默认值为 false

{
    "animals": {
        "type": "array",
        "items": {
            "type": "string"
        }
    }
}
animals:
  type: array
  items:
    type: string
<animals>...</animals>
<animals>...</animals>
<animals>...</animals>
4.8.26.2.2 XML 名称替换
{
  "animals": {
    "type": "string",
    "xml": {
      "name": "animal"
    }
  }
}
animals:
  type: string
  xml:
    name: animal
<animal>...</animal>
4.8.26.2.3 XML 属性、前缀和命名空间

在此示例中,显示了完整的模型定义。

{
  "Person": {
    "type": "object",
    "properties": {
      "id": {
        "type": "integer",
        "format": "int32",
        "xml": {
          "attribute": true
        }
      },
      "name": {
        "type": "string",
        "xml": {
          "namespace": "https://example.com/schema/sample",
          "prefix": "sample"
        }
      }
    }
  }
}
Person:
  type: object
  properties:
    id:
      type: integer
      format: int32
      xml:
        attribute: true
    name:
      type: string
      xml:
        namespace: https://example.com/schema/sample
        prefix: sample
<Person id="123">
    <sample:name xmlns:sample="https://example.com/schema/sample">example</sample:name>
</Person>
4.8.26.2.4 XML 数组

更改元素名称

{
  "animals": {
    "type": "array",
    "items": {
      "type": "string",
      "xml": {
        "name": "animal"
      }
    }
  }
}
animals:
  type: array
  items:
    type: string
    xml:
      name: animal
<animal>value</animal>
<animal>value</animal>

外部的name属性对XML没有影响

{
  "animals": {
    "type": "array",
    "items": {
      "type": "string",
      "xml": {
        "name": "animal"
      }
    },
    "xml": {
      "name": "aliens"
    }
  }
}
animals:
  type: array
  items:
    type: string
    xml:
      name: animal
  xml:
    name: aliens
<animal>value</animal>
<animal>value</animal>

即使数组被包装,如果未显式定义名称,则内部和外部将使用相同的名称

{
  "animals": {
    "type": "array",
    "items": {
      "type": "string"
    },
    "xml": {
      "wrapped": true
    }
  }
}
animals:
  type: array
  items:
    type: string
  xml:
    wrapped: true
<animals>
  <animals>value</animals>
  <animals>value</animals>
</animals>

为了克服上面示例中的命名问题,可以使用以下定义

{
  "animals": {
    "type": "array",
    "items": {
      "type": "string",
      "xml": {
        "name": "animal"
      }
    },
    "xml": {
      "wrapped": true
    }
  }
}
animals:
  type: array
  items:
    type: string
    xml:
      name: animal
  xml:
    wrapped: true
<animals>
  <animal>value</animal>
  <animal>value</animal>
</animals>

影响内部和外部名称

{
  "animals": {
    "type": "array",
    "items": {
      "type": "string",
      "xml": {
        "name": "animal"
      }
    },
    "xml": {
      "name": "aliens",
      "wrapped": true
    }
  }
}
animals:
  type: array
  items:
    type: string
    xml:
      name: animal
  xml:
    name: aliens
    wrapped: true
<aliens>
  <animal>value</animal>
  <animal>value</animal>
</aliens>

如果我们更改外部元素但未更改内部元素

{
  "animals": {
    "type": "array",
    "items": {
      "type": "string"
    },
    "xml": {
      "name": "aliens",
      "wrapped": true
    }
  }
}
animals:
  type: array
  items:
    type: string
  xml:
    name: aliens
    wrapped: true
<aliens>
  <aliens>value</aliens>
  <aliens>value</aliens>
</aliens>
最后更新: -