Skip to content

Publish/Send should pass declared producer message type (Produce<T>) even if sending derived message type - #445

Merged
zarusz merged 1 commit into
masterfrom
feature/DeclaredProducerTypeInSerializer
Nov 28, 2025
Merged

zarusz merged 1 commit into
masterfrom
feature/DeclaredProducerTypeInSerializer

Conversation

@zarusz

@zarusz zarusz commented Nov 28, 2025

Copy link
Copy Markdown
Owner

Restoring v2 Serialization Behavior in v3

This PR reintroduces the serialization behavior from SlimMessageBus v2 to ensure backward compatibility:

  • v2 behavior: The serializer was always given the MessageType of Producer<T>(), even when a derived message type was published.
  • v3 behavior: The serializer received the actual runtime message type, which caused compatibility issues with existing serializers.

Changes included in this PR:

  • Updated documentation to explicitly define and clarify the intended (v2) serialization behavior.
  • Added comprehensive tests - covering interceptors and bulk publishing - to enforce and validate this behavior going forward.

Note on Serialization with Polymorphic Messages:

When publishing a derived message type (e.g., CustomerCreatedEvent) with only the base type configured (e.g., Produce<CustomerEvent>), the serializer's Serialize method will receive the configured type from the Produce<T>() declaration (i.e., CustomerEvent), not the actual runtime type (CustomerCreatedEvent). This allows serializers to maintain consistent type information in headers and ensures proper polymorphic behavior.

However, if you explicitly declare Produce<CustomerCreatedEvent>() for the derived type, the serializer will receive CustomerCreatedEvent instead. SMB will use the most specific matching producer configuration available for each message type.

This behavior applies to both single message publishing via Publish() and bulk publishing with collections of messages.


@zarusz zarusz self-assigned this Nov 28, 2025
@zarusz zarusz added the Core label Nov 28, 2025
@zarusz
zarusz force-pushed the feature/DeclaredProducerTypeInSerializer branch from 1feafaf to a8ad9e7 Compare November 28, 2025 10:17
…even if sending derived message type

Signed-off-by: Tomasz Maruszak <maruszaktomasz@gmail.com>
@zarusz
zarusz force-pushed the feature/DeclaredProducerTypeInSerializer branch from a8ad9e7 to 2ab2771 Compare November 28, 2025 10:19
@sonarqubecloud

Copy link
Copy Markdown

@zarusz
zarusz merged commit 1ed24d9 into master Nov 28, 2025
@zarusz
zarusz deleted the feature/DeclaredProducerTypeInSerializer branch November 28, 2025 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant