You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在OpenAPI YAML中复用Schema并裁剪枚举与属性子集?

可以实现,具体方案如下

完全可以在YAML(比如OpenAPI Schema场景)里实现你要的需求——复用另一Schema的部分属性,同时裁剪枚举值的子集。核心思路是不直接引用整个目标Schema,而是按需挑选属性并覆盖枚举规则,下面用你提到的兽医场景给出具体示例:

第一步:定义原始的InjuryInfo Schema

先把完整的InjuryInfo Schema写出来,作为复用的基础:

components:
  schemas:
    InjuryInfo:
      type: object
      properties:
        location:
          type: string
          enum: ["leg", "paw", "ear", "tail", "body"]
          description: 受伤部位
        details:
          type: string
          description: 受伤具体情况描述
        severity:
          type: string
          enum: ["mild", "moderate", "severe"]
          description: 受伤严重程度

第二步:构建Dog Schema并按需复用属性

在Dog的injury属性中,只保留location和details,同时把location的枚举值限制为["leg", "paw"]:

Dog:
      type: object
      properties:
        name:
          type: string
          description: 狗狗名字
        breed:
          type: string
          description: 犬种
        injury:
          type: object
          properties:
            # 自定义location的枚举子集,覆盖原始InjuryInfo的枚举
            location:
              type: string
              enum: ["leg", "paw"]
              description: 狗狗受伤部位(仅保留四肢相关)
            # 直接复用InjuryInfo中的details属性定义,避免重复编写规则
            details:
              $ref: '#/components/schemas/InjuryInfo/properties/details'
          required: ["location", "details"]  # 强制要求这两个属性

关键说明

  • 对于需要修改规则的属性(比如location):直接重新定义属性结构,指定你需要的枚举子集即可。
  • 对于需要完全复用的属性(比如details):用$ref直接指向原始Schema中对应的属性路径,这样原始Schema的details规则更新时,Dog里的这个属性也会同步生效。
  • 这种方式完全符合OpenAPI等Schema规范的要求,YAML作为载体可以完美支持。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 10:38:16