You need to enable JavaScript to run this app. OpenAPI能否以实用方式集成HATEOAS REST API?
优惠活动
大模型
产品
解决方案
定价
更多

OpenAPI能否以实用方式集成HATEOAS REST API?

是否可使用OpenAPI描述HATEOAS REST API?

当采用HAL格式描述API时,需要为其定义三套Schema(分别对应请求负载、集合资源、项资源),示例Schema配置如下:

components:
  schemas:
    Link:
      type: object
      properties:
        href:
          type: string
        hreflang:
          type: string
        title:
          type: string
        type:
          type: string
        deprecation:
          type: string
        profile:
          type: string
        name:
          type: string
        templated:
          type: boolean
    Links:
      type: object
      discriminator:
        propertyName: _links
      properties:
        _links:
          type: object
          additionalProperties:
            $ref: "#/components/schemas/Link"
    CollectionModel:
      type: object
      discriminator:
        propertyName: _embedded
      properties:
        _embedded:
          type: object
        _links:
          type: object
          properties:
            self:
              type: string
            profile:
              type: string
            search:
              type: string
    CollectionModel_Foo:
      type: object
      allOf:
        - $ref: "#/components/schemas/CollectionModel"
      properties:
        _embedded:
          type: object
          properties:
            projects:
              type: array
              items:
                $ref: "#/components/schemas/EntityModel_Foo"
    EntityModel_Foo:
      type: object
      allOf:
        - $ref: "#/components/schemas/Foo"
        - $ref: "#/components/schemas/Links"
    Foo:
      type: object
      properties:
        id:
          type: string
          format: uuid
          readOnly: true
        bar:
          type: string

这种Schema定义方式实用性很差:

  • 会大幅提升API规范的维护复杂度
  • 使用OpenAPI Generator基于该Schema生成客户端时,生成的客户端不会适配HATEOAS逻辑,只会固定请求预定义的资源端点,完全发挥不了HATEOAS的动态交互能力,这套方案在目标场景下完全无效

之前曾考虑采用JSON:API规范,但完整的JSON Schema兼容支持仅在OpenAPI 3.1版本中落地,旧版本无法满足全量描述需求。
截至目前,暂时没有找到能将OpenAPI与HATEOAS API顺畅集成的落地方法。


内容的提问来源于stack exchange,提问作者twobiers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.15 12:36:04