Files
hot-news-api/test_openapi.py
2026-03-26 15:42:37 +08:00

35 lines
1.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
测试 OpenAPI schema 生成
使用方法python test_openapi.py
"""
try:
from app.main import app
import json
# 生成 OpenAPI schema
schema = app.openapi()
print("✅ OpenAPI schema 生成成功!")
print(f"\n📊 API 路径数量:{len(schema['paths'])}")
print(f"\n📝 可用的 API 路径:")
for path in sorted(schema['paths'].keys()):
methods = list(schema['paths'][path].keys())
print(f" - {path} [{', '.join(methods)}]")
# 保存 schema 到文件
with open('openapi_schema.json', 'w', encoding='utf-8') as f:
json.dump(schema, f, ensure_ascii=False, indent=2)
print(f"\n💾 Schema 已保存到openapi_schema.json")
print(f"\n🌐 启动服务后访问:")
print(f" - Swagger UI: http://localhost:8000/docs")
print(f" - ReDoc: http://localhost:8000/redoc")
print(f" - OpenAPI JSON: http://localhost:8000/openapi.json")
except Exception as e:
print(f"❌ 错误:{e}")
import traceback
traceback.print_exc()