Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Edison-A-N
Copy link

Describe your changes

Added circular reference detection to prevent RecursionError when processing recursive Pydantic models. The solution uses visited_paths tracking to detect circular references and returns placeholder schemas to break infinite recursion cycles.

Issue ticket number and link (if applicable)

Fixes #238

Screenshots of the feature / bugfix

N/A - This is a backend fix for schema resolution

Checklist before requesting a review

  • Added relevant tests
  • Run ruff & mypy
  • All tests pass

Technical Details

Problem: RecursionError when processing recursive Pydantic models with circular references

Solution:

  • Add visited_paths tracking to detect circular references
  • Return placeholder schema when circular reference detected
  • Maintain backward compatibility

How it works:
When a circular reference is detected:

  1. First occurrence: Normal schema resolution with all properties
  2. Subsequent occurrences: Placeholder schema with {"type": "object", "description": "Circular reference detected", "properties": {}}

Impact & Limitations:
Preserved functionality:

  • All non-circular properties remain fully accessible
  • First-level schema information is complete
  • API documentation remains clear and usable

⚠️ Limitations:

  • Deep nested structures beyond the circular reference point show placeholder
  • Infinite depth nesting is not supported (by design)

Changes:

  • Modified resolve_schema_references() in fastapi_mcp/openapi/utils.py
  • Added comprehensive test suite for circular reference scenarios
  • All existing tests pass

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.

RecursionError

1 participant