Skip to content

feat: use FastDepends serializers for AsyncAPI payload schemas - #3334

Open
Siyet wants to merge 10 commits into
ag2ai:0.8.0from
Siyet:2101
Open

Siyet wants to merge 10 commits into
ag2ai:0.8.0from
Siyet:2101

Conversation

@Siyet

@Siyet Siyet commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Generate AsyncAPI 2.6/3.0 payload schemas through the selected FastDepends backend for subscribers, publisher annotations and explicit schema= types. Support Msgspec Structs, aliases and constraints, preserving reference scopes and metadata.

Requires FastDepends 4 (minimum 4.0.0a0). Pydantic remains required for AsyncAPI document models and FastAPI. Publishers continue to use the generic broker encoder.

Local paired measurements: sample document generation 0.67 to 0.83 ms; no confirmed message dispatch slowdown.

Depends on #3333.
Closes #2101.

@CLAassistant

CLAassistant commented Nov 14, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added documentation Improvements or additions to documentation dependencies Pull requests that update a dependency file AsyncAPI Issues related to AsyncAPI specification generation labels Nov 14, 2026
@Lancetnik
Lancetnik changed the base branch from main to 0.8.0 November 15, 2026 17:50
@Lancetnik

Copy link
Copy Markdown
Member

Reviewed 4aa69015a3a75c91a379380f609bd32ea61af417 against 0.8.0 (8d25349a). The reference collision below still needs a fix before merge.

Standards

  • P3 — assertion structure. base/arguments.py:58–60 and test_schema_refs.py:182–185 check one schema through several field assertions. The repository's One equality per behaviour rule calls for one comparison using matchers such as IsPartialDict. Keep independent checks, such as verifying that the input was not mutated, separate.
  • P3 — possible duplicated code, optional cleanup. The ResourceFactory / ResourceSerializer pairs at base/arguments.py:168 and base/arguments.py:226 repeat the factory signatures, passthrough validation and schema processing. They differ mainly in how the input document is built. A shared test factory in base/serialization.py could remove that duplication. This is a judgement call, not a documented architecture violation.

Spec

P2 — payload definitions from different endpoints overwrite each other. This violates the stated requirement to preserve reference scopes and metadata.

For fields carrying $id, process_schema(..., embed=True) retains the input wrapper under the fixed name __fast_depends_input. Both generators then unconditionally merge it into the shared schema components: AsyncAPI 2.6 and AsyncAPI 3.0.

For example, register two subscribers with these body parameters:

# First subscriber
value: Annotated[int, Field(json_schema_extra={"$id": "https://example.test/A"})]
# Second subscriber
value: Annotated[str, Field(json_schema_extra={"$id": "https://example.test/B"})]

Both payloads end up referencing #/components/schemas/__fast_depends_input/properties/value. Whichever definition is written last determines the documented type for both handlers, so one is wrong. Using different parameter names can instead produce dangling references.

Confirmed on the current commit with FastDepends 4.0.0a1 for both AsyncAPI versions, Pydantic and Msgspec, and subscribers and explicit publisher schemas. The simple Pydantic subscriber case also confirms a regression against the base implementation, which keeps the two payloads separate.

Namespace the retained definitions per payload and rewrite their references consistently. A regression test with two endpoints in the same document should cover this.

Standards: 2 findings, highest priority P3 (assertion structure). Spec: 1 finding, P2 (component collision).

@IvanKirpichnikov IvanKirpichnikov left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The msgspec models of the same name replace each other's schemes. If two subscribers accept list[a.Record] and list[b.Record] with different fields, both receive a $ref on components/schemas/Record. In both versions of AsyncAPI, only one definition is saved, without warning. Different definitions need separate keys.
  • oneOf from msgspec metadata breaks AsyncAPI 2.6. msgspec.Meta(extra_json_schema={"oneOf": [{"minimum": 1}, {"maximum": -1}]}) leads to AttributeError. The generator considers each element of the list to be a reference and calls .split() on the number. In 3.0, both restrictions remain. The 2.6 branch should save the oneOf array inside the payload schema
  • The tagged union msgspec generates an incorrect discriminator. For Created | Removed, where both structures are declared with tag=True, both versions of the generator output discriminator: type in the scheme with anyOf. The type field is declared and required only in child schemas. AsyncAPI 3.0 requires declaring it and including it in required on the discriminator schema itself.

@Siyet

Siyet commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thx, fixed all three.

  1. Conflicting definitions are now stored under separate component names. Payload and internal references are updated together.
  2. AsyncAPI 2.6 now preserves regular oneOf arrays inside the payload schema.
  3. The discriminator property is now declared and required on the union schema itself. anyOf is preserved.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AsyncAPI Issues related to AsyncAPI specification generation dependencies Pull requests that update a dependency file documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Generate AsyncAPI from msgspec.Struct

4 participants