请用 OpenAPI 3.1 的 webhooks 字段描述一个异步订单状态推送场景

解读

面试官想验证三件事:

  1. 你是否真的读过 OpenAPI 3.1 规范,知道 webhooks 与 callbacks 的区别;
  2. 能否把“订单状态变化”这一典型中国电商场景抽象成“事件–订阅–推送”模型;
  3. 是否具备技术写作基本功:字段命名、枚举值、安全策略、示例、版本控制、中文语境下的可读性。

答题时要体现“文档即产品”的思维:先给场景,再给契约,最后补充运营细节(重试、幂等、签名、灰度)。切忌只贴一段 YAML,没有上下文说明。

知识点

  1. OpenAPI 3.1 新增顶层 webhooks 字段,与 paths 同级,用于描述“由服务端主动发起”的 HTTP 请求;
  2. webhooks 的 key 是事件名(eventType),value 是 Path Item Object 或 Reference Object;
  3. 消息体最好再包一层信封(eventId、eventType、timestamp、sign),方便接收方做幂等与验签;
  4. 国内监管要求:个人信息不出境,HTTPS 强制 TLS1.2+,敏感字段脱敏;
  5. 技术写作规范:
    – 字段名用 lowerCamelCase,中文注释放在 description;
    – 枚举值全部大写,下划线分隔;
    – 示例用真实可运行的 JSON,时间格式统一 RFC3339;
    – 版本号写在 info.version,遵循 SemVer;
    – 给每个 webhook 配“重试策略”说明,减少后续运维工单。

答案

openapi: 3.1.0
info:
  title: 商户订单状态推送契约
  version: 1.2.0
  description: |
    适用于国内电商 SaaS 平台。  
    当订单状态发生变更时,平台通过 HTTPS POST 向商户注册的回调地址推送事件。  
    本规范遵循《GB/T 35273—2020 个人信息安全规范》。
webhooks:
  orderStatusChanged:
    post:
      summary: 订单状态变化事件
      description: |
        触发时机:订单状态从“待支付”变为“已支付”、“已发货”、“已完成”、“已关闭”任一节点。  
        重试策略:指数退避,最大 5 次,间隔 2^n 秒;接收方需返回 200 且 payload 包含 {"code":0},否则视为失败。  
        幂等保证:eventId 全局唯一,接收方可用 eventId 做去重,有效期 24h。
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                eventId:
                  type: string
                  description: 事件唯一编号,UUID v4
                  example: 6ba7b810-9dad-11d1-80b4-00c04fd430c8
                eventType:
                  type: string
                  description: 事件类型
                  enum: [ORDER_STATUS_CHANGED]
                  example: ORDER_STATUS_CHANGED
                timestamp:
                  type: string
                  format: date-time
                  description: 事件产生时间,RFC3339
                  example: "2024-06-01T12:00:00+08:00"
                data:
                  type: object
                  description: 业务数据
                  properties:
                    orderId:
                      type: string
                      description: 平台侧订单号
                      example: "202406011234567890"
                    merchantOrderId:
                      type: string
                      description: 商户侧订单号
                      example: "MER202406011234"
                    userIdMask:
                      type: string
                      description: 用户 ID 掩码,中间 4 位打码
                      example: "u_12****90"
                    status:
                      type: string
                      description: 最新状态
                      enum: [PAID, SHIPPED, COMPLETED, CLOSED]
                      example: PAID
                    amount:
                      type: integer
                      description: 订单实付金额,单位分
                      example: 2999
            example:
              eventId: 6ba7b810-9dad-11d1-80b4-00c04fd430c8
              eventType: ORDER_STATUS_CHANGED
              timestamp: "2024-06-01T12:00:00+08:00"
              data:
                orderId: "202406011234567890"
                merchantOrderId: "MER202406011234"
                userIdMask: "u_12****90"
                status: PAID
                amount: 2999
      responses:
        "200":
          description: 接收成功
          content:
            application/json:
              schema:
                type: object
                properties:
                  code:
                    type: integer
                    description: 0 表示成功,其它值平台会重试
                    example: 0
                  message:
                    type: string
                    example: "success"
        "400":
          $ref: "#/components/responses/BadRequest"
        "500":
          $ref: "#/components/responses/ServerError"
      security:
        - merchantHmac: []
components:
  securitySchemes:
    merchantHmac:
      type: http
      scheme: bearer
      description: |
        Bearer 内容为 HMAC-SHA256 签名。  
        签名格式:HexEncode(HMAC_SHA256(secret, timestamp + "." + eventId + "." + rawBody))  
        secret 由平台在商户后台提供,32 位随机字符串。  
        接收方需用相同算法验签,防抵赖与篡改。
  responses:
    BadRequest:
      description: 参数错误
    ServerError:
      description: 服务端异常

拓展思考

  1. 灰度发布:在 webhooks 里增加 x-gray-percent: 10 扩展字段,文档中说明灰度策略,方便商户联调。
  2. 批量推送:大促时可将 10 条事件合并为一次批量请求,减少 QPS,需在文档中给出数组包装示例与最大条数限制。
  3. 订阅管理:把“注册/更新回调地址”的 REST 接口也写进同一 OpenAPI 文件,形成完整闭环,降低客户集成成本。
  4. 多语言 SDK:基于这份契约用 openapi-generator 生成 Java、Python、Go 三种语言的 Model 与验签工具,再把使用示例写成 Markdown,放在 docs/ 目录,体现技术文档工程师的“交付物”思维。