diff --git a/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py b/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py index 1745698e410..8557e928b83 100644 --- a/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py +++ b/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py @@ -26,6 +26,7 @@ from ....auth.auth_credential import AuthCredential from ....auth.auth_schemes import AuthScheme +from ..._gemini_schema_util import _resolve_json_pointer from ..._gemini_schema_util import _to_snake_case from ..common.common import ApiParameter from .operation_parser import _required_scheme_name @@ -277,16 +278,20 @@ def _resolve_references(self, openapi_spec: Dict[str, Any]) -> Dict[str, Any]: def resolve_ref(ref_string, current_doc): """Resolves a single $ref string.""" - parts = ref_string.split("/") - if parts[0] != "#": + if ref_string.split("/")[0] != "#": raise ValueError(f"External references not supported: {ref_string}") - + # A JSON Pointer escapes "/" and "~" in keys as "~1" and "~0" (RFC 6901), + # as in a ref into `paths`, and indexes arrays by position. + resolved = _resolve_json_pointer(ref_string, current_doc) + if resolved is not None: + return resolved + # Some specs use keys verbatim in refs without escaping them (e.g. a + # schema named "My%20Schema"), so fall back to an unescaped lookup. current = current_doc - for part in parts[1:]: - if part in current: - current = current[part] - else: - return None # Reference not found + for part in ref_string.split("/")[1:]: + if not isinstance(current, dict) or part not in current: + return None + current = current[part] return current def recursive_resolve(obj, current_doc, seen_refs=None): diff --git a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py index f8f29d1a968..bb91f56c4a0 100644 --- a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py +++ b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py @@ -180,6 +180,70 @@ def test_parse_spec_with_reference(openapi_spec_generator): assert op.return_value.type_value.__origin__ is dict +def test_parse_spec_with_escaped_and_indexed_json_pointer_reference( + openapi_spec_generator, +): + """A $ref into `paths` escapes "/" as "~1" and indexes arrays (RFC 6901).""" + openapi_spec = { + "openapi": "3.1.0", + "info": {"title": "API with pointer refs", "version": "1.0.0"}, + "paths": { + "/pets/{petId}": { + "get": { + "operationId": "getPet", + "parameters": [{ + "name": "petId", + "in": "path", + "required": True, + "schema": {"type": "string"}, + }], + "responses": {"200": {"description": "OK"}}, + }, + "delete": { + "operationId": "deletePet", + "parameters": [ + {"$ref": "#/paths/~1pets~1%7BpetId%7D/get/parameters/0"} + ], + "responses": {"204": {"description": "Deleted"}}, + }, + } + }, + } + + parsed_operations = openapi_spec_generator.parse(openapi_spec) + delete_op = next(op for op in parsed_operations if op.name == "delete_pet") + + assert [ + (p.original_name, p.param_location) for p in delete_op.parameters + ] == [("petId", "path")] + + +@pytest.mark.parametrize( + "schema_name, ref", + [ + ("a~b", "#/components/schemas/a~0b"), + ("My%20Schema", "#/components/schemas/My%20Schema"), + ("a~1b", "#/components/schemas/a~1b"), + ], +) +def test_resolve_references_escaped_and_verbatim_keys( + openapi_spec_generator, schema_name, ref +): + """Escaped refs resolve, and refs that use a key verbatim still resolve.""" + openapi_spec = { + "components": { + "schemas": { + schema_name: {"type": "string"}, + "Holder": {"$ref": ref}, + } + } + } + + resolved = openapi_spec_generator._resolve_references(openapi_spec) + + assert resolved["components"]["schemas"]["Holder"] == {"type": "string"} + + def test_parse_spec_with_circular_reference(openapi_spec_generator): """Test correct handling of circular $ref (important!).""" openapi_spec = {