Skip to content

Commit

Permalink
FIX: manage_libraryfalseのときOpenAPIスキーマーの参照がエラーになる問題を修正 (VOICEVOX…
Browse files Browse the repository at this point in the history
…#1374)

* FIX: `manage_library`が`false`のときOpenAPIスキーマーの参照がエラーになる問題を修正

* FIX: `manage_library`が`False`の場合のみ`BaseLibraryInfo`と`LibrarySpeaker`を足す

* FIX: `manage_library`がTrueの場合のみ追加する
  • Loading branch information
sabonerune authored Jun 5, 2024
1 parent 7952787 commit 5140c70
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
4 changes: 3 additions & 1 deletion voicevox_engine/app/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 14 additions & 11 deletions voicevox_engine/app/openapi_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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として表には出ないが、エディタ側で利用したいので、手動で追加する
Expand All @@ -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

Expand Down

0 comments on commit 5140c70

Please sign in to comment.