File size: 3,686 Bytes
8c5bdf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""Test experimental OpenAPI parser feature flag behavior."""

import httpx
import pytest
from fastapi import FastAPI

from fastmcp import FastMCP
from fastmcp.experimental.server.openapi import (
    FastMCPOpenAPI as ExperimentalFastMCPOpenAPI,
)
from fastmcp.server.openapi import FastMCPOpenAPI as LegacyFastMCPOpenAPI
from fastmcp.utilities.tests import temporary_settings


class TestOpenAPIExperimentalFeatureFlag:
    """Test experimental OpenAPI parser feature flag behavior."""

    @pytest.fixture
    def simple_openapi_spec(self):
        """Simple OpenAPI spec for testing."""
        return {
            "openapi": "3.0.0",
            "info": {"title": "Test API", "version": "1.0.0"},
            "paths": {
                "/test": {
                    "get": {
                        "operationId": "test_operation",
                        "summary": "Test operation",
                        "responses": {"200": {"description": "Success"}},
                    }
                }
            },
        }

    @pytest.fixture
    def mock_client(self):
        """Mock HTTP client."""
        return httpx.AsyncClient(base_url="https://api.example.com")

    def test_from_openapi_uses_legacy_by_default(
        self, simple_openapi_spec, mock_client
    ):
        """Test that from_openapi uses legacy parser by default."""
        # Create server using from_openapi (should use legacy by default)
        server = FastMCP.from_openapi(
            openapi_spec=simple_openapi_spec, client=mock_client
        )

        # Should be the legacy implementation
        assert isinstance(server, LegacyFastMCPOpenAPI)
        # Note: Log message "Using legacy OpenAPI parser..." is emitted during creation

    def test_from_openapi_uses_experimental_with_flag(
        self, simple_openapi_spec, mock_client
    ):
        """Test that from_openapi uses experimental parser with flag enabled."""
        # Create server with experimental flag enabled
        with temporary_settings(experimental__enable_new_openapi_parser=True):
            server = FastMCP.from_openapi(
                openapi_spec=simple_openapi_spec, client=mock_client
            )

        # Should be the experimental implementation
        assert isinstance(server, ExperimentalFastMCPOpenAPI)
        # Note: No log message should be emitted when using experimental parser

    def test_from_fastapi_uses_legacy_by_default(self):
        """Test that from_fastapi uses legacy parser by default."""
        # Create a simple FastAPI app
        app = FastAPI(title="Test API")

        @app.get("/test")
        def test_endpoint():
            return {"message": "test"}

        # Create server using from_fastapi (should use legacy by default)
        server = FastMCP.from_fastapi(app=app)

        # Should be the legacy implementation
        assert isinstance(server, LegacyFastMCPOpenAPI)
        # Note: Log message "Using legacy OpenAPI parser..." is emitted during creation

    def test_from_fastapi_uses_experimental_with_flag(self):
        """Test that from_fastapi uses experimental parser with flag enabled."""
        # Create a simple FastAPI app
        app = FastAPI(title="Test API")

        @app.get("/test")
        def test_endpoint():
            return {"message": "test"}

        # Create server with experimental flag enabled
        with temporary_settings(experimental__enable_new_openapi_parser=True):
            server = FastMCP.from_fastapi(app=app)

        # Should be the experimental implementation
        assert isinstance(server, ExperimentalFastMCPOpenAPI)
        # Note: No log message should be emitted when using experimental parser