Skip to content

Commit 27acba1

Browse files
committed
fix: openapi working in sub-app
1 parent 365f158 commit 27acba1

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

src/a2a/server/apps/jsonrpc/fastapi_app.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import logging
2+
import sys
23

3-
from collections.abc import AsyncIterator
4-
from contextlib import asynccontextmanager
54
from typing import Any
65

76
from fastapi import FastAPI
87

8+
9+
if sys.version_info < (3, 12): # pragma: no cover
10+
from typing_extensions import override
11+
else: # pragma: no cover
12+
from typing import override
13+
914
from a2a.server.apps.jsonrpc.jsonrpc_app import (
1015
CallContextBuilder,
1116
JSONRPCApplication,
@@ -22,6 +27,28 @@
2227
logger = logging.getLogger(__name__)
2328

2429

30+
class A2AFastAPI(FastAPI):
31+
"""A FastAPI application that adds A2A-specific OpenAPI components."""
32+
33+
a2a_components_added: bool = False
34+
35+
@override
36+
def openapi(self) -> dict[str, Any]:
37+
openapi_schema = super().openapi()
38+
if not self.a2a_components_added:
39+
a2a_request_schema = A2ARequest.model_json_schema(
40+
ref_template='#/components/schemas/{model}'
41+
)
42+
defs = a2a_request_schema.pop('$defs', {})
43+
component_schemas = openapi_schema.setdefault(
44+
'components', {}
45+
).setdefault('schemas', {})
46+
component_schemas.update(defs)
47+
component_schemas['A2ARequest'] = a2a_request_schema
48+
self.a2a_components_added = True
49+
return openapi_schema
50+
51+
2552
class A2AFastAPIApplication(JSONRPCApplication):
2653
"""A FastAPI application implementing the A2A protocol server endpoints.
2754
@@ -112,23 +139,7 @@ def build(
112139
Returns:
113140
A configured FastAPI application instance.
114141
"""
115-
116-
@asynccontextmanager
117-
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
118-
a2a_request_schema = A2ARequest.model_json_schema(
119-
ref_template='#/components/schemas/{model}'
120-
)
121-
defs = a2a_request_schema.pop('$defs', {})
122-
openapi_schema = app.openapi()
123-
component_schemas = openapi_schema.setdefault(
124-
'components', {}
125-
).setdefault('schemas', {})
126-
component_schemas.update(defs)
127-
component_schemas['A2ARequest'] = a2a_request_schema
128-
129-
yield
130-
131-
app = FastAPI(lifespan=lifespan, **kwargs)
142+
app = A2AFastAPI(**kwargs)
132143

133144
self.add_routes_to_app(
134145
app, agent_card_url, rpc_url, extended_agent_card_url

0 commit comments

Comments
 (0)