蓝色天空网mbsky.com · 知识 · 实践 · 创造Swagger · 本地阅读预览
教程 / 后端开发 / Swagger

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必填标题、描述、联系信息品牌统一来自本站设置,不复制参考站名称
serversURL/variables数组描述基础地址本包/api/v1相对当前来源
paths路径→方法对象描述HTTP操作每个path参数必须required:true
operationId稳定唯一字符串生成客户端方法的标识不可任意重复
parameterspath/query/header/cookie描述位置、类型、约束3.0 body使用requestBody
requestBodyrequired/content/schema完整输入数据required与schema字段required是两层
responses状态码→description/content定义输出及失败不写实际未实现的成功结果
componentsschemas/parameters/responses/securitySchemes共用定义的集中位置不是重复写同名键覆盖原定义
security方案对象数组OR或AND认证条件数组对象OR;一个对象多键AND;[]公开覆盖
tags/externalDocs标签与链接组织操作及文档入口不是运行能力开关

Schema约束的明确含义

关键字类型/条件用途误区
type/formatstring/integer/number/boolean/array/object及格式限制值的结构与补充格式format需要验证器执行;不能自动哈希密码
required对象内字段名数组要求键存在不自动排除null或空白
enum候选值数组限制允许值输入仍需真实校验
items数组元素Schema约束每一个元素不能只写数组不描述元素
minimum/maximum数值上下限约束金额、数量、分页字符串长度用不同关键字
minLength/maxLength/pattern长度与正则约束文本服务端空白处理要另明示
allOf/oneOf/anyOfSchema数组组合/恰一/至少一allOf不是自动继承语言类;闭合属性需谨慎
readOnly/writeOnly方向提示区分响应及请求字段服务端还须禁止客户端改id、泄露密码
nullableOpenAPI3.0布尔标记允许null的版本化语义3.1不能照搬全部3.0写法
additionalProperties布尔或Schema约束未知字段默认行为、生成器选项与实际校验分别核对

本章独立核心源码与验证步骤

javascript · 阅读源码
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});
OpenAPI完整结构与版本边界:独立核心实例

所需环境:Windows / Node24.22.1 / SwaggerUI5.34.1 / Edge156 / Python3.12 / 固定生成器历史兼容版本

当前源码 SHA256:973f3bd9944c7b13c5414dac4972425b41828f56bc6b61f0e9b267b2f6bcf045

下载本站示例源码
预期结果 · 待实际运行核验
  • 错误合同/参数/引用/模型和边界分别拒绝
  • 本轮实际范围以记录为准,规范通过/生成通过不自动等于其它语言运行成功

独立输入与实现:specs/openapi.json

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": []
    }
  ]
}
独立输入与实现:specs/openapi.json

所需环境:Windows / Node24.22.1 / SwaggerUI5.34.1 / Edge156 / Python3.12 / 固定生成器历史兼容版本

当前源码 SHA256:e64ab78465858325b97d42678f11b0aa43977e6d286c289978823b249e65d417

下载本站示例源码
预期结果 · 待实际运行核验
  • 按本章对应实际结果核对,未运行的其它环境仍待处理

常见错误与边界

  • 把Swagger工具和OpenAPI规范混为同一服务。
  • 登录误继承全局Bearer,或生成器参数混用。
  • 只有状态码没有检查字段、敏感数据、实际权限与金额。

练习与解答

改一个字段或约束,先写成功和失败预期,再校验规范、服务和客户端三处。

展开参考解答

保持合同和实现同版本;明确验证输入、响应、权限、异常和环境,不能用模拟结果冒充真实运行。

规范与官方依据