diff --git a/src/google/adk/tools/openapi_tool/common/common.py b/src/google/adk/tools/openapi_tool/common/common.py index 281f4032771..d5fb88a1169 100644 --- a/src/google/adk/tools/openapi_tool/common/common.py +++ b/src/google/adk/tools/openapi_tool/common/common.py @@ -52,6 +52,16 @@ def _schema_from_openapi(value: object, *, context: str) -> Schema: raise TypeError(f'{context} must be an OpenAPI schema, got {type(value)!r}') +def _is_dictionary_schema(value: object) -> bool: + """Returns whether an object accepts entries without named properties.""" + return ( + isinstance(value, Schema) + and value.type == 'object' + and not value.properties + and value.additionalProperties is not False + ) + + def rename_python_keywords(s: str, prefix: str = 'param_') -> str: """Renames Python keywords by adding a prefix. diff --git a/src/google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py b/src/google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py index 127f0f8630d..48439b98a34 100644 --- a/src/google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py +++ b/src/google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py @@ -32,6 +32,7 @@ from fastapi.openapi.models import Schema from ..._gemini_schema_util import _to_snake_case +from ..common.common import _is_dictionary_schema from ..common.common import _schema_from_openapi from ..common.common import ApiParameter from ..common.common import PydocHelper @@ -189,7 +190,17 @@ def _process_request_body(self) -> None: ) description = request_body.description or '' - if schema.type == 'object': + if _is_dictionary_schema(schema): + self._params.append( + ApiParameter( + original_name='body', + param_location='body', + param_schema=schema, + description=description, + required=bool(request_body.required), + ) + ) + elif schema.type == 'object': properties = schema.properties or {} required_properties = set(schema.required or []) for prop_name, prop_details in properties.items(): diff --git a/src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py b/src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py index 18dbb70b259..f2f852349d1 100644 --- a/src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py +++ b/src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py @@ -59,6 +59,7 @@ from ..auth.auth_helpers import credential_to_param from ..auth.auth_helpers import dict_to_auth_scheme from ..auth.credential_exchangers.auto_auth_credential_exchanger import AutoAuthCredentialExchanger +from ..common.common import _is_dictionary_schema from ..common.common import ApiParameter from .openapi_spec_parser import OperationEndpoint from .openapi_spec_parser import ParsedOperation @@ -573,7 +574,7 @@ def _prepare_request_params( schema = media_type_object.schema_ body_data = None - if schema.type == "object": + if schema.type == "object" and not _is_dictionary_schema(schema): body_data = {} for param in parameters: if param.param_location == "body" and param.py_name in kwargs: diff --git a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py index cefba427795..0275483a894 100644 --- a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py +++ b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import os from typing import Any from typing import Dict @@ -25,7 +26,9 @@ from google.adk.auth.auth_credential import AuthCredential from google.adk.auth.auth_credential import AuthCredentialTypes from google.adk.tools.openapi_tool.openapi_spec_parser.openapi_toolset import OpenAPIToolset +from google.adk.tools.openapi_tool.openapi_spec_parser.operation_parser import OperationParser from google.adk.tools.openapi_tool.openapi_spec_parser.rest_api_tool import RestApiTool +import httpx import pytest import yaml @@ -583,3 +586,152 @@ def test_openapi_toolset_disambiguates_when_suffixed_name_already_taken( tool = toolset.get_tool(name) assert tool is not None assert tool.endpoint.path == f"/items/{i}" + + +@pytest.fixture +def body_tool_factory(): + """Build a real toolset with only the HTTP transport replaced.""" + + def build(schema, *, required=False, parameters=None): + requests = [] + + def respond(request): + requests.append(request) + return httpx.Response(200, json={"ok": True}) + + spec = { + "openapi": "3.0.3", + "info": {"title": "Body serialization", "version": "1"}, + "servers": [{"url": "https://example.invalid"}], + "paths": { + "/labels": { + "post": { + "operationId": "set_labels", + "parameters": parameters or [], + "requestBody": { + "required": required, + "content": {"application/json": {"schema": schema}}, + }, + "responses": {"200": {"description": "OK"}}, + } + } + }, + } + toolset = OpenAPIToolset( + spec_dict=spec, + httpx_client_factory=lambda: httpx.AsyncClient( + transport=httpx.MockTransport(respond) + ), + allow_private_network_access=True, + ) + return toolset.get_tool("set_labels"), requests + + return build + + +@pytest.mark.parametrize( + "schema,payload", + [ + ( + {"type": "object", "additionalProperties": {"type": "string"}}, + {"owner": "alice", "team": "red"}, + ), + ( + {"type": "object", "additionalProperties": True}, + {"enabled": True, "nested": {"count": 2}}, + ), + ({"type": "object", "additionalProperties": {}}, {"count": 2}), + ({"type": "object"}, {"owner": "alice"}), + ( + {"type": "object", "properties": {}, "additionalProperties": True}, + {"owner": "alice"}, + ), + ({"type": "object", "additionalProperties": {"type": "string"}}, {}), + ], +) +async def test_dictionary_request_body_preserves_entries( + body_tool_factory, schema, payload +): + """A dictionary is sent whole, including an explicitly empty dictionary.""" + tool, requests = body_tool_factory(schema) + + await tool.run_async(args={"body": payload}, tool_context=None) + + assert json.loads(requests[0].content) == payload + + +@pytest.mark.parametrize("required", [False, True]) +def test_dictionary_body_declaration_retains_schema_and_requiredness( + body_tool_factory, required +): + """The model can supply dictionary entries using one body argument.""" + schema = {"type": "object", "additionalProperties": {"type": "string"}} + tool, _ = body_tool_factory(schema, required=required) + + declaration = OperationParser(tool.operation).get_json_schema() + + assert declaration["properties"]["body"]["additionalProperties"] == { + "type": "string" + } + assert declaration["required"] == (["body"] if required else []) + + +async def test_dictionary_body_uses_deduplicated_parameter_name( + body_tool_factory, +): + """A query named body does not replace or wrap the dictionary payload.""" + tool, requests = body_tool_factory( + {"type": "object", "additionalProperties": True}, + parameters=[ + {"name": "body", "in": "query", "schema": {"type": "string"}} + ], + ) + + await tool.run_async( + args={"body": "filter", "body_0": {"owner": "alice"}}, + tool_context=None, + ) + + assert requests[0].url.params["body"] == "filter" + assert json.loads(requests[0].content) == {"owner": "alice"} + + +async def test_optional_dictionary_body_remains_absent(body_tool_factory): + """Omitting an optional dictionary does not send an empty JSON object.""" + tool, requests = body_tool_factory( + {"type": "object", "additionalProperties": True} + ) + + await tool.run_async(args={}, tool_context=None) + + assert requests[0].content == b"" + + +@pytest.mark.parametrize("additional_properties", [None, False, True]) +async def test_named_body_properties_keep_flat_arguments( + body_tool_factory, additional_properties +): + """Named-property bodies retain their existing argument interface.""" + schema = {"type": "object", "properties": {"owner": {"type": "string"}}} + if additional_properties is not None: + schema["additionalProperties"] = additional_properties + tool, requests = body_tool_factory(schema) + + declaration = OperationParser(tool.operation).get_json_schema() + await tool.run_async(args={"owner": "alice"}, tool_context=None) + + assert set(declaration["properties"]) == {"owner"} + assert json.loads(requests[0].content) == {"owner": "alice"} + + +async def test_closed_empty_object_body_keeps_no_arguments(body_tool_factory): + """An object that forbids extra keys still has no body argument.""" + tool, requests = body_tool_factory( + {"type": "object", "additionalProperties": False} + ) + + declaration = OperationParser(tool.operation).get_json_schema() + await tool.run_async(args={}, tool_context=None) + + assert declaration["properties"] == {} + assert json.loads(requests[0].content) == {} diff --git a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_operation_parser.py b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_operation_parser.py index 9dc0a428229..22ffd28fe2c 100644 --- a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_operation_parser.py +++ b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_operation_parser.py @@ -251,8 +251,8 @@ def test_process_request_body_one_of_schema_assigns_name(): assert '' not in schema['properties'] -def test_process_request_body_empty_object(): - """Test _process_request_body with a schema that is of type object but with no properties.""" +def test_process_request_body_open_object(): + """An object allows arbitrary dictionary entries unless explicitly closed.""" operation = Operation( requestBody=RequestBody( content={'application/json': MediaType(schema=Schema(type='object'))} @@ -260,7 +260,7 @@ def test_process_request_body_empty_object(): ) parser = OperationParser(operation, should_parse=False) parser._process_request_body() - assert len(parser._params) == 0 + assert [param.py_name for param in parser.get_parameters()] == ['body'] def test_dedupe_param_names(sample_operation):