Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)