OpenAPI 3.x.x 对可选参数的更改有不同的响应

2024-02-08

我正在尝试为现有 API 创建 OAS Swagger 文档,其中 API 的响应根据查询参数而变化。我正在努力以开发人员友好的方式记录这一点,因此需要一些帮助。以下是完整的场景供您参考。

端点 1:/订单?展开=假

/order?expand=false

{
    "orderNo": "12345",
    "orderDetail": "Description of Order",
    "orderMarket": {
        "id": "UK"
    },
    "brandOrderStatus": {
        "id": "LLA"
    }
}

端点2:/顺序?展开=真

{
    "orderNo": "12345",
    "orderDetail": "Description of Order",
    "orderMarket": {
        "id": "UK",
        "descr": "United Kingdom",
        "lang": "en-GB"
    },
    "brandOrderStatus": {
        "id": "LLA",
        "descr": "Some Status Description",
        "lang": "en-GB"
    }
}

端点3:/order?expand=true&include=功能

{
    "orderNo": "12345",
    "orderDetail": "Description of Order",
    "orderMarket": {
        "id": "UK",
        "descr": "United Kingdom",
        "lang": "en-GB"
    },
    "brandOrderStatus": {
        "id": "LLA",
        "descr": "Some Status Description",
        "lang": "en-GB"
    }
    "_embedded": {
        "features": [
            {
                "id": "AJS",
                "type": "FeatureType",
                "descr": "FeatureDescription",
                "prices": [
                    {
                        "type": "msrpNetPrice",
                        "amount": 0.00,
                        "currency": "GBP"
                    }
                ],
                "group": "null"
            }
        ]
    }
}

我尝试使用 OneOf,但并不认为这在这种情况下有效,因为结构会随着每个可选参数的变化而变化。

关于如何将其记录在 Swagger 文档中,您有什么想法吗?或者任何其他想法来记录这一点。


对于此类问题,最直接的解决方案取决于您期望如何使用 OpenAPI 描述。如果仅用于文档目的,您可以使用examples并相应地定义它们。如果您想将其用于代码生成,请使用anyOf可能是定义预期有效负载的更好选择。

不幸的是,OpenAPI 没有一个很好的方法来记录这样的“场景”。 (如果我发送这些查询参数,这就是响应)。没有任何一个examples将明确对应于anyOf模式表示。您需要使用完整表示或使用anyOf架构设计。

这是示例和 anyOf 响应模式

{
  "openapi": "3.0.3",
  "info": {
    "title": "test",
    "version": "1.0.0"
  },
  "servers": [],
  "paths": {
    "/order": {
      "get": {
        "summary": "stack",
        "parameters": [
          {
            "$ref": "#/components/parameters/expand"
          },
          {
            "$ref": "#/components/parameters/include"
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "headers": {},
            "content": {
              "application/json": {
                "schema": {
                  "anyOf": [
                    {
                      "$ref": "#/components/schemas/order"
                    },
                    {
                      "$ref": "#/components/schemas/orderDetail"
                    },
                    {
                      "$ref": "#/components/schemas/orderDetailWithFeatures"
                    }
                  ]
                },
                "examples": {
                  "expand_false": {
                    "$ref": "#/components/examples/expand_false"
                  },
                  "expand_true": {
                    "$ref": "#/components/examples/expand_true"
                  },
                  "expand_with_features": {
                    "$ref": "#/components/examples/expand_with_features"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "parameters": {
      "expand": {
        "name": "expand",
        "description": "expands the response schema with more details",
        "in": "query",
        "schema": {
          "type": "boolean"
        }
      },
      "include": {
        "name": "include",
        "description": "expands the response schema with more details",
        "in": "query",
        "schema": {
          "type": "string",
          "enum": [
            "feature"
          ]
        }
      }
    },
    "schemas": {
      "order": {
        "type": "object",
        "properties": {
          "orderNo": {
            "type": "string"
          },
          "orderDetail": {
            "type": "string"
          },
          "orderMarket": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string"
              }
            }
          },
          "brandOrderStatus": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string"
              }
            }
          }
        }
      },
      "amountType": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string"
          },
          "amount": {
            "type": "number"
          },
          "currency": {
            "type": "string",
            "enum": [
              "USD",
              "GBP",
              "EUR"
            ]
          }
        }
      },
      "lang-descType": {
        "type": "object",
        "properties": {
          "lang": {
            "type": "string"
          },
          "descr": {
            "type": "string"
          }
        }
      },
      "features": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "id": {
              "type": "string"
            },
            "type": {
              "type": "string"
            },
            "descr": {
              "type": "string"
            },
            "prices": {
              "type": "array",
              "items": {
                "$ref": "#/components/schemas/amountType"
              }
            },
            "group": {
              "type": "string"
            }
          }
        }
      },
      "orderDetail": {
        "allOf": [
          {
            "$ref": "#/components/schemas/order"
          },
          {
            "type": "object",
            "properties": {
              "orderMarket": {
                "allOf": [
                  {
                    "type": "object",
                    "properties": {
                      "id": {
                        "type": "string"
                      }
                    }
                  },
                  {
                    "$ref": "#/components/schemas/lang-descType"
                  }
                ]
              },
              "brandOrderStatus": {
                "allOf": [
                  {
                    "type": "object",
                    "properties": {
                      "id": {
                        "type": "string"
                      }
                    }
                  },
                  {
                    "$ref": "#/components/schemas/lang-descType"
                  }
                ]
              }
            }
          }
        ]
      },
      "orderDetailWithFeatures": {
        "allOf": [
          {
            "$ref": "#/components/schemas/orderDetail"
          },
          {
            "type": "object",
            "properties": {
              "_embedded": {
                "type": "object",
                "properties": {
                  "features": {
                    "type": "array",
                    "items": {
                      "$ref": "#/components/schemas/features"
                    }
                  }
                }
              }
            }
          }
        ]
      }
    },
    "examples": {
      "expand_false": {
        "value": {
          "orderNo": "12345",
          "orderDetail": "Description of Order",
          "orderMarket": {
            "id": "UK"
          },
          "brandOrderStatus": {
            "id": "LLA"
          }
        }
      },
      "expand_true": {
        "value": {
          "orderNo": "12345",
          "orderDetail": "Description of Order",
          "orderMarket": {
            "id": "UK",
            "descr": "United Kingdom",
            "lang": "en-GB"
          },
          "brandOrderStatus": {
            "id": "LLA",
            "descr": "Some Status Description",
            "lang": "en-GB"
          }
        }
      },
      "expand_with_features": {
        "value": {
          "orderNo": "12345",
          "orderDetail": "Description of Order",
          "orderMarket": {
            "id": "UK",
            "descr": "United Kingdom",
            "lang": "en-GB"
          },
          "brandOrderStatus": {
            "id": "LLA",
            "descr": "Some Status Description",
            "lang": "en-GB"
          },
          "_embedded": {
            "features": [
              {
                "id": "AJS",
                "type": "FeatureType",
                "descr": "FeatureDescription",
                "prices": [
                  {
                    "type": "msrpNetPrice",
                    "amount": 0.00,
                    "currency": "GBP"
                  }
                ],
                "group": "null"
              }
            ]
          }
        }
      }
    }
  }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

OpenAPI 3.x.x 对可选参数的更改有不同的响应 的相关文章

随机推荐

  • 将字符串转换为json字符串并在R中解析

    我有一个数据 其中一列为 json 字符串 reservation reasons 1592 name gt jorge value gt MX name gt Billing phone number value gt 1123 name
  • d3.js 右对齐嵌套条形图

    我正在与这个 d3 js 示例 http bl ocks org mbostock 1283663我希望将图表的整个方向更改为从右到左 我能够反转 x 轴刻度 var x d3 scale linear range width 0 以及 y
  • 在 Python 中打印不带换行符(但带空格)的列表

    我正在尝试使用打印不带换行符的列表的值sys stdout write 它工作得很好 但唯一的问题是我想将每个值与另一个值隔开 换句话说 而不是123 我想1 2 3 我在网站上寻找解决方案 但没有找到涉及列表的内容 当我添加 to sys
  • multiDexEnabled 不起作用

    我有一个相当大的android项目 该项目仍然可以编译 但是当我尝试编译测试时出现错误 Execution failed for task app dexDebugTest trouble writing output Too many m
  • Ruby 中的“if (a == b || c == b)”语句可以做得更短吗

    我有一段 Ruby 代码 如下所示 def check if a b c b execute some code b the same variable end end 这可以写成这样 def check if a c b this doe
  • 预期的 ';'在声明末尾 /vector /c++

    当我尝试初始化一个vector of ints 我总是收到此错误 预期的 在声明结束时 我使用了 C Primer 中的原始代码 vector
  • .NET:阻止 XmlDocument.LoadXml 检索 DTD

    我有以下代码 C 它花费太长时间并且抛出异常 new XmlDocument LoadXml
  • Android 捕获视频 mediaRecorder.start() 失败 -19

    我需要录制视频并保存 但出现错误start 媒体记录器方法 失败 19 这个错误应该是什么 文档中没有对此进行评论 第二天我正在与这个错误作斗争 我尝试了多个代码 谷歌教程 英特尔示例 我在网络上找到了所有代码 但无法使其中任何一个工作 请
  • SQL,什么聚合逻辑会产生不同的结果?

    SQL1 返回具有聚合名称的行 而 SQL2 返回非聚合名称 问题是执行这两个SQL时聚合逻辑有什么区别 谢谢 SQL1 SELECT name CASE WHEN COUNT CASE WHEN course SQL THEN 1 END
  • 从 C 中的命令行参数打开文件

    我希望我的 C 程序要求用户键入他们想要打开的文件的名称 并将该文件的内容打印到屏幕上 我正在学习 C 教程 到目前为止有以下代码 但是当我执行它时 它实际上不允许我输入文件名 我得到 按任意按钮继续 我正在使用代码块 我在这里做错了什么
  • Liferay:如何保存到portlet用户信息?

    我在欢迎页面上有一个天气 portlet 用户可以配置该 portlet 并选择他的城市 是否可以将用户信息存储在 portlet 首选项中 以便每个用户都有一个存储的城市 或者存储用户 portlet 信息而无需开发自己的 持久 服务的标
  • 将比视口更宽的 DIV 居中[重复]

    这个问题在这里已经有答案了 我正在创建一个页面来显示祖先的家谱 该页面是动态创建的 因此我无法知道会有多少代或内容是什么 然而 这里显示了一个相当简单的示例 http myrootsmap com so tree2 php http myr
  • 将 TF.exe 与 Team Foundation Service 结合使用?

    我们正在将构建从 Team Build Team Foundation Service 转移到使用 Jenkins CI 进行构建的本地构建机器 但是 我们仍然希望使用云进行源代码控制 所以现在我们需要访问云TFS来获取最新版本 Jenki
  • 为什么我无法加载 PySpark RandomForestClassifier 模型?

    我无法加载 Spark 保存的 RandomForestClassificationModel 环境 Apache Spark 2 0 1 在小型 4 台机器 集群上运行的独立模式 没有 HDFS 一切都保存到本地磁盘 构建并保存模型 cl
  • 跟踪数据库的进度保存在“实时”进度条中 asp.net mvc

    我需要显示 net 4 5 中 SQL 加载的进度 目的是显示正在发生的上传的 实时 流程 以便上传信息 的人可以看到上传正在运行 控制器方法 private void ProgressUpload int SystemGeneralAnn
  • 使用替换组从 xsd.exe 生成的代码序列化的 XML 无效(无效的 xsi:type 错误)

    我已经从一些 3GPP XSD 多个 XSD 文件 命名空间 生成了一些 C 类 除了替换组中使用的抽象类型的具体实例存在问题之外 它非常适合序列化 首先 架构的相关部分 通用Nrm xsd
  • 在 UITableView 单元格 iOS Swift 中突出显示搜索结果

    我已经实现了一个搜索栏TableView 现在我想强调一下结果 例如 如果我输入了两个字母 那么这两个字母应该在结果中突出显示TableView从搜索栏下拉 谁能帮我做到这一点 我知道我应该为此使用自定义单元格 但我失去了实现它的能力 He
  • @commands.has_permissions 不检查权限

    所以基本上我正在做的就是尝试编写一个非常基本的踢出和禁止命令以用于我的不和谐机器人 我浏览了许多不同的教程和提出的类似问题 但我根本找不到解决此问题的方法 bot command commands has permissions kick
  • 解压在中间停止并且输出文件填充零(黑色像素)?

    我正在尝试对 bmp 位图 文件应用 DCT 离散余弦变换 压缩 我有一个在 Turbo C 中运行的 c 文件 这实际上并不是压缩 但我试图实现 DCT 和 IDCT 代码如下 the image to be compressed is
  • OpenAPI 3.x.x 对可选参数的更改有不同的响应

    我正在尝试为现有 API 创建 OAS Swagger 文档 其中 API 的响应根据查询参数而变化 我正在努力以开发人员友好的方式记录这一点 因此需要一些帮助 以下是完整的场景供您参考 端点 1 订单 展开 假 order expand