def dereference_openapi(openapi_doc):
"""Dereferences a Swagger/OpenAPI document by resolving all $ref pointers."""
try:
import jsonschema
except ImportError:
raise ImportError(
"The jsonschema library is required to parse OpenAPI documents. "
"Please install it with `pip install jsonschema`."
)
resolver = jsonschema.RefResolver.from_schema(openapi_doc)
stack = [openapi_doc]
while stack:
obj = stack.pop()
if isinstance(obj, dict):
if "$ref" in obj:
with resolver.resolving(obj["$ref"]) as resolved:
stack.append(resolved)
else:
stack.extend(obj.values())
elif isinstance(obj, list):
stack.extend(obj)
return openapi_doc
def process_api_spec(self, spec: dict) -> dict:
"""Perform simplification and reduction on an OpenAPI specification.
The goal is to create a more concise and efficient representation
for retrieval purposes.
"""
def reduce_details(details: dict) -> dict:
reduced = {}
if details.get("description"):
reduced["description"] = details.get("description")
if details.get("parameters"):
reduced["parameters"] = [
param
for param in details.get("parameters", [])
if param.get("required")
]
if "200" in details["responses"]:
reduced["responses"] = details["responses"]["200"]
return reduced
spec = dereference_openapi(spec)
endpoints = []
for route, operations in spec["paths"].items():
for operation, details in operations.items():
if operation in ["get", "post", "patch"]:
endpoint_name = f"{operation.upper()} {route}"
description = details.get("description")
endpoints.append(
(endpoint_name, description, reduce_details(details))
)
return {
"servers": spec["servers"],
"description": spec["info"].get("description"),
"endpoints": endpoints,
}
3条答案
按热度按时间oyt4ldly1#
嘿,@vaibhavtupe!我在这里帮助你解决任何bug、问题或贡献。我是一个机器人,旨在协助你在等待人类维护者时提供帮助。
要使用
OpenAPIToolSpec
高效处理大型OpenAPI规范YAML文件,而不会遇到RecursionError
,你可以尝试以下方法:dereference_openapi
函数以使用迭代方法。这有助于避免触及递归限制。以下是如何修改
dereference_openapi
函数以使用迭代方法的示例:以下是如何优化OpenAPI规范的示例:
通过使用迭代方法进行反引用和优化OpenAPI规范,你可以更有效地处理大型文件,而不会触及递归错误1(https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/tools/llama-index-tools-openapi/llama_index/tools/openapi/base.py)。
qgzx9mmu2#
如果你能分享一下规格,那可能会有助于调试。
fcg9iug33#
以下是文本内容的翻译结果:
@logan-markewich 这里是API规范: https://app.swaggerhub.com/apis/equinix-api/fabric/4.14
增加递归限制会使代码无限运行。[无限递归]