Skip to content

Transformation: Favor exhaustive pattern matching over polymorphism for closed hierarchies - #673

Open
hjohn wants to merge 2 commits into
mainfrom
feature/transformation-improvements
Open

hjohn wants to merge 2 commits into
mainfrom
feature/transformation-improvements

Conversation

@hjohn

@hjohn hjohn commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

EventTransformation had two operations on it, matcher() and an inherited transform(...), both marked @Internal. Neither was ever dispatched polymorphically: everywhere the chain did something with a transformation it first switched on the concrete type, and only then called through the interface. Because they sat on the interface they had to be public, which is how framework-only operations ended up on a type users hold. Both are now package-private on the variants and called directly after the switch, with a small matcherOf(...) helper in TransformationIndex for the few sites that had not already narrowed. EventTransformation carries no members at all, declared or inherited.

The usual objection to trading polymorphism for a switch is that adding a variant means touching every call site. That cost does not apply here: the four variants are package-private, so nobody outside the module could add a fifth, and because the hierarchy is sealed, adding one later is a compile error at each switch rather than a silent gap. DropEventTransformation.transform existed only to satisfy the interface and was never invoked, since the chain short-circuits drops before reaching it, so it is gone, together with the test asserting that direct-call contract. No behaviour changed.

MessageTransformation stays, as a marker interface for the transformations of each kind of message: it loses transform(...) and its type parameter.

Breaking change (5.5.0 release notes): MessageTransformation<M extends Message>, public since 5.2.0, no longer has a type parameter, so code referring to MessageTransformation<...> no longer compiles.

This PR should be back ported to axoniq-5.4.x once approved and merged.

@hjohn hjohn added this to the Release 5.4.1 milestone Oct 19, 2026
@hjohn hjohn self-assigned this Oct 19, 2026
@hjohn
hjohn requested a review from a team as a code owner October 19, 2026 08:21
@hjohn hjohn added Priority 2: Should High priority. Ideally, these issues are part of the release they’re assigned to. Type: Enhancement Use to signal an issue enhances an already existing feature of the project. labels Oct 19, 2026
@hjohn
hjohn requested review from MateuszNaKodach, hatzlj and schananas and removed request for a team October 19, 2026 08:21
@hjohn hjohn changed the title Favor exhaustive pattern matching over polymorphism for closed hierarchies Transformation: Favor exhaustive pattern matching over polymorphism for closed hierarchies Oct 19, 2026
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Build status (run 45476982876, commit cc66586)

JDK 21 - SUCCESS - 33 modules (0 cached, 33 built) in 22:26

Per-module details
ModuleCacheTime
✅ axoniq-frameworkbuilt00:01
✅ axoniq-test-loggingbuilt00:01
✅ axoniq-framework-parentbuilt00:03
✅ axoniq-framework-bombuilt00:00
✅ axoniq-postgresqlbuilt00:54
✅ axoniq-distributed-messagingbuilt00:11
✅ axoniq-testcontainerbuilt00:37
✅ axon-server-connectorbuilt01:57
✅ axoniq-springcloudbuilt00:18
✅ axoniq-dead-letterbuilt00:29
✅ axoniq-event-streamingbuilt00:14
✅ axoniq-multi-tenancybuilt00:22
✅ axoniq-workflow-parentbuilt00:00
✅ axoniq-workflow-enginebuilt00:36
✅ axoniq-tracing-micrometerbuilt00:05
✅ axoniq-workflow-dslbuilt00:02
✅ axoniq-workflow-testbuilt00:11
✅ axoniq-framework-springbuilt00:00
✅ axoniq-spring-boot-autoconfigurebuilt01:57
✅ axoniq-spring-boot-starterbuilt00:00
✅ axoniq-integrationtestsbuilt00:00
✅ axoniq-integrationtests-supportbuilt00:01
✅ axoniq-integrationtests-connectorbuilt00:00
✅ axoniq-integrationtests-conformancebuilt03:00
✅ axoniq-integrationtests-tracingbuilt00:10
✅ axoniq-integrationtests-dead-letterbuilt00:03
✅ axoniq-legacybuilt03:17
✅ axoniq-integrationtests-legacybuilt00:11
✅ axoniq-integrationtests-workflowbuilt00:37
✅ axoniq-legacy-testbuilt00:10
✅ axoniq-message-transformationbuilt00:07
✅ axoniq-workflow-dsl-kotlinbuilt00:16
✅ axoniq-docs-samplesbuilt00:07

JDK 25 - SUCCESS - 33 modules (31 cached, 2 built) in 00:25

Per-module details
ModuleCacheTime
✅ axoniq-frameworkcached00:00
✅ axoniq-test-loggingcached00:00
✅ axoniq-framework-parentcached00:03
✅ axoniq-framework-bomcached00:00
✅ axoniq-postgresqlcached00:01
✅ axoniq-distributed-messagingcached00:00
✅ axoniq-testcontainercached00:00
✅ axon-server-connectorcached00:00
✅ axoniq-springcloudcached00:00
✅ axoniq-dead-lettercached00:00
✅ axoniq-event-streamingcached00:00
✅ axoniq-multi-tenancycached00:00
✅ axoniq-workflow-parentcached00:00
✅ axoniq-workflow-enginecached00:00
✅ axoniq-tracing-micrometercached00:00
✅ axoniq-workflow-dslcached00:00
✅ axoniq-workflow-testcached00:00
✅ axoniq-framework-springcached00:00
✅ axoniq-spring-boot-autoconfigurecached00:01
✅ axoniq-spring-boot-startercached00:00
✅ axoniq-integrationtestscached00:00
✅ axoniq-integrationtests-supportcached00:00
✅ axoniq-integrationtests-connectorcached00:00
✅ axoniq-integrationtests-conformancecached00:00
✅ axoniq-integrationtests-tracingcached00:00
✅ axoniq-integrationtests-dead-lettercached00:00
✅ axoniq-legacycached00:00
✅ axoniq-integrationtests-legacycached00:00
✅ axoniq-integrationtests-workflowcached00:00
✅ axoniq-legacy-testcached00:00
✅ axoniq-message-transformationbuilt00:08
✅ axoniq-workflow-dsl-kotlincached00:01
✅ axoniq-docs-samplesbuilt00:07

@hjohn
hjohn force-pushed the feature/transformation-improvements branch from ee6e0ab to 9ae39bf Compare October 19, 2026 12:04

@smcvb smcvb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd prefer we keep the MessageTransformation interface. I had Laura add that deliberately, as I'd want us to support command and query transformation for rolling upgrade scenarios. The MessageTransformation is the generic form for any message. I am find if we drop the internal method, though, thus changing it into a true marker interface.

* @author Laura Devriendt
* @since 5.2.0
*/
public interface MessageTransformation<M extends Message> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This interface exists as a precursor to allow transformation of things other than an EventMessage. Perhaps there's no generic contract yet...but if the transform method specifically is bothering you, we can also only drop that. That would change this into a true marker interface, just as our MessageHandler. However, complete removal of the file doesn't strike me as necessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It can be kept, but it is a bit of misnomer, since Snapshots are Entities not Messages.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is back, let me know if the docs on it aren't too verbose.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It can be kept, but it is a bit of misnomer, since Snapshots are Entities not Messages.

But the snapshots are delivered as SnapshotEventMessage extends GenericEventMessage via the MessageStream returned by the EventStoreTransaction#source(...) operation - so arguably you're transforming a messsage as well probably.

@hjohn hjohn Oct 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's true, but MessageStream is a misnomer as well :) I didn't want to make a snapshot message, but I had no choice as it wouldn't otherwise fit in the source API.

Limiting MessageStream to messages like we did (with a generic bound) means that you can't do a source/query, then transform the result to something you can answer a user request with (you can't feed users messages) -- let's say you need to go all the way to a list of Strings as the answer to a user's query. With MessageStream as it is currently, at some point you're stuck, and have to read the entire stream, and convert it to a java.util.Stream to proceed with whatever you wanted to achieve.

Snapshots are only messages because I had no choice (source delivers a MessageStream that restricts the type to "something that extends message"). Similarly, the ConsistencyMarker isn't simply a record ConsistentyMarker(long position) because that wouldn't fit in a MessageStream, so we make a "special" message for that as well just to make it fit.

The bound that we put on MessageStream is exactly what causes the proliferation of many message subtypes in AF4 that we were trying to avoid in AF5 (and we already have many there already as well).

@hjohn
hjohn force-pushed the feature/transformation-improvements branch from 9ae39bf to d59f48e Compare October 21, 2026 14:29
@hjohn
hjohn force-pushed the feature/transformation-improvements branch from d59f48e to d8a821b Compare October 21, 2026 14:30

@smcvb smcvb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am still on the fence on this change you are doing here, @hjohn.

I know you don't agree with the fact a snapshot can be regarded as a message. That doesn't mean the choice is inherently wrong. Furthermore, reiterating your desires to drop the generic on the MessageStream to abuse it for any kind of object also feels off to me. It's a dedicated solution to streams messages, so defining that you'd expect a Message implementation, what's wrong about that?

Added, you go on to state in the description of this issue, that the MessageStream doesn't support 1-to-1, which is equally false. That is exactly what the MessageStream.Single implementation provides and one of the core principles we wanted a MessageStream to support (0, 1, or N results).

Hence, I do not see why a SnapshotTransformation class couldn't be a MessageTransformation implementation of the SnapshotEventMessage only, ensuring a MessageStream.Single is returned instead.

* @since 5.2.0
*/
public interface MessageTransformation<M extends Message> {
public interface MessageTransformation {

@smcvb smcvb Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One of those LLM findings that triggers me too. We're doing breaking changes in public code:

Dropping the @Internal transform is fine, that's exactly the freedom the annotation gives us. Dropping the generic is something else though: MessageTransformation<M extends Message> has been public since 5.2.0 (it's in every tag up to 5.4.2), so this is a source-breaking change for anybody that typed against it. I dare to guess nobody is using it other than us, so I'm fine with it. But it should be stated explicitly in the PR description so it lands in the 5.5.0 release notes. (public surface)

import org.axonframework.messaging.core.MessageStream;
import org.axonframework.messaging.core.MessageType;
import org.axonframework.messaging.eventhandling.EventMessage;

@smcvb smcvb Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should drop the transform mention in the class-level JavaDoc if we go on with this.

@@ -273,7 +272,7 @@ void producedEventsKeepStreamOrderRelativeToSurroundingEvents() {
@Test
void splitEmitsItsReplacementsWhenInvokedDirectly() {
// SPI contract: a direct transform(...) call yields the ordered replacement events

@smcvb smcvb Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

More LLM findings this seemingly easy drive-by cause, which may hit users just as well:

"SPI contract" isn't the right wording anymore; transform(...) is package-private on the variant now, so there's no SPI left to speak of. You dropped the drop-sibling of this test with that exact reasoning. Does splitEmitsItsReplacementsWhenInvokedDirectly still earn its place next to the chain tests covering the ordering? If it stays, please reword the comment.

@@ -57,8 +57,7 @@ final class RenameEventTransformation implements EventTransformation {
* as a rename consults neither the converter nor the resolver

@smcvb smcvb Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we can drop the unused TransformationContext parameter now, right? Assuming we stick with the changes being made here.

@@ -160,19 +159,6 @@ void noPayloadConversionOrIdentityResolutionHappensForDroppedEvents() {
assertThat(outputs).isEmpty();
}

@smcvb smcvb Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change

@hjohn
hjohn force-pushed the feature/transformation-improvements branch from d8a821b to 60fed2a Compare November 24, 2026 19:46
@hjohn
hjohn force-pushed the feature/transformation-improvements branch from 60fed2a to cc66586 Compare November 24, 2026 20:04
@smcvb smcvb added Priority 1: Must Highest priority. A release cannot be made if this issue isn’t resolved. Type: Bug Use to signal issues that describe a bug within the system. and removed Priority 2: Should High priority. Ideally, these issues are part of the release they’re assigned to. Type: Enhancement Use to signal an issue enhances an already existing feature of the project. labels Dec 1, 2026
@smcvb

smcvb commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Although strictly an enhancement, marked as a bug since this functionality is currently blocking users migrating their snapshots from AF4 into AF5.

@smcvb smcvb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checking this PR again, after having reviewed #815 twice, I strongly doubt why we need this adjustment. Frankly, I am leaning towards closing this PR with the label won't fix.

It just introduces an unnecessary breaking change, which I only assume is pulled in because you disagree with the scope of the MessageStream.Entry to contain implementations of a Message only. I think I've already clarified that now is not the time to adjust AF5 APIs like that any further, so we can take that concern once we tackle AF6.

@hjohn

hjohn commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Checking this PR again, after having reviewed #815 twice, I strongly doubt why we need this adjustment. Frankly, I am leaning towards closing this PR with the label won't fix.

It just introduces an unnecessary breaking change, which I only assume is pulled in because you disagree with the scope of the MessageStream.Entry to contain implementations of a Message only. I think I've already clarified that now is not the time to adjust AF5 APIs like that any further, so we can take that concern once we tackle AF6.

We could limit the scope to just removing the two @Internal methods on the interfaces, that should be allowed I think. We're then no longer mixing two ways of doing things (the non-sealed polymorphic way and sealed way that doesn't need that).

As it is, we don't need this to be in for snapshot transformations, so I'm fine with either.

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

Labels

Priority 1: Must Highest priority. A release cannot be made if this issue isn’t resolved. Type: Bug Use to signal issues that describe a bug within the system.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants