自定义工具不支持 body 传参
liyangwd opened this issue · comments
{"openapi":"3.1.0","info":{"title":"FastAPI","version":"0.1.0"},"servers":[{"url":"https:// ssss.net/"}],"paths":{"/book_room":{"post":{"summary":"Book Room","operationId":"book_room_book_room_post","requestBody":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/BookRoom"}}},"required":true},"responses":{"200":{"description":"Successful Response","content":{"application/json":{"schema":{}}}},"422":{"description":"Validation Error","content":{"application/json":{"schema":{"$ref":"#/components/schemas/HTTPValidationError"}}}}}}}},"components":{"schemas":{"BookRoom":{"properties":{"start_time":{"type":"string","title":"Start Time"},"end_time":{"type":"string","title":"End Time"},"user_name":{"type":"string","title":"User Name"},"user_phone":{"type":"string","title":"User Phone"}},"type":"object","required":["start_time","end_time","user_name","user_phone"],"title":"BookRoom"},"HTTPValidationError":{"properties":{"detail":{"items":{"$ref":"#/components/schemas/ValidationError"},"type":"array","title":"Detail"}},"type":"object","title":"HTTPValidationError"},"ValidationError":{"properties":{"loc":{"items":{"anyOf":[{"type":"string"},{"type":"integer"}]},"type":"array","title":"Location"},"msg":{"type":"string","title":"Message"},"type":{"type":"string","title":"Error Type"}},"type":"object","required":["loc","msg","type"],"title":"ValidationError"}}}}
比如上面的openapi json; 解析出来的工具无法测试,没有提取出 body 里的参数。
请问这个问题解决了么?
解决了,可参考这个代码
def get_tool_schema_by_swagger_dict(swagger_dict):
# 初始化结果字典
structured_info = {
"name": swagger_dict["info"]["title"],
"description": swagger_dict["info"].get("description", ""),
"server_host": None,
"children": [],
}
try:
# 提取服务器 URL
servers = swagger_dict["servers"]
if servers:
structured_info["server_host"] = servers[0]["url"]
# 遍历 paths
for path, methods in swagger_dict["paths"].items():
for method, details in methods.items():
operation = {
"path": path,
"method": method.lower(),
"description": details.get("summary", ""),
"operationId": details.get("operationId", ""),
"parameters": [],
}
# 提取请求体参数
if "requestBody" in details:
for _, content in details["requestBody"]["content"].items():
if "$ref" in content["schema"]:
schema_ref = content["schema"]["$ref"]
schema_name = schema_ref.split("/")[-1]
schema = swagger_dict["components"]["schemas"][schema_name]
else:
schema = content["schema"]
if "properties" in schema:
for param_name, param_info in schema["properties"].items():
param = {
"name": param_name,
"in": "body",
"required": param_name
in schema.get("required", []),
"schema": {
"type": param_info.get("type", "string"),
"title": param_info.get("title", param_name),
},
}
operation["parameters"].append(param)
# 提取路径参数和查询参数
for param in details.get("parameters", []):
param_schema = param.get("schema", {}) or {}
operation["parameters"].append(
{
"name": param["name"],
"in": param["in"],
"required": param.get("required", False),
"schema": {
"type": param_schema.get("type", "string"),
"title": param_schema.get("title", param["name"]),
},
}
)
# 添加操作到结果列表
structured_info["children"].append(operation)
except Exception as e:
return {"error": str(e)}
return structured_info