feat: 从 FastAPI 导出 openapi.json 并渲染为 API 文档
通过 vitepress-openapi 插件,在 VitePress 文档中实现基于 openapi.json 的 API 文档。 这样实现的 API 文档可以从最新版本的代码库中提取路由信息,继而实现自动集成。 --- 同时,通过 Kimi 和 Deepseek 实现并审查了一个对 Google 风格 docstring 的解析函数。 该函数可以从 Google 风格的 docstring 中提取参数文档并按 openapi 规范重新整理它们。 --- 增加了 nyahome openapi 命令用来导出 openapi.json。 增加了 task openapi-docs 命令用来准备未来的持续集成。
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
@@ -53,3 +54,15 @@ app.add_middleware(
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
def save_openapi_json(save_path: str | Path) -> None:
|
||||
from docstring_parser import DocstringStyle
|
||||
|
||||
from nyahome.cli.openapi_docstring import enrich_openapi_from_docstrings
|
||||
|
||||
# 增强 openapi docstring 参数文档提取
|
||||
enrich_openapi_from_docstrings(app, docstring_style=DocstringStyle.GOOGLE)
|
||||
|
||||
with open(save_path, "w") as f:
|
||||
json.dump(app.openapi(), f, indent=2)
|
||||
|
||||
Reference in New Issue
Block a user