OpenAPI完整结构与版本边界
把元信息、服务器、路径、模型、安全及引用组织成可验证合同。
学完这一章,你能够
- 把元信息、服务器、路径、模型、安全及引用组织成可验证合同。
- 把元信息、服务器、路径、模型、安全及引用组织成可验证合同。
完整文档从根结构开始
JSON必须有完整的外层对象,YAML需要合法缩进与唯一键。openapi声明规范版本,info.title与info.version描述API;contact、license和description提供联系、许可及说明,不能随意冒用第三方许可或真实支持邮箱。
servers中的基础地址与paths组合成实际URL。开发、测试、生产可分别声明,但教程的默认请求只指向独立本机服务,mbsky.com作为品牌展示不表示向生产站执行增删操作。
操作、参数、请求与响应
每个路径可含GET、POST、PUT、DELETE等操作,并用唯一operationId和tags组织。路径占位符需要对应同名且required=true的path参数;query、header、cookie分别定义类型、范围、默认值和序列化方式。
requestBody按媒体类型声明JSON或表单模型;responses按状态码声明说明、内容类型和Schema。204没有响应体;200、201、400、401、403、404、409等与真实行为对应,不能只写成功结果。
组件复用与数据模型
components可保存schemas、parameters、responses及securitySchemes,再通过$ref引用。用户模型区分基本资料、详情、创建和修改;产品与订单模型分别定义类别、库存、地址、条目数量和服务器计算字段,不能把全部字段都当客户端可写。
string/integer/number/boolean/array/object、enum、required、长度、数值范围及date/date-time等约束各有职责。默认值不代表服务器会自动填入;format也不是所有验证器都自动执行的检查。3.1使用JSON Schema相关语义,不能把3.0 nullable直接当成所有版本通用写法。
安全要求是合同,实施仍在服务器
apiKey、HTTP Basic/Bearer、OAuth2、OpenID Connect等由securitySchemes描述,再由security应用。security数组的不同对象是可选的OR,同一个对象里的多个方案是AND;操作级要求可覆盖全局,security:[]可明确取消继承。
历史implicit流程保留其用途与迁移背景,现代授权流程需按官方规范选择;JWT的bearerFormat只是提示,不会自动验证签名。登录/注册如果误继承全局Bearer要求,会形成尚未登录却必须先有令牌的矛盾。
规范、工具与实现分别检查
YAML/JSON可以表达相同合同,选择哪种格式不能替代校验。Editor、VS Code插件、Stoplight等辅助编辑;UI、ReDoc及静态文档工具各有展示目标;Codegen、OpenAPI Generator、NSwag等也有各自语言与版本范围。
保持统一路径命名、HTTP方法、业务版本和错误模型。URL版本、服务器基础路径版本、请求头/媒体类型版本是不同方案,必须与真实路由协商一致;规范通过不等于Postman、Prism、Microcks或其它工具已经运行。
OpenAPI3.0结构字段及条件
| 字段 | 形式 | 用途 | 条件 |
| openapi | 版本字符串 | 本包3.0.3 | 与业务版本info.version分开 |
| info | 对象;title/version必填 | 标题、描述、联系信息 | 品牌统一来自本站设置,不复制参考站名称 |
| servers | URL/variables数组 | 描述基础地址 | 本包/api/v1相对当前来源 |
| paths | 路径→方法对象 | 描述HTTP操作 | 每个path参数必须required:true |
| operationId | 稳定唯一字符串 | 生成客户端方法的标识 | 不可任意重复 |
| parameters | path/query/header/cookie | 描述位置、类型、约束 | 3.0 body使用requestBody |
| requestBody | required/content/schema | 完整输入数据 | required与schema字段required是两层 |
| responses | 状态码→description/content | 定义输出及失败 | 不写实际未实现的成功结果 |
| components | schemas/parameters/responses/securitySchemes | 共用定义的集中位置 | 不是重复写同名键覆盖原定义 |
| security | 方案对象数组 | OR或AND认证条件 | 数组对象OR;一个对象多键AND;[]公开覆盖 |
| tags/externalDocs | 标签与链接 | 组织操作及文档入口 | 不是运行能力开关 |
Schema约束的明确含义
| 关键字 | 类型/条件 | 用途 | 误区 |
| type/format | string/integer/number/boolean/array/object及格式 | 限制值的结构与补充格式 | format需要验证器执行;不能自动哈希密码 |
| required | 对象内字段名数组 | 要求键存在 | 不自动排除null或空白 |
| enum | 候选值数组 | 限制允许值 | 输入仍需真实校验 |
| items | 数组元素Schema | 约束每一个元素 | 不能只写数组不描述元素 |
| minimum/maximum | 数值上下限 | 约束金额、数量、分页 | 字符串长度用不同关键字 |
| minLength/maxLength/pattern | 长度与正则 | 约束文本 | 服务端空白处理要另明示 |
| allOf/oneOf/anyOf | Schema数组 | 组合/恰一/至少一 | allOf不是自动继承语言类;闭合属性需谨慎 |
| readOnly/writeOnly | 方向提示 | 区分响应及请求字段 | 服务端还须禁止客户端改id、泄露密码 |
| nullable | OpenAPI3.0布尔标记 | 允许null的版本化语义 | 3.1不能照搬全部3.0写法 |
| additionalProperties | 布尔或Schema | 约束未知字段 | 默认行为、生成器选项与实际校验分别核对 |
本章独立核心源码与验证步骤
展开查看收起
const {runChapter}=require('./chapter-lab.cjs');
const SCENARIOS=[
{
"id": "1",
"action": "schema_failures",
"goal": "错误合同/参数/引用/模型和边界分别拒绝"
}
];
runChapter("openapi-basic",SCENARIOS).catch(error=>{console.error(error.stack);process.exitCode=1});
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 错误合同/参数/引用/模型和边界分别拒绝
- 本轮实际范围以记录为准,规范通过/生成通过不自动等于其它语言运行成功
独立输入与实现:specs/openapi.json
展开查看收起
{
"openapi": "3.0.3",
"info": {
"title": "蓝色天空网 · 学习接口",
"version": "1.0.0",
"description": "mbsky.com 本地独立模拟服务;此地址不指向生产后台。",
"contact": {
"name": "蓝色天空网",
"url": "https://mbsky.com"
}
},
"servers": [
{
"url": "/api/v1",
"description": "独立本机教学服务"
}
],
"tags": [
{
"name": "users",
"description": "users模拟教学操作"
},
{
"name": "auth",
"description": "auth模拟教学操作"
},
{
"name": "books",
"description": "books模拟教学操作"
},
{
"name": "products",
"description": "products模拟教学操作"
},
{
"name": "orders",
"description": "orders模拟教学操作"
}
],
"paths": {
"/users": {
"get": {
"operationId": "listUsers",
"summary": "分页和状态过滤用户",
"tags": [
"users"
],
"responses": {
"200": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UserList"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "权限不足",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"parameters": [
{
"$ref": "#/components/parameters/Page"
},
{
"$ref": "#/components/parameters/Limit"
},
{
"$ref": "#/components/parameters/Offset"
},
{
"name": "status",
"in": "query",
"schema": {
"type": "string",
"enum": [
"active",
"inactive",
"banned"
]
}
}
]
},
"post": {
"operationId": "createUser",
"summary": "创建用户",
"tags": [
"users"
],
"responses": {
"201": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/User"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "权限不足",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"409": {
"description": "用户名或邮箱冲突",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"413": {
"description": "请求体过大",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"415": {
"description": "媒体类型错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NewUser"
}
}
}
}
}
},
"/users/{userId}": {
"get": {
"operationId": "getUser",
"summary": "获取用户详情",
"tags": [
"users"
],
"responses": {
"200": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UserDetail"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "权限不足",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"parameters": [
{
"name": "userId",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"minimum": 1
}
}
]
},
"put": {
"operationId": "replaceUser",
"summary": "整体替换允许的用户资料",
"tags": [
"users"
],
"responses": {
"200": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UserDetail"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "权限不足",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"413": {
"description": "请求体过大",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"415": {
"description": "媒体类型错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"409": {
"description": "资源冲突",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"parameters": [
{
"name": "userId",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"minimum": 1
}
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ReplaceUser"
}
}
}
}
},
"delete": {
"operationId": "deleteUser",
"summary": "删除用户",
"tags": [
"users"
],
"responses": {
"204": {
"description": "成功"
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "权限不足",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"parameters": [
{
"name": "userId",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"minimum": 1
}
}
]
},
"patch": {
"operationId": "patchUser",
"summary": "部分更新允许的用户字段",
"tags": [
"users"
],
"responses": {
"200": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UserDetail"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "权限不足",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"413": {
"description": "请求体过大",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"415": {
"description": "媒体类型错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"409": {
"description": "资源冲突",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"parameters": [
{
"name": "userId",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"minimum": 1
}
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UpdateUser"
}
}
}
}
}
},
"/auth/login": {
"post": {
"operationId": "login",
"summary": "教学用户登录",
"tags": [
"auth"
],
"responses": {
"200": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Token"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "凭证错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"413": {
"description": "请求体过大",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"415": {
"description": "媒体类型错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"security": [],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Login"
}
}
}
}
}
},
"/auth/register": {
"post": {
"operationId": "register",
"summary": "注册独立模拟用户",
"tags": [
"auth"
],
"responses": {
"201": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/RegisterResult"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"409": {
"description": "冲突",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"413": {
"description": "请求体过大",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"415": {
"description": "媒体类型错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"security": [],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NewUser"
}
}
}
}
}
},
"/books": {
"get": {
"operationId": "listBooks",
"summary": "分页过滤books",
"tags": [
"books"
],
"responses": {
"200": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/BookList"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"security": [],
"parameters": [
{
"$ref": "#/components/parameters/Page"
},
{
"$ref": "#/components/parameters/Limit"
},
{
"name": "category",
"in": "query",
"schema": {
"type": "string"
}
},
{
"name": "author",
"in": "query",
"schema": {
"type": "string"
}
},
{
"name": "title",
"in": "query",
"schema": {
"type": "string"
}
}
]
},
"post": {
"operationId": "createBook",
"summary": "创建books",
"tags": [
"books"
],
"responses": {
"201": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Book"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "权限不足",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"409": {
"description": "资源冲突",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"413": {
"description": "请求体过大",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"415": {
"description": "媒体类型错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/BookInput"
}
}
}
}
}
},
"/books/{bookId}": {
"get": {
"operationId": "getBook",
"summary": "books详情/替换/删除",
"tags": [
"books"
],
"responses": {
"200": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Book"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"security": [],
"parameters": [
{
"name": "bookId",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"minimum": 1
}
}
]
},
"put": {
"operationId": "replaceBook",
"summary": "books详情/替换/删除",
"tags": [
"books"
],
"responses": {
"200": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Book"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "权限不足",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"413": {
"description": "请求体过大",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"415": {
"description": "媒体类型错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"409": {
"description": "资源冲突",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"parameters": [
{
"name": "bookId",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"minimum": 1
}
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/BookInput"
}
}
}
}
},
"delete": {
"operationId": "deleteBook",
"summary": "books详情/替换/删除",
"tags": [
"books"
],
"responses": {
"204": {
"description": "成功"
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "权限不足",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"parameters": [
{
"name": "bookId",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"minimum": 1
}
}
]
},
"patch": {
"operationId": "patchBook",
"summary": "部分更新允许的图书字段",
"tags": [
"books"
],
"responses": {
"200": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Book"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "权限不足",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"413": {
"description": "请求体过大",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"415": {
"description": "媒体类型错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"409": {
"description": "资源冲突",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"parameters": [
{
"name": "bookId",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"minimum": 1
}
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/BookPatch"
}
}
}
}
}
},
"/products": {
"get": {
"operationId": "listProducts",
"summary": "分页过滤products",
"tags": [
"products"
],
"responses": {
"200": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProductList"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"security": [],
"parameters": [
{
"$ref": "#/components/parameters/Page"
},
{
"$ref": "#/components/parameters/Limit"
},
{
"name": "category",
"in": "query",
"schema": {
"type": "string"
}
}
]
},
"post": {
"operationId": "createProduct",
"summary": "创建products",
"tags": [
"products"
],
"responses": {
"201": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Product"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "权限不足",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"409": {
"description": "资源冲突",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"413": {
"description": "请求体过大",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"415": {
"description": "媒体类型错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProductInput"
}
}
}
}
}
},
"/products/{productId}": {
"get": {
"operationId": "getProduct",
"summary": "products详情/替换/删除",
"tags": [
"products"
],
"responses": {
"200": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Product"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"security": [],
"parameters": [
{
"name": "productId",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"minimum": 1
}
}
]
},
"put": {
"operationId": "replaceProduct",
"summary": "products详情/替换/删除",
"tags": [
"products"
],
"responses": {
"200": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Product"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "权限不足",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"413": {
"description": "请求体过大",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"415": {
"description": "媒体类型错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"parameters": [
{
"name": "productId",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"minimum": 1
}
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProductInput"
}
}
}
}
},
"delete": {
"operationId": "deleteProduct",
"summary": "products详情/替换/删除",
"tags": [
"products"
],
"responses": {
"204": {
"description": "成功"
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "权限不足",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"parameters": [
{
"name": "productId",
"in": "path",
"required": true,
"schema": {
"type": "integer",
"minimum": 1
}
}
]
}
},
"/orders": {
"get": {
"operationId": "listOrders",
"summary": "查询自己的订单",
"tags": [
"orders"
],
"responses": {
"200": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/OrderList"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "权限不足",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"parameters": [
{
"$ref": "#/components/parameters/Page"
},
{
"$ref": "#/components/parameters/Limit"
},
{
"name": "status",
"in": "query",
"schema": {
"type": "string",
"enum": [
"pending",
"processing",
"shipped",
"delivered",
"cancelled"
]
}
}
]
},
"post": {
"operationId": "createOrder",
"summary": "按真实产品价格计算模拟订单",
"tags": [
"orders"
],
"responses": {
"201": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Order"
}
}
}
},
"400": {
"description": "输入错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"404": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"403": {
"description": "权限不足",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"429": {
"description": "本地教学限流",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"413": {
"description": "请求体过大",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"415": {
"description": "媒体类型错误",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/OrderInput"
}
}
}
}
}
}
},
"components": {
"schemas": {
"UserBase": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"minimum": 1,
"readOnly": true
},
"username": {
"type": "string",
"minLength": 3,
"maxLength": 50
},
"email": {
"type": "string",
"format": "email"
},
"status": {
"type": "string",
"enum": [
"active",
"inactive",
"banned"
]
},
"createdAt": {
"type": "string",
"format": "date-time",
"readOnly": true
}
},
"required": [
"id",
"username",
"email",
"status",
"createdAt"
]
},
"User": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"minimum": 1,
"readOnly": true
},
"username": {
"type": "string",
"minLength": 3,
"maxLength": 50
},
"email": {
"type": "string",
"format": "email"
},
"status": {
"type": "string",
"enum": [
"active",
"inactive",
"banned"
]
},
"createdAt": {
"type": "string",
"format": "date-time",
"readOnly": true
}
},
"allOf": [
{
"$ref": "#/components/schemas/UserBase"
}
],
"additionalProperties": false
},
"UserDetail": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"minimum": 1,
"readOnly": true
},
"username": {
"type": "string",
"minLength": 3,
"maxLength": 50
},
"email": {
"type": "string",
"format": "email"
},
"status": {
"type": "string",
"enum": [
"active",
"inactive",
"banned"
]
},
"createdAt": {
"type": "string",
"format": "date-time",
"readOnly": true
},
"firstName": {
"type": "string",
"maxLength": 80
},
"lastName": {
"type": "string",
"maxLength": 80
},
"phone": {
"type": "string",
"maxLength": 30
},
"lastLogin": {
"type": "string",
"format": "date-time"
}
},
"allOf": [
{
"$ref": "#/components/schemas/UserBase"
}
],
"additionalProperties": false
},
"NewUser": {
"type": "object",
"properties": {
"username": {
"type": "string",
"minLength": 3,
"maxLength": 50
},
"email": {
"type": "string",
"format": "email"
},
"password": {
"type": "string",
"minLength": 8,
"maxLength": 100,
"writeOnly": true
}
},
"required": [
"username",
"email",
"password"
],
"additionalProperties": false
},
"UpdateUser": {
"type": "object",
"properties": {
"email": {
"type": "string",
"format": "email"
},
"firstName": {
"type": "string",
"maxLength": 80
},
"lastName": {
"type": "string",
"maxLength": 80
},
"phone": {
"type": "string",
"maxLength": 30
},
"password": {
"type": "string",
"minLength": 8,
"maxLength": 100,
"writeOnly": true
}
},
"minProperties": 1,
"additionalProperties": false
},
"Login": {
"type": "object",
"properties": {
"username": {
"type": "string"
},
"password": {
"type": "string",
"writeOnly": true
}
},
"required": [
"username",
"password"
],
"additionalProperties": false
},
"Error": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
],
"additionalProperties": false
},
"Token": {
"type": "object",
"properties": {
"accessToken": {
"type": "string"
},
"tokenType": {
"type": "string",
"enum": [
"Bearer"
]
},
"expiresIn": {
"type": "integer",
"minimum": 1
}
},
"required": [
"accessToken",
"tokenType",
"expiresIn"
],
"additionalProperties": false
},
"Address": {
"type": "object",
"properties": {
"street": {
"type": "string",
"minLength": 1,
"maxLength": 120
},
"city": {
"type": "string",
"minLength": 1,
"maxLength": 120
},
"state": {
"type": "string",
"minLength": 1,
"maxLength": 120
},
"zipCode": {
"type": "string",
"minLength": 1,
"maxLength": 120
},
"country": {
"type": "string",
"minLength": 1,
"maxLength": 120
}
},
"required": [
"street",
"city",
"state",
"zipCode",
"country"
],
"additionalProperties": false
},
"ProductInput": {
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 1,
"maxLength": 100
},
"description": {
"type": "string",
"maxLength": 1000
},
"priceCents": {
"type": "integer",
"minimum": 0,
"maximum": 100000000
},
"category": {
"type": "string",
"enum": [
"electronics",
"books",
"clothing",
"food"
]
},
"inStock": {
"type": "boolean",
"default": true
}
},
"required": [
"name",
"priceCents",
"category"
],
"additionalProperties": false
},
"OrderItemInput": {
"type": "object",
"properties": {
"productId": {
"type": "integer",
"minimum": 1
},
"quantity": {
"type": "integer",
"minimum": 1,
"maximum": 100
}
},
"required": [
"productId",
"quantity"
],
"additionalProperties": false
},
"OrderInput": {
"type": "object",
"properties": {
"items": {
"type": "array",
"minItems": 1,
"maxItems": 100,
"items": {
"$ref": "#/components/schemas/OrderItemInput"
}
},
"shippingAddress": {
"$ref": "#/components/schemas/Address"
}
},
"required": [
"items",
"shippingAddress"
],
"additionalProperties": false
},
"BookInput": {
"type": "object",
"properties": {
"title": {
"type": "string",
"minLength": 1,
"maxLength": 120
},
"author": {
"type": "string",
"minLength": 1,
"maxLength": 80
},
"publishDate": {
"type": "string",
"format": "date"
},
"isbn": {
"type": "string",
"format": "isbn",
"description": "Custom validation: real ISBN10/13 checksum; sample code does not claim actual publisher assignment."
},
"category": {
"type": "string",
"minLength": 1,
"maxLength": 40
}
},
"required": [
"title",
"author",
"isbn"
],
"additionalProperties": false
},
"ReplaceUser": {
"type": "object",
"properties": {
"username": {
"type": "string",
"minLength": 3,
"maxLength": 50
},
"email": {
"type": "string",
"format": "email"
},
"status": {
"type": "string",
"enum": [
"active",
"inactive",
"banned"
]
},
"firstName": {
"type": "string",
"maxLength": 80
},
"lastName": {
"type": "string",
"maxLength": 80
},
"phone": {
"type": "string",
"maxLength": 30
}
},
"required": [
"username",
"email",
"status"
],
"additionalProperties": false
},
"Product": {
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 1,
"maxLength": 100
},
"description": {
"type": "string",
"maxLength": 1000
},
"priceCents": {
"type": "integer",
"minimum": 0,
"maximum": 100000000
},
"category": {
"type": "string",
"enum": [
"electronics",
"books",
"clothing",
"food"
]
},
"inStock": {
"type": "boolean",
"default": true
},
"id": {
"type": "integer",
"minimum": 1,
"readOnly": true
},
"createdAt": {
"type": "string",
"format": "date-time",
"readOnly": true
}
},
"required": [
"name",
"priceCents",
"category",
"id",
"createdAt"
],
"additionalProperties": false
},
"Book": {
"type": "object",
"properties": {
"title": {
"type": "string",
"minLength": 1,
"maxLength": 120
},
"author": {
"type": "string",
"minLength": 1,
"maxLength": 80
},
"publishDate": {
"type": "string",
"format": "date"
},
"isbn": {
"type": "string",
"format": "isbn",
"description": "Custom validation: real ISBN10/13 checksum; sample code does not claim actual publisher assignment."
},
"category": {
"type": "string",
"minLength": 1,
"maxLength": 40
},
"id": {
"type": "integer",
"minimum": 1,
"readOnly": true
}
},
"required": [
"title",
"author",
"isbn",
"id"
],
"additionalProperties": false
},
"BookPatch": {
"type": "object",
"properties": {
"title": {
"type": "string",
"minLength": 1,
"maxLength": 120
},
"author": {
"type": "string",
"minLength": 1,
"maxLength": 80
},
"publishDate": {
"type": "string",
"format": "date"
},
"isbn": {
"type": "string",
"format": "isbn",
"description": "Custom validation: real ISBN10/13 checksum; sample code does not claim actual publisher assignment."
},
"category": {
"type": "string",
"minLength": 1,
"maxLength": 40
}
},
"additionalProperties": false,
"minProperties": 1
},
"OrderItem": {
"type": "object",
"properties": {
"productId": {
"type": "integer",
"minimum": 1
},
"quantity": {
"type": "integer",
"minimum": 1,
"maximum": 100
},
"unitPriceCents": {
"type": "integer",
"minimum": 0,
"readOnly": true
}
},
"required": [
"productId",
"quantity",
"unitPriceCents"
],
"additionalProperties": false
},
"Order": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"minimum": 1,
"readOnly": true
},
"userId": {
"type": "integer",
"minimum": 1,
"readOnly": true
},
"items": {
"type": "array",
"items": {
"$ref": "#/components/schemas/OrderItem"
}
},
"shippingAddress": {
"$ref": "#/components/schemas/Address"
},
"totalCents": {
"type": "integer",
"minimum": 0,
"readOnly": true
},
"status": {
"type": "string",
"enum": [
"pending",
"processing",
"shipped",
"delivered",
"cancelled"
]
},
"createdAt": {
"type": "string",
"format": "date-time",
"readOnly": true
}
},
"required": [
"id",
"userId",
"items",
"shippingAddress",
"totalCents",
"status",
"createdAt"
],
"additionalProperties": false
},
"UserList": {
"type": "object",
"properties": {
"total": {
"type": "integer",
"minimum": 0
},
"page": {
"type": "integer",
"minimum": 1
},
"limit": {
"type": "integer",
"minimum": 1,
"maximum": 100
},
"data": {
"type": "array",
"items": {
"$ref": "#/components/schemas/User"
}
}
},
"required": [
"total",
"page",
"limit",
"data"
],
"additionalProperties": false
},
"BookList": {
"type": "object",
"properties": {
"total": {
"type": "integer",
"minimum": 0
},
"page": {
"type": "integer",
"minimum": 1
},
"limit": {
"type": "integer",
"minimum": 1,
"maximum": 100
},
"data": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Book"
}
}
},
"required": [
"total",
"page",
"limit",
"data"
],
"additionalProperties": false
},
"ProductList": {
"type": "object",
"properties": {
"total": {
"type": "integer",
"minimum": 0
},
"page": {
"type": "integer",
"minimum": 1
},
"limit": {
"type": "integer",
"minimum": 1,
"maximum": 100
},
"data": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Product"
}
}
},
"required": [
"total",
"page",
"limit",
"data"
],
"additionalProperties": false
},
"OrderList": {
"type": "object",
"properties": {
"total": {
"type": "integer",
"minimum": 0
},
"page": {
"type": "integer",
"minimum": 1
},
"limit": {
"type": "integer",
"minimum": 1,
"maximum": 100
},
"data": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Order"
}
}
},
"required": [
"total",
"page",
"limit",
"data"
],
"additionalProperties": false
},
"RegisterResult": {
"type": "object",
"properties": {
"user": {
"$ref": "#/components/schemas/User"
},
"token": {
"$ref": "#/components/schemas/Token"
}
},
"required": [
"user",
"token"
],
"additionalProperties": false
}
},
"parameters": {
"Page": {
"name": "page",
"in": "query",
"schema": {
"type": "integer",
"minimum": 1,
"default": 1
}
},
"Limit": {
"name": "limit",
"in": "query",
"schema": {
"type": "integer",
"minimum": 1,
"maximum": 100,
"default": 20
}
},
"Offset": {
"name": "offset",
"in": "query",
"schema": {
"type": "integer",
"minimum": 0,
"default": 0
}
}
},
"responses": {
"Unauthorized": {
"description": "未认证",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"NotFound": {
"description": "资源不存在",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
},
"securitySchemes": {
"bearerAuth": {
"type": "http",
"scheme": "bearer",
"bearerFormat": "JWT"
},
"apiKeyAuth": {
"type": "apiKey",
"in": "header",
"name": "X-API-Key"
},
"OAuthExample": {
"type": "oauth2",
"flows": {
"authorizationCode": {
"authorizationUrl": "https://example.invalid/authorize",
"tokenUrl": "https://example.invalid/token",
"scopes": {
"read": "读取",
"write": "写入"
}
}
}
}
}
},
"security": [
{
"bearerAuth": []
}
]
}
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 按本章对应实际结果核对,未运行的其它环境仍待处理
本章附件与实例包
常见错误与边界检查
- 把Swagger工具和OpenAPI规范混为同一服务。
- 登录误继承全局Bearer,或生成器参数混用。
- 只有状态码没有检查字段、敏感数据、实际权限与金额。
技术规范与官方资料
练习与参考解答
查看参考解答
保持合同和实现同版本;明确验证输入、响应、权限、异常和环境,不能用模拟结果冒充真实运行。