Skip to content

fix(openapi): handle self-referencing schemas without RecursionError (adopted from #306) - #335

Open
K4bain wants to merge 1 commit into
tadata-org:mainfrom
K4bain:fix/schema-recursion
Open

fix(openapi): handle self-referencing schemas without RecursionError (adopted from #306)#335
K4bain wants to merge 1 commit into
tadata-org:mainfrom
K4bain:fix/schema-recursion

Conversation

@K4bain

@K4bain K4bain commented Aug 27, 2026

Copy link
Copy Markdown

Adopted from #306 (original patch by @mojixcoder) — rebased onto current main and verified.

Why this is needed

Any FastAPI app with a recursive Pydantic model (a filter tree, a nested comment/category model, etc.) crashes convert_openapi_to_mcp_tools with RecursionError — resolve_schema_references() re-resolves the same forever (issue #287).

The fix threads a _visited frozenset of paths through the recursion: when a ref is already on the current expansion path (a cycle), it is left in place instead of being expanded again. Clients that understand (any spec-compliant MCP client) handle the cycle fine; the library just stops exploding.

Verification

  • New regression test builds an app with a self-referencing CompoundFilter model (filters: List[Union[ComparisonFilter, CompoundFilter]]) and converts the OpenAPI schema. On main: RecursionError. With this fix: conversion succeeds, tool inputSchema contains query + filters.
  • Full unit suite: 70 passed (69 existing + 1 new).

Credit to @mojixcoder for the original report and patch in #306.

resolve_schema_references() recursed infinitely when the OpenAPI schema
contained a recursive Pydantic model (e.g. a filter tree referencing
itself), crashing tool conversion with RecursionError. Track  on
the current expansion path and leave circular refs in place instead of
re-resolving them.

Adopted from tadata-org#306 (original patch by @mojixcoder), fixes tadata-org#287.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant