Skip to content

Calling DefaultKafkaHeaderMapper constructor with the same ObjectMapper instance may result memory leak #2611

Closed
@sbozcan

Description

@sbozcan

In what version(s) of Spring for Apache Kafka are you seeing this issue?

2.8.2

Describe the bug

DefaultKafkaHeaderMapper is registering a SimpleModule in constructor to the given ObjectMapper instance. Since the SimpleModule's are unnamed modules, IGNORE_DUPLICATE_MODULE_REGISTRATIONS configuration of ObjectMapper doesn't handle multiple registration of this module. This is problematic and can cause a memory leak if the DefaultKafkaHeaderMapper(ObjectMapper mapper) constructor is called with the same ObjectMapper instance multiple times.

To Reproduce

Call the DefaultKafkaHeaderMapper(ObjectMapper mapper) function with same ObjectMapper instance multiple times.

Expected behavior

A new SimpleModule would be registered to the ObjectMapper beacuse of the new SimpleModule().addDeserializer(MimeType.class, new MimeTypeJsonDeserializer()) call.

Solution

This problematic situation can be avoided simply by giving a name to the SimpleModule created as below:

this.objectMapper.registerModule(new SimpleModule("mimeTypeJsonDeserializer", Version.unknownVersion())
						.addDeserializer(MimeType.class, new MimeTypeJsonDeserializer()));

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions