File size: 7,434 Bytes
85b13ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""Tests for allOf handling at requestBody top level."""

from fastmcp.experimental.utilities.openapi.models import (
    HTTPRoute,
    RequestBodyInfo,
)
from fastmcp.experimental.utilities.openapi.schemas import _combine_schemas


def test_allof_at_requestbody_top_level():
    """Test that allOf schemas at requestBody top level are properly merged."""

    # Create a route with allOf at the requestBody top level
    route = HTTPRoute(
        path="/test",
        method="POST",
        operation_id="testOperation",
        parameters=[],
        request_body=RequestBodyInfo(
            required=True,
            content_schema={
                "application/json": {
                    "allOf": [
                        {
                            "type": "object",
                            "properties": {
                                "name": {"type": "string"},
                                "age": {"type": "integer"},
                            },
                            "required": ["name"],
                        },
                        {
                            "type": "object",
                            "properties": {
                                "email": {"type": "string"},
                                "phone": {"type": "string"},
                            },
                            "required": ["email"],
                        },
                    ]
                }
            },
        ),
        responses={},
    )

    # Combine schemas - this should merge allOf schemas
    combined = _combine_schemas(route)

    # Check that all properties from both allOf schemas are present
    properties = combined.get("properties", {})
    assert "name" in properties
    assert "age" in properties
    assert "email" in properties
    assert "phone" in properties

    # Check property types
    assert properties["name"]["type"] == "string"
    assert properties["age"]["type"] == "integer"
    assert properties["email"]["type"] == "string"
    assert properties["phone"]["type"] == "string"

    # Check that required fields are merged correctly
    required = set(combined.get("required", []))
    assert "name" in required
    assert "email" in required

    # allOf should be removed after merging
    assert "allOf" not in combined


def test_allof_with_nested_properties():
    """Test allOf with nested object properties."""

    route = HTTPRoute(
        path="/test",
        method="POST",
        operation_id="testNested",
        parameters=[],
        request_body=RequestBodyInfo(
            required=True,
            content_schema={
                "application/json": {
                    "allOf": [
                        {
                            "type": "object",
                            "properties": {
                                "user": {
                                    "type": "object",
                                    "properties": {
                                        "id": {"type": "integer"},
                                        "name": {"type": "string"},
                                    },
                                }
                            },
                            "required": ["user"],
                        },
                        {
                            "type": "object",
                            "properties": {
                                "metadata": {
                                    "type": "object",
                                    "properties": {
                                        "created": {"type": "string"},
                                        "updated": {"type": "string"},
                                    },
                                }
                            },
                        },
                    ]
                }
            },
        ),
        responses={},
    )

    combined = _combine_schemas(route)

    # Check nested properties are preserved
    properties = combined.get("properties", {})
    assert "user" in properties
    assert "metadata" in properties

    # Check nested structure
    assert properties["user"]["type"] == "object"
    assert "id" in properties["user"]["properties"]
    assert "name" in properties["user"]["properties"]

    assert properties["metadata"]["type"] == "object"
    assert "created" in properties["metadata"]["properties"]
    assert "updated" in properties["metadata"]["properties"]

    # Check required
    required = set(combined.get("required", []))
    assert "user" in required
    assert "metadata" not in required  # Not in any required array


def test_allof_with_overlapping_properties():
    """Test allOf with overlapping property names (later schemas override)."""

    route = HTTPRoute(
        path="/test",
        method="POST",
        operation_id="testOverlap",
        parameters=[],
        request_body=RequestBodyInfo(
            required=True,
            content_schema={
                "application/json": {
                    "allOf": [
                        {
                            "type": "object",
                            "properties": {
                                "name": {"type": "string", "minLength": 1},
                                "age": {"type": "integer"},
                            },
                            "required": ["name"],
                        },
                        {
                            "type": "object",
                            "properties": {
                                "name": {"type": "string", "maxLength": 50},  # Override
                                "email": {"type": "string"},
                            },
                            "required": ["email"],
                        },
                    ]
                }
            },
        ),
        responses={},
    )

    combined = _combine_schemas(route)

    properties = combined.get("properties", {})

    # Later schema should win for overlapping properties
    assert "name" in properties
    assert properties["name"]["type"] == "string"
    assert "maxLength" in properties["name"]  # From second schema
    assert properties["name"]["maxLength"] == 50

    # Check other properties
    assert "age" in properties
    assert "email" in properties

    # Both name and email should be required
    required = set(combined.get("required", []))
    assert "name" in required
    assert "email" in required


def test_no_allof_passthrough():
    """Test that schemas without allOf pass through unchanged."""

    route = HTTPRoute(
        path="/test",
        method="POST",
        operation_id="testNoAllOf",
        parameters=[],
        request_body=RequestBodyInfo(
            required=True,
            content_schema={
                "application/json": {
                    "type": "object",
                    "properties": {"simple": {"type": "string"}},
                    "required": ["simple"],
                }
            },
        ),
        responses={},
    )

    combined = _combine_schemas(route)

    # Should pass through unchanged
    properties = combined.get("properties", {})
    assert "simple" in properties
    assert properties["simple"]["type"] == "string"

    required = set(combined.get("required", []))
    assert "simple" in required

    # No allOf in original or result
    assert "allOf" not in combined