feat(v3): support receiving multiple messages per operation (#333) - #336
Merged
Merged
Conversation
Follow-up to #140/#332, which shipped the send side. When a receive operation (or its channel) declares more than one message, generate one subscribe function per message instead of silently handling only the first one: - SubscribeToXForUserCreated / SubscribeToXForUserDeleted, with one callback per message type, plus matching UnsubscribeFromXFor... and per-message methods on the subscriber interface. Single-message operations are unchanged (byte-identical output). - A single broker subscription is opened per channel and multiplexed to the registered per-message handlers, so several per-message subscriptions on one channel no longer collide. - Received messages are discriminated with a try-each-until-valid strategy (unmarshal + validate against each candidate schema using go-playground/validator) and dispatched to the first match. - A message matching none of the expected types is nacked and surfaced through the error handler as the new extensions.ErrNoMatchingMessage. Adds a broker-backed regression test (test/v3/issues/333) covering dispatch of two distinct message types on one channel, the no-match path, and per-message unsubscribe. Documents the behavior and the validator dependency in the README. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #333. Follow-up to #140/#332 (which shipped the send side); this completes the receive side.
What changed
When a receive operation (or its channel) declares more than one message, the generator now produces one subscribe function per message instead of silently handling only the first one.
SubscribeToXForUserCreated(ctx, fn)/SubscribeToXForUserDeleted(ctx, fn), one callback per message type, plus matchingUnsubscribeFromXFor...functions and per-message methods on the subscriber interface. Single-message operations are unchanged (byte-identical output).*messageDispatcherso the controller stays copy-safe). Several per-message subscriptions on one channel no longer collide, and the broker subscription is canceled once the last message type is unsubscribed.go-playground/validator, already a dependency) and dispatched to the first match. Heuristic and best when messages have distinguishing required /enum/constfields, as acknowledged in AsyncAPI v3: support receiving multiple messages per operation (follow-up to #140) #333.Naked and surfaced through the error handler as the newextensions.ErrNoMatchingMessage, consistent with the existing processing-error path.This also completes the
UserControllerreceive side of the #140 fixture, which previously only handled the first message.Design decisions
The two open decisions in the issue were resolved as: single subscription + multiplex (keeps single-message output byte-identical; one broker subscription per channel) and Nak + errorHandler for unmatched messages (consistent with the existing error path; users can override via
WithErrorHandler).Tests
test/v3/issues/333is a broker-backed regression test covering: dispatch of two distinct message types on one channel, the no-match (Nak +ErrNoMatchingMessage) path, and per-message unsubscribe. Passes with-race.Notes
go-playground/validator/v10; documented in the README.130/requestreplyand186golden files have a trivial cosmetic whitespace change (closing}was previously jammed onto the next function's doc comment); behavior is identical.🤖 Generated with Claude Code