Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | """Tests for :func:`composio.utils.openapi.function_signature_from_jsonschema`. |
| 2 | |
| 3 | This builder is what the ``google_adk`` provider uses to attach a |
| 4 | ``__signature__`` to wrapped tools, so a crash here fails tool wrapping. The |
| 5 | top-level "no type" property already falls back to ``Any``; these tests cover |
| 6 | the nested combiner options (``oneOf``/``anyOf``/``allOf``) that used to raise. |
| 7 | """ |
| 8 | |
| 9 | import typing as t |
| 10 | |
| 11 | import pytest |
| 12 | |
| 13 | from composio.exceptions import InvalidSchemaError |
| 14 | from composio.utils import openapi |
| 15 | from composio.utils.openapi import function_signature_from_jsonschema |
| 16 | |
| 17 | |
| 18 | def _annotation(prop: t.Dict[str, t.Any]) -> t.Any: |
| 19 | params = function_signature_from_jsonschema({"properties": {"x": prop}}) |
| 20 | return params[0].annotation |
| 21 | |
| 22 | |
| 23 | class TestTypelessCombinerOptions: |
| 24 | """A combiner option without a ``type`` key should default to Any, not raise.""" |
| 25 | |
| 26 | @pytest.mark.unit |
| 27 | @pytest.mark.schema |
| 28 | def test_typeless_any_of_option_defaults_to_any(self): |
| 29 | annotation = _annotation( |
| 30 | {"anyOf": [{"description": "free form"}, {"type": "string"}]} |
| 31 | ) |
| 32 | assert annotation == t.Union[t.Any, str] |
| 33 | |
| 34 | @pytest.mark.unit |
| 35 | @pytest.mark.schema |
| 36 | def test_typeless_one_of_option_defaults_to_any(self): |
| 37 | annotation = _annotation( |
| 38 | {"oneOf": [{"type": "string"}, {"description": "free form"}]} |
| 39 | ) |
| 40 | assert annotation == t.Union[str, t.Any] |
| 41 | |
| 42 | @pytest.mark.unit |
| 43 | @pytest.mark.schema |
| 44 | def test_empty_all_of_resolves_to_any(self): |
| 45 | assert _annotation({"allOf": []}) is t.Any |
| 46 | |
| 47 | |
| 48 | class TestEmptyCombinerLists: |
| 49 | """An empty oneOf/anyOf has no options to union; fall back to Any.""" |
| 50 | |
| 51 | @pytest.mark.unit |
| 52 | @pytest.mark.schema |
| 53 | def test_empty_any_of(self): |
| 54 | assert _annotation({"anyOf": []}) is t.Any |
| 55 | |
| 56 | @pytest.mark.unit |
| 57 | @pytest.mark.schema |
| 58 | def test_empty_one_of(self): |
| 59 | assert _annotation({"oneOf": []}) is t.Any |
| 60 | |
| 61 | |
| 62 | class TestExistingShapesUnchanged: |
| 63 | """Shapes that already worked must keep resolving identically.""" |
| 64 | |
| 65 | @pytest.mark.unit |
| 66 | @pytest.mark.schema |
| 67 | def test_nullable_any_of_is_optional(self): |
| 68 | assert ( |
| 69 | _annotation({"anyOf": [{"type": "string"}, {"type": "null"}]}) |
| 70 | == t.Optional[str] |
| 71 | ) |
| 72 | |
| 73 | @pytest.mark.unit |
| 74 | @pytest.mark.schema |
| 75 | def test_multi_member_one_of(self): |
| 76 | annotation = _annotation( |
| 77 | { |
| 78 | "oneOf": [ |
| 79 | {"type": "string"}, |
| 80 | {"type": "integer"}, |
| 81 | {"type": "boolean"}, |
| 82 | {"type": "number"}, |
| 83 | ] |
| 84 | } |
| 85 | ) |
| 86 | assert annotation == t.Union[str, int, bool, float] |
| 87 | |
| 88 | @pytest.mark.unit |
| 89 | @pytest.mark.schema |
| 90 | def test_enum(self): |
| 91 | assert ( |
| 92 | _annotation({"type": "string", "enum": ["a", "b"]}) == t.Literal["a", "b"] |
| 93 | ) |
| 94 | |
| 95 | @pytest.mark.unit |
| 96 | @pytest.mark.schema |
| 97 | def test_array(self): |
| 98 | assert ( |
| 99 | _annotation({"type": "array", "items": {"type": "string"}}) == t.List[str] |
| 100 | ) |
| 101 | |
| 102 | @pytest.mark.unit |
| 103 | @pytest.mark.schema |
| 104 | def test_object(self): |
| 105 | assert _annotation({"type": "object"}) == t.Dict[str, t.Any] |
| 106 | |
| 107 | @pytest.mark.unit |
| 108 | @pytest.mark.schema |
| 109 | def test_typeless_property_is_any(self): |
| 110 | assert _annotation({"description": "free form"}) is t.Any |
| 111 | |
| 112 | |
| 113 | class TestListValuedType: |
| 114 | """A list-valued ``type`` (e.g. ``["string", "null"]``) resolves to a Union. |
| 115 | |
| 116 | JSON Schema Draft 2020-12 / OpenAPI 3.1 express a nullable field as a list of |
| 117 | types rather than an ``anyOf``. A list is unhashable, so the older |
| 118 | ``p_type in OPENAPI_TO_PYTHON`` membership test raised |
| 119 | ``TypeError: unhashable type: 'list'`` and crashed tool wrapping. |
| 120 | """ |
| 121 | |
| 122 | @pytest.mark.unit |
| 123 | @pytest.mark.schema |
| 124 | def test_nullable_list_is_optional(self): |
| 125 | assert _annotation({"type": ["string", "null"]}) == t.Optional[str] |
| 126 | |
| 127 | @pytest.mark.unit |
| 128 | @pytest.mark.schema |
| 129 | def test_single_member_list_resolves_to_that_type(self): |
| 130 | assert _annotation({"type": ["integer"]}) is int |
| 131 | |
| 132 | @pytest.mark.unit |
| 133 | @pytest.mark.schema |
| 134 | def test_multiple_non_null_members_build_a_union(self): |
| 135 | assert _annotation({"type": ["string", "integer"]}) == t.Union[str, int] |
| 136 | |
| 137 | @pytest.mark.unit |
| 138 | @pytest.mark.schema |
| 139 | def test_nullable_array_preserves_items_schema(self): |
| 140 | assert ( |
| 141 | _annotation({"type": ["array", "null"], "items": {"type": "string"}}) |
| 142 | == t.Optional[t.List[str]] |
| 143 | ) |
| 144 | |
| 145 | @pytest.mark.unit |
| 146 | @pytest.mark.schema |
| 147 | @pytest.mark.parametrize("property_type", ["file", ["file", "null"]]) |
| 148 | def test_unrecognized_member_raises_invalid_schema(self, property_type: t.Any): |
| 149 | with pytest.raises(InvalidSchemaError, match="Invalid property type file"): |
| 150 | _annotation({"type": property_type}) |
| 151 | |
| 152 | @pytest.mark.unit |
| 153 | @pytest.mark.schema |
| 154 | def test_empty_list_resolves_to_any(self): |
| 155 | assert _annotation({"type": []}) is t.Any |
| 156 | |
| 157 | |
| 158 | class TestArrayItemSchemas: |
| 159 | @pytest.mark.unit |
| 160 | @pytest.mark.schema |
| 161 | @pytest.mark.parametrize("combiner", ["anyOf", "oneOf"]) |
| 162 | def test_nullable_combiner_items(self, combiner: str): |
| 163 | assert ( |
| 164 | _annotation( |
| 165 | { |
| 166 | "type": "array", |
| 167 | "items": {combiner: [{"type": "string"}, {"type": "null"}]}, |
| 168 | } |
| 169 | ) |
| 170 | == t.List[t.Optional[str]] |
| 171 | ) |
| 172 | |
| 173 | @pytest.mark.unit |
| 174 | @pytest.mark.schema |
| 175 | def test_nested_array_combiner_items(self): |
| 176 | assert ( |
| 177 | _annotation( |
| 178 | { |
| 179 | "type": "array", |
| 180 | "items": { |
| 181 | "type": "array", |
| 182 | "items": {"anyOf": [{"type": "string"}, {"type": "null"}]}, |
| 183 | }, |
| 184 | } |
| 185 | ) |
| 186 | == t.List[t.List[t.Optional[str]]] |
| 187 | ) |
| 188 | |
| 189 | @pytest.mark.unit |
| 190 | @pytest.mark.schema |
| 191 | def test_combiner_nested_in_an_item_option(self): |
| 192 | assert ( |
| 193 | _annotation( |
| 194 | { |
| 195 | "type": "array", |
| 196 | "items": { |
| 197 | "anyOf": [ |
| 198 | {"oneOf": [{"type": "string"}, {"type": "integer"}]}, |
| 199 | {"type": "null"}, |
| 200 | ] |
| 201 | }, |
| 202 | } |
| 203 | ) |
| 204 | == t.List[t.Union[str, int, None]] |
| 205 | ) |
| 206 | |
| 207 | @pytest.mark.unit |
| 208 | @pytest.mark.schema |
| 209 | def test_all_of_items(self): |
| 210 | assert ( |
| 211 | _annotation({"type": "array", "items": {"allOf": [{"type": "string"}]}}) |
| 212 | == t.List[str] |
| 213 | ) |
| 214 | |
| 215 | @pytest.mark.unit |
| 216 | @pytest.mark.schema |
| 217 | def test_enum_items_without_explicit_type(self): |
| 218 | assert ( |
| 219 | _annotation({"type": "array", "items": {"enum": ["open", "closed"]}}) |
| 220 | == t.List[t.Literal["open", "closed"]] |
| 221 | ) |
| 222 | |
| 223 | @pytest.mark.unit |
| 224 | @pytest.mark.schema |
| 225 | @pytest.mark.parametrize("combiner", ["anyOf", "oneOf"]) |
| 226 | def test_item_enum_keeps_priority_over_combiner(self, combiner: str): |
| 227 | assert ( |
| 228 | _annotation( |
| 229 | { |
| 230 | "type": "array", |
| 231 | "items": { |
| 232 | "type": "string", |
| 233 | "enum": ["open", "closed"], |
| 234 | combiner: [{"type": "string"}, {"type": "null"}], |
| 235 | }, |
| 236 | } |
| 237 | ) |
| 238 | == t.List[t.Literal["open", "closed"]] |
| 239 | ) |
| 240 | |
| 241 | @pytest.mark.unit |
| 242 | @pytest.mark.schema |
| 243 | @pytest.mark.parametrize("combiner", ["anyOf", "oneOf"]) |
| 244 | def test_item_type_keeps_priority_over_combiner(self, combiner: str): |
| 245 | assert ( |
| 246 | _annotation( |
| 247 | { |
| 248 | "type": "array", |
| 249 | "items": { |
| 250 | "type": "string", |
| 251 | combiner: [{"type": "string"}, {"type": "null"}], |
| 252 | }, |
| 253 | } |
| 254 | ) |
| 255 | == t.List[str] |
| 256 | ) |
| 257 | |
| 258 | @pytest.mark.unit |
| 259 | @pytest.mark.schema |
| 260 | @pytest.mark.parametrize( |
| 261 | "prop", |
| 262 | [ |
| 263 | {"type": "array"}, |
| 264 | {"type": "array", "items": {}}, |
| 265 | {"type": "array", "items": {"anyOf": []}}, |
| 266 | ], |
| 267 | ) |
| 268 | def test_unconstrained_items_still_resolve_to_any(self, prop: t.Dict[str, t.Any]): |
| 269 | assert _annotation(prop) == t.List[t.Any] |
| 270 | |
| 271 | @pytest.mark.unit |
| 272 | @pytest.mark.schema |
| 273 | @pytest.mark.parametrize("combiner", ["anyOf", "oneOf", "allOf"]) |
| 274 | def test_deeply_nested_item_combiners_are_bounded(self, combiner: str): |
| 275 | items: t.Dict[str, t.Any] = {"type": "string"} |
| 276 | for _ in range(400): |
| 277 | items = {combiner: [items]} |
| 278 | assert _annotation({"type": "array", "items": items}) == t.List[t.Any] |
| 279 | |
| 280 | @pytest.mark.unit |
| 281 | @pytest.mark.schema |
| 282 | def test_deeply_nested_array_items_are_bounded(self): |
| 283 | prop: t.Dict[str, t.Any] = {"type": "string"} |
| 284 | for _ in range(400): |
| 285 | prop = {"type": "array", "items": prop} |
| 286 | annotation = _annotation(prop) |
| 287 | depth = 0 |
| 288 | while t.get_origin(annotation) is list: |
| 289 | depth += 1 |
| 290 | annotation = t.get_args(annotation)[0] |
| 291 | assert 0 < depth < 400 |
| 292 | assert annotation is t.Any |
| 293 | |
| 294 | @pytest.mark.unit |
| 295 | @pytest.mark.schema |
| 296 | @pytest.mark.parametrize( |
| 297 | "wrap", |
| 298 | [ |
| 299 | lambda inner: {"type": ["array"] * 8, "items": inner}, |
| 300 | lambda inner: {"type": "array", "items": {"anyOf": [inner] * 8}}, |
| 301 | ], |
| 302 | ids=["type-list", "item-combiner"], |
| 303 | ) |
| 304 | def test_branching_item_schemas_are_bounded_in_work( |
| 305 | self, |
| 306 | wrap: t.Callable[[t.Dict[str, t.Any]], t.Dict[str, t.Any]], |
| 307 | monkeypatch: pytest.MonkeyPatch, |
| 308 | ): |
| 309 | # Eight branches per level make the work grow 8x per level, far below |
| 310 | # the depth bound; without a work budget this never finishes. |
| 311 | prop: t.Dict[str, t.Any] = {"type": "string"} |
| 312 | for _ in range(30): |
| 313 | prop = wrap(prop) |
| 314 | calls = 0 |
| 315 | convert = openapi._type_to_parameter |
| 316 | |
| 317 | def counting(*args: t.Any, **kwargs: t.Any) -> t.Any: |
| 318 | nonlocal calls |
| 319 | calls += 1 |
| 320 | return convert(*args, **kwargs) |
| 321 | |
| 322 | monkeypatch.setattr(openapi, "_type_to_parameter", counting) |
| 323 | _annotation(prop) |
| 324 | assert calls < 100_000 |
| 325 | |
| 326 | @pytest.mark.unit |
| 327 | @pytest.mark.schema |
| 328 | def test_long_type_list_stops_at_the_work_budget( |
| 329 | self, monkeypatch: pytest.MonkeyPatch |
| 330 | ): |
| 331 | # Every member copies the whole schema, so members past the budget |
| 332 | # must not be visited at all. |
| 333 | prop: t.Dict[str, t.Any] = {"type": ["string"] * 10_000} |
| 334 | calls = 0 |
| 335 | convert = openapi._type_to_parameter |
| 336 | |
| 337 | def counting(*args: t.Any, **kwargs: t.Any) -> t.Any: |
| 338 | nonlocal calls |
| 339 | calls += 1 |
| 340 | return convert(*args, **kwargs) |
| 341 | |
| 342 | monkeypatch.setattr(openapi, "_type_to_parameter", counting) |
| 343 | assert _annotation(prop) == t.Union[str, t.Any] |
| 344 | assert calls <= openapi._MAX_SCHEMA_NODES + 1 |