API 服务器和基本路径
所有 API 端点都相对于基本 URL。例如,假设基本 URL 为 https://api.example.com/v1
,则 /users
端点指的是 https://api.example.com/v1/users
。
1https://api.example.com/v1/users?role=admin&status=active2\________________________/\____/ \______________________/3server URL endpoint query parameters4path
在 OpenAPI 3.0 中,您可以使用 servers
数组来指定 API 的一个或多个基本 URL。servers
替换了 OpenAPI 2.0 中使用的 host
、basePath
和 schemes
关键字。每个服务器都有一个 url
和一个可选的 Markdown 格式的 description
。
1servers:2 - url: https://api.example.com/v1 # The "url: " prefix is required
您还可以拥有多个服务器,例如,生产环境和沙盒环境
1servers:2 - url: https://api.example.com/v13 description: Production server (uses live data)4 - url: https://sandbox-api.example.com:8443/v15 description: Sandbox server (uses test data)
服务器 URL 格式
服务器 URL 格式遵循 RFC 3986,通常如下所示
1scheme://host[:port][/path]
主机可以是名称或 IP 地址(IPv4 或 IPv6)。OpenAPI 2.0 中的 WebSocket 方案 ws:// 和 wss:// 在 OpenAPI 3.0 中也受支持。有效服务器 URL 的示例
1https://api.example.com2https://api.example.com:8443/v1/reports3http://localhost:3025/v14http://10.0.81.36/v15ws://api.example.com/v16wss://api.example.com/v17/v1/reports8/9//api.example.com
如果服务器 URL 是相对的,则会针对托管给定 OpenAPI 定义文件的服务器进行解析(更多信息请参见下方)。注意:服务器 URL 不得包含查询字符串参数。例如,这是无效的
1https://api.example.com/v1?route=
如果未提供 servers
数组或为空,则服务器 URL 默认为 /
1servers:2 - url: /
服务器模板
服务器 URL 的任何部分(方案、主机名或其部分、端口、子路径)都可以使用变量进行参数化。变量在服务器 URL 中用 {花括号} 表示,如下所示
1servers:2 - url: https://{customerId}.saas-app.com:{port}/v23 variables:4 customerId:5 default: demo6 description: Customer ID assigned by the service provider7 port:8 enum:9 - '443'10 - '8443'11 default: '443'
与路径参数不同,服务器变量不使用 schema
。相反,它们被假定为字符串。变量可以具有任意值,或者可以限制为 enum
。在任何情况下,都需要一个 default
值,如果客户端未提供值,则将使用该值。变量 description
是可选的,但很有用,并且支持 Markdown (CommonMark) 进行富文本格式设置。服务器模板的常见用例
- 指定多个协议(例如 HTTP 与 HTTPS)。
- SaaS(托管)应用程序,其中每个客户都有自己的子域。
- 不同地理区域的区域服务器(例如:Amazon Web Services)。
- 用于 SaaS 和本地 API 的单个 API 定义。
示例
HTTPS 和 HTTP
1servers:2 - url: http://api.example.com3 - url: https://api.example.com
或使用模板
1servers:2 - url: '{protocol}://api.example.com'3 variables:4 protocol:5 enum:6 - http7 - https8 default: https
注意:这两个示例在语义上是不同的。第二个示例显式将 HTTPS 服务器设置为 default
,而第一个示例没有默认服务器。
生产、开发和暂存
1servers:2 - url: https://{environment}.example.com/v23 variables:4 environment:5 default: api # Production server6 enum:7 - api # Production server8 - api.dev # Development server9 - api.staging # Staging server
SaaS 和本地
1servers:2 - url: "{server}/v1"3 variables:4 server:5 default: https://api.example.com # SaaS server
不同地理区域的区域端点
1servers:2 - url: https://{region}.api.cognitive.microsoft.com3 variables:4 region:5 default: westus6 enum:7 - westus8 - eastus29 - westcentralus10 - westeurope11 - southeastasia
覆盖服务器
全局 servers
数组可以在路径级别或操作级别被覆盖。如果某些端点使用与 API 的其余部分不同的服务器或基本路径,这将非常方便。常见示例包括
- 文件上传和下载操作的不同基本 URL,
- 已弃用但仍可用的端点。
1servers:2 - url: https://api.example.com/v13
4paths:5 /files:6 description: File upload and download operations7 servers:8 - url: https://files.example.com9 description: Override base path for all operations with the /files path10 ...11
12/ping:13 get:14 servers:15 - url: https://echo.example.com16 description: Override base path for the GET /ping operation
相对 URL
servers
数组中的 URL 可以是相对的,例如 /v2
。在这种情况下,URL 会针对托管给定 OpenAPI 定义的服务器进行解析。这在托管在客户自己的服务器上的本地安装中非常有用。例如,如果托管在 http://localhost:3001/openapi.yaml
的定义指定了 url: /v2
,则 url
将解析为 http://localhost:3001/v2
。相对 URL 解析规则遵循 RFC 3986。此外,API 定义中的几乎所有其他 URL,包括 OAuth 2 流端点、termsOfService
、外部文档 URL 等,都可以指定为相对于服务器 URL。
1servers:2 - url: https://api.example.com3 - url: https://sandbox-api.example.com4
5# Relative URL to Terms of Service6info:7 version: 0.0.08 title: test9 termsOfService: /terms-of-use10
11# Relative URL to external documentation12externalDocs:13 url: /docs14 description: Find more info here15
16# Relative URLs to OAuth2 authorization and token URLs17components:18 securitySchemes:19 oauth2:20 type: oauth221 flows:22 authorizationCode:23 authorizationUrl: /oauth/dialog24 tokenUrl: /oauth/token
请注意,如果使用多个服务器,则相对 URL 指定的资源应存在于所有服务器上。