Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@
import org.springframework.core.ResolvableType;
import org.springframework.kafka.support.serializer.JacksonJsonSerde;

import tools.jackson.databind.json.JsonMapper;

/**
* Utility class that contains various methods to help resolve {@link Serde Serdes}.
*
* @author Chris Bono
* @author adityaanikam
* @since 4.0
*/
abstract class SerdeResolverUtils {
Expand Down Expand Up @@ -102,7 +105,8 @@ static Serde<?> resolveForType(ConfigurableApplicationContext context, Resolvabl

// Use JsonSerde if type is not exactly Object
if (!genericRawClazz.isAssignableFrom((Object.class))) {
return new JacksonJsonSerde<>(genericRawClazz);
JsonMapper jsonMapper = context.getBeanProvider(JsonMapper.class).getIfUnique();
return jsonMapper != null ? new JacksonJsonSerde<>(genericRawClazz, jsonMapper) : new JacksonJsonSerde<>(genericRawClazz);
}

// Finally, just resort to using the fallback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.cloud.stream.binder.kafka.streams;

import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.UUID;
Expand All @@ -40,6 +41,9 @@
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.ResolvableType;
import org.springframework.kafka.support.serializer.JacksonJsonSerde;
import org.springframework.kafka.support.serializer.JacksonJsonSerializer;

import tools.jackson.databind.json.JsonMapper;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;
Expand All @@ -49,6 +53,7 @@
* Unit tests for {@link SerdeResolverUtils}.
*
* @author Chris Bono
* @author adityaanikam
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
class SerdeResolverUtilsTests {
Expand Down Expand Up @@ -157,6 +162,38 @@ void returnsFallbackSerdeForJavaLangObject() {
.isNull());
}
}

@Nested
class WithJsonMapperBean {

@Test
void usesConfiguredJsonMapperBeanWhenPresent() throws Exception {
new ApplicationContextRunner()
.withPropertyValues("spring.cloud.function.ineligible-definitions: sendToDlqAndContinue")
.withUserConfiguration(SerdeResolverJsonMapperTestApp.class)
.run((context) -> {
Serde<?> serde = SerdeResolverUtils.resolveForType(context, ResolvableType.forClass(Foo.class), null);
assertThat(serde).isInstanceOf(JacksonJsonSerde.class);

JsonMapper expectedMapper = context.getBean(JsonMapper.class);
JacksonJsonSerializer<?> serializer = (JacksonJsonSerializer<?>) serde.serializer();
Field mapperField = JacksonJsonSerializer.class.getDeclaredField("jsonMapper");
mapperField.setAccessible(true);
assertThat(mapperField.get(serializer)).isSameAs(expectedMapper);
});
}

@EnableAutoConfiguration
static class SerdeResolverJsonMapperTestApp {

@Bean
public JsonMapper customJsonMapper() {
return JsonMapper.builder().build();
}

}

}
}
}

Expand Down