Skip to content

Commit cf6a785

Browse files
garyrussellartembilan
authored andcommitted
GH-915: Fix JSON default object mapper config
Fixes #915 The configuration options were added after the reader was created. * Add test.
1 parent 7c512c9 commit cf6a785

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

spring-kafka/src/main/java/org/springframework/kafka/support/serializer/JsonDeserializer.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2018 the original author or authors.
2+
* Copyright 2015-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.util.Arrays;
2121
import java.util.Map;
22+
import java.util.function.Consumer;
2223

2324
import org.apache.kafka.common.errors.SerializationException;
2425
import org.apache.kafka.common.header.Headers;
@@ -144,9 +145,10 @@ public JsonDeserializer(Class<T> targetType) {
144145
* @since 2.2
145146
*/
146147
public JsonDeserializer(Class<T> targetType, boolean useHeadersIfPresent) {
147-
this(targetType, new ObjectMapper(), useHeadersIfPresent);
148-
this.objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
149-
this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
148+
this(targetType, new ObjectMapper(), useHeadersIfPresent, om -> {
149+
om.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
150+
om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
151+
});
150152
}
151153

152154
/**
@@ -167,8 +169,14 @@ public JsonDeserializer(Class<T> targetType, ObjectMapper objectMapper) {
167169
* type if not.
168170
* @since 2.2
169171
*/
170-
@SuppressWarnings("unchecked")
171172
public JsonDeserializer(@Nullable Class<T> targetType, ObjectMapper objectMapper, boolean useHeadersIfPresent) {
173+
this(targetType, objectMapper, useHeadersIfPresent, om -> { });
174+
}
175+
176+
@SuppressWarnings("unchecked")
177+
private JsonDeserializer(@Nullable Class<T> targetType, ObjectMapper objectMapper, boolean useHeadersIfPresent,
178+
Consumer<ObjectMapper> configurer) {
179+
172180
Assert.notNull(objectMapper, "'objectMapper' must not be null.");
173181
this.objectMapper = objectMapper;
174182
this.targetType = targetType;
@@ -178,6 +186,7 @@ public JsonDeserializer(@Nullable Class<T> targetType, ObjectMapper objectMapper
178186
Assert.isTrue(this.targetType != null || useHeadersIfPresent,
179187
"'targetType' cannot be null if 'useHeadersIfPresent' is false");
180188

189+
configurer.accept(this.objectMapper);
181190
if (this.targetType != null) {
182191
this.reader = this.objectMapper.readerFor(this.targetType);
183192
}

spring-kafka/src/test/java/org/springframework/kafka/support/serializer/JsonSerializationTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -172,6 +172,14 @@ public void testDeserializedJsonNullEqualsNull() {
172172
assertThat(jsonReader.deserialize(topic, null)).isEqualTo(null);
173173
}
174174

175+
@Test
176+
public void testExtraFieldIgnored() {
177+
JsonDeserializer<DummyEntity> deser = new JsonDeserializer<>(DummyEntity.class);
178+
assertThat(deser.deserialize(topic, "{\"intValue\":1,\"extra\":2}".getBytes()))
179+
.isInstanceOf(DummyEntity.class);
180+
deser.close();
181+
}
182+
175183
static class DummyEntityJsonDeserializer extends JsonDeserializer<DummyEntity> {
176184

177185
}

0 commit comments

Comments
 (0)