From 5140c705214a0a9ebe3f4d7a0cc418f05317f52a Mon Sep 17 00:00:00 2001 From: sabonerune <102559104+sabonerune@users.noreply.github.com> Date: Wed, 5 Jun 2024 10:09:17 +0900 Subject: [PATCH] =?UTF-8?q?FIX:=20`manage=5Flibrary`=E3=81=8C`false`?= =?UTF-8?q?=E3=81=AE=E3=81=A8=E3=81=8DOpenAPI=E3=82=B9=E3=82=AD=E3=83=BC?= =?UTF-8?q?=E3=83=9E=E3=83=BC=E3=81=AE=E5=8F=82=E7=85=A7=E3=81=8C=E3=82=A8?= =?UTF-8?q?=E3=83=A9=E3=83=BC=E3=81=AB=E3=81=AA=E3=82=8B=E5=95=8F=E9=A1=8C?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3=20(#1374)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * FIX: `manage_library`が`false`のときOpenAPIスキーマーの参照がエラーになる問題を修正 * FIX: `manage_library`が`False`の場合のみ`BaseLibraryInfo`と`LibrarySpeaker`を足す * FIX: `manage_library`がTrueの場合のみ追加する --- voicevox_engine/app/application.py | 4 +++- voicevox_engine/app/openapi_schema.py | 25 ++++++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/voicevox_engine/app/application.py b/voicevox_engine/app/application.py index 8c9c83b17..6b76c173d 100644 --- a/voicevox_engine/app/application.py +++ b/voicevox_engine/app/application.py @@ -81,6 +81,8 @@ def generate_app( ) app.include_router(generate_portal_page_router(engine_manifest.name)) - app = configure_openapi_schema(app) + app = configure_openapi_schema( + app, engine_manifest.supported_features.manage_library + ) return app diff --git a/voicevox_engine/app/openapi_schema.py b/voicevox_engine/app/openapi_schema.py index 815bf4808..b4f3715fd 100644 --- a/voicevox_engine/app/openapi_schema.py +++ b/voicevox_engine/app/openapi_schema.py @@ -4,11 +4,12 @@ from fastapi import FastAPI from fastapi.openapi.utils import get_openapi +from pydantic import BaseModel from voicevox_engine.library.model import BaseLibraryInfo, VvlibManifest -def configure_openapi_schema(app: FastAPI) -> FastAPI: +def configure_openapi_schema(app: FastAPI, manage_library: bool | None) -> FastAPI: """自動生成された OpenAPI schema へカスタム属性を追加する。""" # BaseLibraryInfo/VvlibManifestモデルはAPIとして表には出ないが、エディタ側で利用したいので、手動で追加する @@ -27,16 +28,18 @@ def custom_openapi() -> Any: contact=app.contact, license_info=app.license_info, ) - openapi_schema["components"]["schemas"][ - "VvlibManifest" - ] = VvlibManifest.schema() - # ref_templateを指定しない場合、definitionsを参照してしまうので、手動で指定する - base_library_info = BaseLibraryInfo.schema( - ref_template="#/components/schemas/{model}" - ) - # definitionsは既存のモデルを重複して定義するため、不要なので削除 - del base_library_info["definitions"] - openapi_schema["components"]["schemas"]["BaseLibraryInfo"] = base_library_info + if manage_library: + additional_models: list[type[BaseModel]] = [ + BaseLibraryInfo, + VvlibManifest, + ] + for model in additional_models: + # ref_templateを指定しない場合、definitionsを参照してしまうので、手動で指定する + schema = model.schema(ref_template="#/components/schemas/{model}") + # definitionsは既存のモデルを重複して定義するため、不要なので削除 + if "definitions" in schema: + del schema["definitions"] + openapi_schema["components"]["schemas"][schema["title"]] = schema app.openapi_schema = openapi_schema return openapi_schema