From eb487ccb46437dc6de38fa9aca6ebd5a40f4a665 Mon Sep 17 00:00:00 2001 From: Tianyu Liu Date: Sun, 19 Apr 2026 23:25:13 +0200 Subject: [PATCH] Track exported OpenAPI schema --- .gitignore | 1 - README.md | 2 + openapi/openapi.json | 88 +++++++++++++++++++++++++++++++++++++++ openapi/openapi.yaml | 56 +++++++++++++++++++++++++ scripts/export_openapi.py | 11 +++-- 5 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 openapi/openapi.json create mode 100644 openapi/openapi.yaml diff --git a/.gitignore b/.gitignore index 517a4a5..d69d3bb 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,3 @@ __pycache__/ *.pyc data/ -openapi/ diff --git a/README.md b/README.md index ffaed7f..9079de9 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,8 @@ python scripts/export_openapi.py - `openapi/openapi.json` - `openapi/openapi.yaml` +`openapi/` 当前纳入版本控制。接口发生变更时,应重新运行导出脚本并同步提交生成的 schema 文件。 + ## 容器启动 1. 准备环境变量文件 diff --git a/openapi/openapi.json b/openapi/openapi.json new file mode 100644 index 0000000..8f02aa2 --- /dev/null +++ b/openapi/openapi.json @@ -0,0 +1,88 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "Home Automation Backend (Python)", + "description": "Python rewrite skeleton for the home automation backend. This stage provides only the foundation for future module migration.", + "version": "0.1.0" + }, + "paths": { + "/status": { + "get": { + "tags": [ + "system" + ], + "summary": "Get Status", + "operationId": "get_status_status_get", + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/StatusResponse" + } + } + } + } + } + } + }, + "/": { + "get": { + "tags": [ + "pages" + ], + "summary": "Home", + "operationId": "home__get", + "responses": { + "200": { + "description": "Successful Response", + "content": { + "text/html": { + "schema": { + "type": "string" + } + } + } + } + } + } + }, + "/location/record": { + "post": { + "tags": [ + "location" + ], + "summary": "Create Location Record", + "operationId": "create_location_record_location_record_post", + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": {} + } + } + } + } + } + } + }, + "components": { + "schemas": { + "StatusResponse": { + "properties": { + "status": { + "type": "string", + "title": "Status" + } + }, + "type": "object", + "required": [ + "status" + ], + "title": "StatusResponse" + } + } + } +} \ No newline at end of file diff --git a/openapi/openapi.yaml b/openapi/openapi.yaml new file mode 100644 index 0000000..f0db5ad --- /dev/null +++ b/openapi/openapi.yaml @@ -0,0 +1,56 @@ +openapi: 3.1.0 +info: + title: Home Automation Backend (Python) + description: Python rewrite skeleton for the home automation backend. This stage + provides only the foundation for future module migration. + version: 0.1.0 +paths: + /status: + get: + tags: + - system + summary: Get Status + operationId: get_status_status_get + responses: + '200': + description: Successful Response + content: + application/json: + schema: + $ref: '#/components/schemas/StatusResponse' + /: + get: + tags: + - pages + summary: Home + operationId: home__get + responses: + '200': + description: Successful Response + content: + text/html: + schema: + type: string + /location/record: + post: + tags: + - location + summary: Create Location Record + operationId: create_location_record_location_record_post + responses: + '200': + description: Successful Response + content: + application/json: + schema: {} +components: + schemas: + StatusResponse: + properties: + status: + type: string + title: Status + type: object + required: + - status + title: StatusResponse diff --git a/scripts/export_openapi.py b/scripts/export_openapi.py index 1adb17a..5917e64 100644 --- a/scripts/export_openapi.py +++ b/scripts/export_openapi.py @@ -1,13 +1,19 @@ +import json +import sys from pathlib import Path import yaml +PROJECT_ROOT = Path(__file__).resolve().parents[1] +if str(PROJECT_ROOT) not in sys.path: + sys.path.insert(0, str(PROJECT_ROOT)) + from app.main import create_app def main() -> None: app = create_app() - output_dir = Path("openapi") + output_dir = PROJECT_ROOT / "openapi" output_dir.mkdir(parents=True, exist_ok=True) schema = app.openapi() @@ -15,7 +21,7 @@ def main() -> None: json_path = output_dir / "openapi.json" yaml_path = output_dir / "openapi.yaml" - json_path.write_text(__import__("json").dumps(schema, ensure_ascii=False, indent=2), encoding="utf-8") + json_path.write_text(json.dumps(schema, ensure_ascii=False, indent=2), encoding="utf-8") yaml_path.write_text(yaml.safe_dump(schema, allow_unicode=True, sort_keys=False), encoding="utf-8") print(f"Wrote {json_path}") @@ -24,4 +30,3 @@ def main() -> None: if __name__ == "__main__": main() -