From 74f42338d2a8bb66c1f684805ba191df3c4d8559 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 27 May 2026 13:53:35 -0300 Subject: [PATCH] Fix blog OpenAPI agent path parameters --- agent.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/agent.py b/agent.py index adefb28..bd596ec 100644 --- a/agent.py +++ b/agent.py @@ -2,6 +2,7 @@ from __future__ import annotations import base64 import json +import re from typing import Any import httpx @@ -30,6 +31,41 @@ OPERATIONS = json.loads("{\n \"create_blog_post\": {\n \"description\": \"Cr ROOT_SECURITY = json.loads("[]") SECURITY_SCHEMES = json.loads("{\n \"apiKeyAuth\": {\n \"description\": \"Use the configured `BLOG_API_KEY` value.\",\n \"in\": \"header\",\n \"name\": \"x-api-key\",\n \"type\": \"apiKey\"\n },\n \"bearerAuth\": {\n \"description\": \"Use `Authorization: Bearer $BLOG_API_KEY`.\",\n \"scheme\": \"bearer\",\n \"type\": \"http\"\n }\n}") SECURITY_FIELDS = json.loads("{\n \"apiKeyAuth\": {\n \"field\": \"BLOG_API_KEY\",\n \"kind\": \"apiKey\",\n \"location\": \"header\",\n \"name\": \"x-api-key\"\n },\n \"bearerAuth\": {\n \"field\": \"BLOG_API_KEY\",\n \"kind\": \"http\",\n \"scheme\": \"bearer\"\n }\n}") +PATH_PARAMETER_RE = re.compile(r"{([^}/]+)}") + + +def _ensure_path_parameters() -> None: + for operation in OPERATIONS.values(): + path = str(operation.get("path") or "") + parameters = operation.get("parameters") + if not isinstance(parameters, list): + parameters = [] + operation["parameters"] = parameters + seen = { + (param.get("name"), param.get("in")) + for param in parameters + if isinstance(param, dict) + } + insert_at = 0 + for name in PATH_PARAMETER_RE.findall(path): + key = (name, "path") + if key in seen: + continue + parameters.insert( + insert_at, + { + "name": name, + "in": "path", + "required": True, + "description": f"Path parameter `{name}`.", + "schema": {"type": "string"}, + }, + ) + insert_at += 1 + seen.add(key) + + +_ensure_path_parameters() class OperationInput(BaseModel): @@ -349,6 +385,10 @@ class BlogOpenapiAgent(A2AAgent): headers[name] = str(value) elif location == "cookie": cookies[name] = value + unresolved = PATH_PARAMETER_RE.findall(path) + if unresolved: + missing = ", ".join(sorted(set(unresolved))) + raise ValueError("missing required path parameter(s): " + missing) if cookies: headers["cookie"] = "; ".join(f"{key}={value}" for key, value in cookies.items()) auth_headers, auth_query = self._auth_for_operation(ctx, operation)