Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

perf(api): reuse controller OpenAPI documents in schema tests (#43120)

Stephen Zhoube8744264f9a
diff --git a/api/tests/unit_tests/controllers/test_swagger.py b/api/tests/unit_tests/controllers/test_swagger.py
index 2e55be0139b..63933381138 100644
--- a/api/tests/unit_tests/controllers/test_swagger.py
+++ b/api/tests/unit_tests/controllers/test_swagger.py
@@ -1,11 +1,11 @@
"""OpenAPI JSON rendering tests for Flask-RESTX API blueprints."""
import json
-from collections.abc import Iterator
+from collections.abc import Callable, Iterator
from pathlib import Path
import pytest
-from flask import Flask
+from flask import Blueprint, Flask
@pytest.fixture(autouse=True)
@@ -13,6 +13,51 @@ def _swagger_config(config_overrides) -> None:
config_overrides(SWAGGER_UI_ENABLED=True)
+OpenAPIJSONLoader = Callable[[Blueprint, str, bool], str]
+
+
[email protected](scope="module")
+def openapi_json() -> OpenAPIJSONLoader:
+ """Render each blueprint/config pair once; tests parse their own JSON objects.
+
+ Loading stays lazy so rendering runs after the per-test config and isolation
+ fixtures. Tests that change routes or config must render their own documents.
+ """
+ documents: dict[tuple[Blueprint, str, bool], str] = {}
+
+ def load(blueprint: Blueprint, route: str, include_all_models: bool) -> str:
+ key = (blueprint, route, include_all_models)
+ if key not in documents:
+ app = Flask(__name__)
+ app.config["TESTING"] = True
+ if include_all_models:
+ app.config["RESTX_INCLUDE_ALL_MODELS"] = True
+ app.register_blueprint(blueprint)
+ response = app.test_client().get(route)
+ assert response.status_code == 200
+ documents[key] = response.get_data(as_text=True)
+ return documents[key]
+
+ return load
+
+
[email protected](scope="module")
+def _exported_spec_directory(tmp_path_factory: pytest.TempPathFactory) -> Path:
+ return tmp_path_factory.mktemp("swagger-specs")
+
+
+def exported_console_json(_exported_spec_directory: Path, _swagger_config: None) -> str:
+ """Exercise the exporter once; tests parse independent copies of its output."""
+ from dev.generate_swagger_specs import generate_specs
+
+ console_openapi_path = _exported_spec_directory / "console-openapi.json"
+ if not console_openapi_path.exists():
+ written_paths = generate_specs(_exported_spec_directory)
+ assert console_openapi_path in written_paths
+ return console_openapi_path.read_text(encoding="utf-8")
+
+
USER_PROPERTY_SCHEMA = {
"description": (
"End-user identifier, defined by your app and unique within it. Identifies the end user for this request. "
@@ -194,14 +239,11 @@ def test_openapi_json_endpoints_render():
assert app.config["RESTX_INCLUDE_ALL_MODELS"] is True
-def test_agent_tts_routes_document_voice_queries_and_binary_audio():
+def test_agent_tts_routes_document_voice_queries_and_binary_audio(openapi_json: OpenAPIJSONLoader):
from controllers.console import bp as console_bp
from core.base.tts.audio_mime import SUPPORTED_TTS_AUDIO_MIME_TYPES
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.register_blueprint(console_bp)
- payload = app.test_client().get("/console/api/openapi.json").get_json()
+ payload = json.loads(openapi_json(console_bp, "/console/api/openapi.json", False))
voices = payload["paths"]["/agent/{agent_id}/text-to-audio/voices"]["get"]
language = _parameters_by_name(voices)["language"]
@@ -220,15 +262,10 @@ def test_agent_tts_routes_document_voice_queries_and_binary_audio():
assert _response_content_types(preview, status) == {"application/json"}
-def test_service_document_file_routes_document_multipart_form_data():
+def test_service_document_file_routes_document_multipart_form_data(openapi_json: OpenAPIJSONLoader):
from controllers.service_api import bp as service_api_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(service_api_bp)
-
- payload = app.test_client().get("/v1/openapi.json").get_json()
+ payload = json.loads(openapi_json(service_api_bp, "/v1/openapi.json", True))
paths = payload["paths"]
create_operation = paths["/datasets/{dataset_id}/document/create-by-file"]["post"]
@@ -262,15 +299,10 @@ def test_service_document_file_routes_document_multipart_form_data():
assert update_operation["requestBody"]["required"] is False
-def test_service_openapi_merges_public_api_reference_descriptions():
+def test_service_openapi_merges_public_api_reference_descriptions(openapi_json: OpenAPIJSONLoader):
from controllers.service_api import bp as service_api_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(service_api_bp)
-
- payload = app.test_client().get("/v1/openapi.json").get_json()
+ payload = json.loads(openapi_json(service_api_bp, "/v1/openapi.json", True))
chat_operation = payload["paths"]["/chat-messages"]["post"]
assert chat_operation["summary"] == "Send Chat Message"
@@ -284,15 +316,10 @@ def test_service_openapi_merges_public_api_reference_descriptions():
assert _parameters_by_name(rename_operation)["conversation_id"]["description"] == "Conversation ID."
-def test_service_document_list_documents_query_params_render():
+def test_service_document_list_documents_query_params_render(openapi_json: OpenAPIJSONLoader):
from controllers.service_api import bp as service_api_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(service_api_bp)
-
- payload = app.test_client().get("/v1/openapi.json").get_json()
+ payload = json.loads(openapi_json(service_api_bp, "/v1/openapi.json", True))
operation = payload["paths"]["/datasets/{dataset_id}/documents"]["get"]
params = _parameters_by_name(operation)
@@ -300,15 +327,10 @@ def test_service_document_list_documents_query_params_render():
assert params[name]["in"] == "query"
-def test_service_openapi_documents_decorator_user_contracts():
+def test_service_openapi_documents_decorator_user_contracts(openapi_json: OpenAPIJSONLoader):
from controllers.service_api import bp as service_api_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(service_api_bp)
-
- payload = app.test_client().get("/v1/openapi.json").get_json()
+ payload = json.loads(openapi_json(service_api_bp, "/v1/openapi.json", True))
paths = payload["paths"]
required_json_user_operations = (
@@ -361,15 +383,10 @@ def test_service_openapi_documents_decorator_user_contracts():
assert events_params["user"]["required"] is True
-def test_service_openapi_documents_app_multipart_contracts():
+def test_service_openapi_documents_app_multipart_contracts(openapi_json: OpenAPIJSONLoader):
from controllers.service_api import bp as service_api_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(service_api_bp)
-
- payload = app.test_client().get("/v1/openapi.json").get_json()
+ payload = json.loads(openapi_json(service_api_bp, "/v1/openapi.json", True))
paths = payload["paths"]
for path in ("/files/upload", "/audio-to-text"):
@@ -393,15 +410,10 @@ def test_service_openapi_documents_app_multipart_contracts():
assert pipeline_schema["required"] == ["file"]
-def test_service_openapi_documents_non_json_response_media_types():
+def test_service_openapi_documents_non_json_response_media_types(openapi_json: OpenAPIJSONLoader):
from controllers.service_api import bp as service_api_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(service_api_bp)
-
- payload = app.test_client().get("/v1/openapi.json").get_json()
+ payload = json.loads(openapi_json(service_api_bp, "/v1/openapi.json", True))
paths = payload["paths"]
assert _response_content_types(paths["/chat-messages"]["post"]) == {
@@ -424,15 +436,10 @@ def test_service_openapi_documents_non_json_response_media_types():
}
-def test_service_openapi_documents_uuid_params_and_deprecated_routes():
+def test_service_openapi_documents_uuid_params_and_deprecated_routes(openapi_json: OpenAPIJSONLoader):
from controllers.service_api import bp as service_api_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(service_api_bp)
-
- payload = app.test_client().get("/v1/openapi.json").get_json()
+ payload = json.loads(openapi_json(service_api_bp, "/v1/openapi.json", True))
paths = payload["paths"]
dataset_params = _parameters_by_name(paths["/datasets/{dataset_id}"]["get"])
@@ -463,15 +470,10 @@ def test_service_openapi_documents_uuid_params_and_deprecated_routes():
assert operation["deprecated"] is True
-def test_service_openapi_documents_path_action_enums():
+def test_service_openapi_documents_path_action_enums(openapi_json: OpenAPIJSONLoader):
from controllers.service_api import bp as service_api_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(service_api_bp)
-
- payload = app.test_client().get("/v1/openapi.json").get_json()
+ payload = json.loads(openapi_json(service_api_bp, "/v1/openapi.json", True))
paths = payload["paths"]
annotation_params = _parameters_by_name(paths["/apps/annotation-reply/{action}"]["post"])
@@ -484,15 +486,10 @@ def test_service_openapi_documents_path_action_enums():
assert metadata_params["action"]["schema"]["enum"] == ["disable", "enable"]
-def test_service_openapi_documents_conditional_payload_schemas():
+def test_service_openapi_documents_conditional_payload_schemas(openapi_json: OpenAPIJSONLoader):
from controllers.service_api import bp as service_api_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(service_api_bp)
-
- payload = app.test_client().get("/v1/openapi.json").get_json()
+ payload = json.loads(openapi_json(service_api_bp, "/v1/openapi.json", True))
paths = payload["paths"]
rename_schema = _json_body_schema(payload, paths["/conversations/{conversation_id}/name"]["post"])
@@ -538,13 +535,10 @@ def test_service_dataset_response_schemas_omit_console_permission_metadata():
assert "permission_keys" in console_schema["properties"]
-def test_service_delete_schemas_omit_unenforced_state_constraints():
+def test_service_delete_schemas_omit_unenforced_state_constraints(openapi_json: OpenAPIJSONLoader):
from controllers.service_api import bp as service_api_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.register_blueprint(service_api_bp)
- paths = app.test_client().get("/v1/openapi.json").get_json()["paths"]
+ paths = json.loads(openapi_json(service_api_bp, "/v1/openapi.json", False))["paths"]
delete_dataset = paths["/datasets/{dataset_id}"]["delete"]
assert "409" not in delete_dataset["responses"]
@@ -555,13 +549,10 @@ def test_service_delete_schemas_omit_unenforced_state_constraints():
assert "archived_document_immutable" in delete_document["responses"]["403"]["description"]
-def test_service_schemas_only_document_reachable_not_found_responses():
+def test_service_schemas_only_document_reachable_not_found_responses(openapi_json: OpenAPIJSONLoader):
from controllers.service_api import bp as service_api_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.register_blueprint(service_api_bp)
- paths = app.test_client().get("/v1/openapi.json").get_json()["paths"]
+ paths = json.loads(openapi_json(service_api_bp, "/v1/openapi.json", False))["paths"]
for path, method in (
("/apps/annotation-reply/{action}/status/{job_id}", "get"),
@@ -578,15 +569,10 @@ def test_service_schemas_only_document_reachable_not_found_responses():
assert "404" in paths["/chat-messages"]["post"]["responses"]
-def test_service_openapi_does_not_encode_docs_coverage_boundaries():
+def test_service_openapi_does_not_encode_docs_coverage_boundaries(openapi_json: OpenAPIJSONLoader):
from controllers.service_api import bp as service_api_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(service_api_bp)
-
- payload = app.test_client().get("/v1/openapi.json").get_json()
+ payload = json.loads(openapi_json(service_api_bp, "/v1/openapi.json", True))
paths = payload["paths"]
for path_item in paths.values():
@@ -601,15 +587,10 @@ def test_service_openapi_does_not_encode_docs_coverage_boundaries():
assert paths["/datasets/{dataset_id}/documents/{document_id}/update-by-file"]["post"]["deprecated"] is True
-def test_service_openapi_documents_auth_and_compatibility_payloads():
+def test_service_openapi_documents_auth_and_compatibility_payloads(openapi_json: OpenAPIJSONLoader):
from controllers.service_api import bp as service_api_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(service_api_bp)
-
- payload = app.test_client().get("/v1/openapi.json").get_json()
+ payload = json.loads(openapi_json(service_api_bp, "/v1/openapi.json", True))
assert payload["components"]["securitySchemes"]["Bearer"] == {
"bearerFormat": "API_KEY",
@@ -632,15 +613,10 @@ def test_service_openapi_documents_auth_and_compatibility_payloads():
assert tag_ids_schema["required"] == ["tag_ids", "target_id"]
-def test_console_account_avatar_query_param_renders_as_query():
+def test_console_account_avatar_query_param_renders_as_query(openapi_json: OpenAPIJSONLoader):
from controllers.console import bp as console_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(console_bp)
-
- payload = app.test_client().get("/console/api/openapi.json").get_json()
+ payload = json.loads(openapi_json(console_bp, "/console/api/openapi.json", True))
operation = payload["paths"]["/account/avatar"]["get"]
params = _parameters_by_name(operation)
@@ -649,15 +625,10 @@ def test_console_account_avatar_query_param_renders_as_query():
assert params["avatar"]["required"] is True
-def test_console_account_profile_patch_and_deprecated_aliases():
+def test_console_account_profile_patch_and_deprecated_aliases(openapi_json: OpenAPIJSONLoader):
from controllers.console import bp as console_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(console_bp)
-
- payload = app.test_client().get("/console/api/openapi.json").get_json()
+ payload = json.loads(openapi_json(console_bp, "/console/api/openapi.json", True))
paths = payload["paths"]
profile_patch = paths["/account/profile"]["patch"]
@@ -680,29 +651,20 @@ def test_console_account_profile_patch_and_deprecated_aliases():
assert paths["/account/avatar"]["get"].get("deprecated") is not True
-def test_console_agent_debug_conversation_refresh_has_no_body():
+def test_console_agent_debug_conversation_refresh_has_no_body(openapi_json: OpenAPIJSONLoader):
from controllers.console import bp as console_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.register_blueprint(console_bp)
-
- payload = app.test_client().get("/console/api/openapi.json").get_json()
+ payload = json.loads(openapi_json(console_bp, "/console/api/openapi.json", False))
operation = payload["paths"]["/agent/{agent_id}/debug-conversation/refresh"]["post"]
assert "requestBody" not in operation
assert "AgentDebugConversationRefreshPayload" not in payload["components"]["schemas"]
-def test_console_member_invite_documents_bad_request_response():
+def test_console_member_invite_documents_bad_request_response(openapi_json: OpenAPIJSONLoader):
from controllers.console import bp as console_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(console_bp)
-
- payload = app.test_client().get("/console/api/openapi.json").get_json()
+ payload = json.loads(openapi_json(console_bp, "/console/api/openapi.json", True))
operation = payload["paths"]["/workspaces/current/members/invite-email"]["post"]
response_schema = operation["responses"]["400"]["content"]["application/json"]["schema"]
@@ -723,15 +685,10 @@ def test_console_member_invite_documents_bad_request_response():
}
-def test_console_billing_routes_document_error_responses():
+def test_console_billing_routes_document_error_responses(openapi_json: OpenAPIJSONLoader):
from controllers.console import bp as console_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(console_bp)
-
- payload = app.test_client().get("/console/api/openapi.json").get_json()
+ payload = json.loads(openapi_json(console_bp, "/console/api/openapi.json", True))
expected_responses = {
("/billing/subscription", "get"): {
"422": "BillingUnprocessableEntityErrorResponse",
@@ -778,26 +735,17 @@ def test_console_billing_routes_document_error_responses():
assert compliance_response["required"] == ["url"]
-def test_console_model_provider_checkout_route_is_deprecated():
+def test_console_model_provider_checkout_route_is_deprecated(openapi_json: OpenAPIJSONLoader):
from controllers.console import bp as console_bp
- app = Flask(__name__)
- app.config["TESTING"] = True
- app.config["RESTX_INCLUDE_ALL_MODELS"] = True
- app.register_blueprint(console_bp)
-
- payload = app.test_client().get("/console/api/openapi.json").get_json()
+ payload = json.loads(openapi_json(console_bp, "/console/api/openapi.json", True))
operation = payload["paths"]["/workspaces/current/model-providers/{provider}/checkout-url"]["get"]
assert operation["deprecated"] is True
-def test_console_plugin_category_list_exported_schema_uses_typed_items(tmp_path: Path):
- from dev.generate_swagger_specs import generate_specs
-
- written_paths = generate_specs(tmp_path)
- console_openapi_path = next(path for path in written_paths if path.name == "console-openapi.json")
- payload = json.loads(console_openapi_path.read_text(encoding="utf-8"))
+def test_console_plugin_category_list_exported_schema_uses_typed_items(exported_console_json: str):
+ payload = json.loads(exported_console_json)
operation = payload["paths"]["/workspaces/current/plugin/{category}/list"]["get"]
parameters = {parameter["name"]: parameter for parameter in operation["parameters"]}
assert parameters["query"]["in"] == "query"
@@ -833,12 +781,8 @@ def test_console_plugin_category_list_exported_schema_uses_typed_items(tmp_path:
assert field in builtin_tool_schema["properties"]
-def test_console_installed_plugin_ids_exported_schema_is_lightweight(tmp_path: Path):
- from dev.generate_swagger_specs import generate_specs
-
- written_paths = generate_specs(tmp_path)
- console_openapi_path = next(path for path in written_paths if path.name == "console-openapi.json")
- payload = json.loads(console_openapi_path.read_text(encoding="utf-8"))
+def test_console_installed_plugin_ids_exported_schema_is_lightweight(exported_console_json: str):
+ payload = json.loads(exported_console_json)
operation = payload["paths"]["/workspaces/current/plugin/installed-ids"]["get"]
parameters = {parameter["name"]: parameter for parameter in operation["parameters"]}
assert parameters["category"]["in"] == "query"
@@ -866,12 +810,8 @@ def test_console_installed_plugin_ids_exported_schema_is_lightweight(tmp_path: P
}
-def test_console_datasource_catalog_exports_domain_response_schema(tmp_path: Path) -> None:
- from dev.generate_swagger_specs import generate_specs
-
- written_paths = generate_specs(tmp_path)
- console_openapi_path = next(path for path in written_paths if path.name == "console-openapi.json")
- payload = json.loads(console_openapi_path.read_text(encoding="utf-8"))
+def test_console_datasource_catalog_exports_domain_response_schema(exported_console_json: str) -> None:
+ payload = json.loads(exported_console_json)
operation = payload["paths"]["/rag/pipelines/datasource-plugins"]["get"]
assert operation.get("parameters", []) == []
@@ -913,12 +853,8 @@ def test_console_datasource_catalog_exports_domain_response_schema(tmp_path: Pat
}
-def test_console_model_provider_summary_exported_schema_is_lightweight(tmp_path: Path):
- from dev.generate_swagger_specs import generate_specs
-
- written_paths = generate_specs(tmp_path)
- console_openapi_path = next(path for path in written_paths if path.name == "console-openapi.json")
- payload = json.loads(console_openapi_path.read_text(encoding="utf-8"))
+def test_console_model_provider_summary_exported_schema_is_lightweight(exported_console_json: str):
+ payload = json.loads(exported_console_json)
operation = payload["paths"]["/workspaces/current/model-providers/summary"]["get"]
assert operation.get("parameters", []) == []