Custom JacksonSerializer builder problems inorder to customize the event payload type

Hello Axoniq,
We are trying to implement a Custom JacksonSerialize with a custom payload type.
We have the same consideration as imnewone in Customize event payload type for adapting the JacksonSerializer.

If we make our own ShorterJacksonSerializer we get into trouble with the builder of the JacksonSerializer in our AxonConfig.class
The ShorterJacksonSerializer.builder() will return the JacksonSerializer and not our own ShorterJacksonSerializer with the overridden methods.
How can we resolve this without completely writing our own serializer?

We are using Axon framework v4.9.4 with Spring boor 3.2.x

@Configuration
public class AxonConfig {
    @Bean
    @Primary
    public Serializer defaultSerializer() {

        ObjectMapper mapper = ShorterJacksonSerializer.defaultSerializer().getObjectMapper();

        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        mapper.activateDefaultTyping(
                mapper.getPolymorphicTypeValidator(),
                DefaultTyping.OBJECT_AND_NON_CONCRETE
        );

        return ShorterJacksonSerializer.builder()
                .objectMapper(mapper)
                .lenientDeserialization()
                .build();
    }
}



public class ShorterJacksonSerializer extends JacksonSerializer {

    String basePackage = "event";

    protected ShorterJacksonSerializer(Builder builder) {
        super(builder);
    }

    protected String resolveClassName(SerializedType serializedType) {
        return basePackage + "." + serializedType.getName();
    }

    @Override
    public SerializedType typeForClass(Class type) {
        if (type.getName().startsWith(basePackage)) {
            String shortName = "%s.%s".formatted(basePackage, type.getSimpleName());
            return new SimpleSerializedType(shortName, getRevisionResolver().revisionOf(type));
        } else {
            return new SimpleSerializedType(type.getSimpleName(), this.getRevisionResolver().revisionOf(type));
        }
    }
}

That’s because ShorterJacksonSerializer doesn’t define a builder() method, so you’re using the one defined in the subclass. You can specify one that returns a Builder specific to your serializer implementation. Make sure also to override all the methods in the builder, so that your builder type is returned, and not the builder of the JacksonSerializer.

Instead of extending the JacksonSerializer, consider wrapping it (composition over inheritance). Remove the prefix from the SerializedType in the SerializedObject you get back from Jackson. And add it again when you delegate the serialize call.