OpenAPI 编写指南
- 根对象
openapi: 3.0.0
info:
servers:
paths:
components:
security:
tags:
externalDocs:
- info 对象
info:
title: API标题
version: 1.0.0
description: API详细描述
termsOfService: https://...
contact:
name: 支持团队
url: https://support.example.com
email: support@example.com
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
- 路径操作
paths:
/endpoint:
get|post|put|patch|delete|options|head|trace:
summary: 简短描述
description: 详细说明
operationId: uniqueId
tags: [tag1]
parameters:
- $ref: '#/components/parameters/Param'
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Model'
responses:
'200':
$ref: '#/components/responses/Success'
- 参数定义规范
paths:parameters:
- name: paramName
in: path|query|header|cookie
description: 参数描述
required: true|false
deprecated: false
schema:
type: string|integer|boolean...
format: date-time|int32|uuid...
pattern: 'regex'
example: value
- 请求体定义
requestBody:
description: 请求体描述
required: true|false
content:
application/json:
schema:
$ref: '#/components/schemas/Model'
examples:
example1:
value:
key: value
- 响应定义
responses:
'200':
description: 成功响应描述
headers:
Header-Name:
schema: {type: string}
content:
application/json:
schema:
$ref: '#/components/schemas/Model'
'4xx':
$ref: '#/components/responses/Error'
- 组件重用
components:
schemas:
User:
type: object
properties:
id: {type: integer}
name: {type: string}
parameters:
UserIdParam:
name: userId
in: path
schema: {type: integer}
responses:
Success:
description: 成功响应
content:
application/json:
schema:
$ref: '#/components/schemas/Success'
securitySchemes:
BearerAuth:
type: http
scheme: bearer