Skip to content

Consider using Serializer<?> for the delegates of DelegatingByTypeSerializer #2140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
v-chernyshev opened this issue Feb 25, 2022 · 0 comments · Fixed by #2143
Closed

Consider using Serializer<?> for the delegates of DelegatingByTypeSerializer #2140

v-chernyshev opened this issue Feb 25, 2022 · 0 comments · Fixed by #2143
Assignees
Milestone

Comments

@v-chernyshev
Copy link

Expected Behavior

I've tried using the newly added assignable types support and, initially, wrote the following code:

final var delegates = new LinkedHashMap<Class<?>, Serializer<?>>();
delegates.put(Message.class, new ProtobufMessageSerializer());
delegates.put(byte[].class, new ByteArraySerializer());

return new DefaultKafkaProducerFactory<>(
        this.springProperties.buildProducerProperties(),
        new VoidSerializer(),
        new DelegatingByTypeSerializer(delegates, true));

Where ProtobufMessageSerializer is a custom type that can handle any protobuf message. Unfortunately, the code does not compile because the constructor of DelegatingByTypeSerializer and the internal member variables all expect to see the raw Serializer type. It is, of course, possible to tweak the code to:

final var delegates = new LinkedHashMap<Class<?>, Serializer>();

But then a warning is raised to highlight the usage of raw types. Generally we treat this warning type as an error.

Current Behavior

I am basically forced to do this instead to silence the warning:

@SuppressWarnings("rawtypes")
final var delegates = new LinkedHashMap<Class<?>, Serializer>();

Context

Our project uses the retryable topics feature, @SendTo and the ErrorHandlingDeserializer class to intercept potential poison pill messages. They are, however, extremely unlikely to happen, so I'd like to make sure that the first delegate that's checked at runtime is the base protobuf Message class. As far as I know, the only way to guarantee the iteration order is to explicitly populate a LinkedHashMap, but creating such a map requires warning suppression.

@garyrussell garyrussell added this to the 3.0.0-M3 milestone Mar 1, 2022
@garyrussell garyrussell removed this from the 3.0.0-M3 milestone Mar 1, 2022
artembilan added a commit to artembilan/spring-kafka that referenced this issue Mar 1, 2022
Fixes spring-projects#2140

Improve some internal code around generics in the `DelegatingByTypeSerializer`.
This allows us to use a dedicated external `Map` variable for the delegates
@artembilan artembilan self-assigned this Mar 1, 2022
@artembilan artembilan added this to the 3.0.0-M3 milestone Mar 1, 2022
garyrussell pushed a commit that referenced this issue Mar 1, 2022
Fixes #2140

Improve some internal code around generics in the `DelegatingByTypeSerializer`.
This allows us to use a dedicated external `Map` variable for the delegates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants