From ab45fac81d055e594624120bfaee08200ec5f687 Mon Sep 17 00:00:00 2001 From: chelsealong Date: Sun, 29 Nov 2026 19:19:38 +0000 Subject: [PATCH] fix(openapi): preserve schema instance values during sanitization --- .../openapi_spec_parser.py | 58 ++++++++++++++++--- .../test_openapi_spec_parser.py | 28 +++++++++ 2 files changed, 77 insertions(+), 9 deletions(-) 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..6b0eba0b162 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 @@ -45,6 +45,33 @@ }) _SCHEMA_CONTAINER_KEYS: Set[str] = frozenset({"schema", "schemas"}) +_SCHEMA_MAP_KEYS: Set[str] = frozenset({ + "$defs", + "definitions", + "dependentSchemas", + "patternProperties", + "properties", +}) +_SCHEMA_LIST_KEYS: Set[str] = frozenset({ + "allOf", + "anyOf", + "oneOf", + "prefixItems", +}) +_NESTED_SCHEMA_KEYS: Set[str] = frozenset({ + "additionalItems", + "additionalProperties", + "contains", + "contentSchema", + "else", + "if", + "items", + "not", + "propertyNames", + "then", + "unevaluatedItems", + "unevaluatedProperties", +}) class OperationEndpoint(BaseModel): @@ -152,24 +179,37 @@ def sanitize_type_field(schema_dict: Dict[str, Any]) -> None: else: del schema_dict["type"] - def sanitize_recursive(obj: Any, *, in_schema: bool) -> Any: + def sanitize_recursive(obj: Any, *, position: str) -> Any: + if position == "value": + return obj if isinstance(obj, dict): - if in_schema: + if position == "schema": sanitize_type_field(obj) - # Recursively process all values in the dict for key, value in obj.items(): - obj[key] = sanitize_recursive( - value, - in_schema=in_schema or key in _SCHEMA_CONTAINER_KEYS, - ) + child_position = "value" if position == "schema" else "other" + if position == "schema_map": + child_position = "schema" + elif position == "schema": + if key in _NESTED_SCHEMA_KEYS: + child_position = "schema" + elif key in _SCHEMA_MAP_KEYS: + child_position = "schema_map" + elif key in _SCHEMA_LIST_KEYS: + child_position = "schema_list" + elif key in _SCHEMA_CONTAINER_KEYS: + child_position = "schema" if key == "schema" else "schema_map" + obj[key] = sanitize_recursive(value, position=child_position) return obj elif isinstance(obj, list): - return [sanitize_recursive(item, in_schema=in_schema) for item in obj] + child_position = "schema" if position == "schema_list" else "other" + return [ + sanitize_recursive(item, position=child_position) for item in obj + ] else: return obj - return sanitize_recursive(openapi_spec, in_schema=False) + return sanitize_recursive(openapi_spec, position="other") def _collect_operations( self, openapi_spec: Dict[str, Any] 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..0d521a7a007 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 @@ -16,6 +16,7 @@ from typing import Dict from google.adk.tools.openapi_tool.openapi_spec_parser.openapi_spec_parser import OpenApiSpecParser +from jsonschema import Draft202012Validator import pytest @@ -960,3 +961,30 @@ def test_sanitize_schema_types_removes_all_invalid_list(openapi_spec_generator): # Type field should be removed entirely assert "type" not in sanitized["schema"] + + +def test_parse_preserves_object_values_in_schema_examples_and_defaults( + openapi_spec_generator, +): + """Schema instance values remain valid after parsing an operation.""" + value = {"type": "invoice", "amount": 1} + schema = { + "type": "object", + "properties": {"amount": {"type": "INTEGER"}}, + "default": value, + "enum": [value], + "examples": [{"type": "STRING", "amount": 1}], + } + spec = create_minimal_openapi_spec() + spec["paths"]["/test"]["get"]["parameters"] = [ + {"name": "filter", "in": "query", "schema": schema} + ] + + parsed = openapi_spec_generator.parse(spec)[0].parameters[0].param_schema + result = parsed.model_dump(exclude_none=True) + + assert parsed.default == value + assert parsed.enum == [value] + assert parsed.examples == [{"type": "STRING", "amount": 1}] + assert result["properties"]["amount"]["type"] == "integer" + assert Draft202012Validator(result).is_valid(value)