Skip to content

Commit fd619e0

Browse files
authored
disable FAIL_ON_UNKNOWN_PROPERTIES deserialization feature of Jackson by default (#896)
1 parent 0763191 commit fd619e0

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

extensions/jackson/src/main/java/io/jsonwebtoken/jackson/io/JacksonSerializer.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.fasterxml.jackson.core.JsonGenerator;
1919
import com.fasterxml.jackson.core.JsonParser;
20+
import com.fasterxml.jackson.databind.DeserializationFeature;
2021
import com.fasterxml.jackson.databind.Module;
2122
import com.fasterxml.jackson.databind.ObjectMapper;
2223
import com.fasterxml.jackson.databind.ObjectWriter;
@@ -46,17 +47,21 @@ public class JacksonSerializer<T> extends AbstractSerializer<T> {
4647

4748
/**
4849
* Creates and returns a new ObjectMapper with the {@code jjwt-jackson} module registered and
49-
* {@code JsonParser.Feature.STRICT_DUPLICATE_DETECTION} enabled (set to true).
50+
* {@code JsonParser.Feature.STRICT_DUPLICATE_DETECTION} enabled (set to true) and
51+
* {@code DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES} disabled (set to false).
52+
*
53+
* @return a new ObjectMapper with the {@code jjwt-jackson} module registered and
54+
* {@code JsonParser.Feature.STRICT_DUPLICATE_DETECTION} enabled (set to true) and
55+
* {@code DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES} disabled (set to false).
5056
*
51-
* @return and returns a new ObjectMapper with the {@code jjwt-jackson} module registered and
52-
* {@code JsonParser.Feature.STRICT_DUPLICATE_DETECTION} enabled (set to true).
5357
* @since 0.12.4
5458
*/
5559
// package protected on purpose, do not expose to the public API
5660
static ObjectMapper newObjectMapper() {
5761
return new ObjectMapper()
5862
.registerModule(MODULE)
59-
.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION, true); // https://github.com/jwtk/jjwt/issues/877
63+
.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION, true) // https://github.com/jwtk/jjwt/issues/877
64+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // https://github.com/jwtk/jjwt/issues/893
6065
}
6166

6267
protected final ObjectMapper objectMapper;

extensions/jackson/src/test/groovy/io/jsonwebtoken/jackson/io/JacksonDeserializerTest.groovy

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,64 @@ class JacksonDeserializerTest {
146146
}
147147
}
148148

149+
/**
150+
* Asserts https://github.com/jwtk/jjwt/issues/893
151+
*/
152+
@Test
153+
void testIgnoreUnknownPropertiesWhenDeserializeWithCustomObject() {
154+
155+
long currentTime = System.currentTimeMillis()
156+
157+
String json = """
158+
{
159+
"oneKey":"oneValue",
160+
"custom": {
161+
"stringValue": "s-value",
162+
"intValue": "11",
163+
"dateValue": ${currentTime},
164+
"shortValue": 22,
165+
"longValue": 33,
166+
"byteValue": 15,
167+
"byteArrayValue": "${base64('bytes')}",
168+
"unknown": "unknown",
169+
"nestedValue": {
170+
"stringValue": "nested-value",
171+
"intValue": "111",
172+
"dateValue": ${currentTime + 1},
173+
"shortValue": 222,
174+
"longValue": 333,
175+
"byteValue": 10,
176+
"byteArrayValue": "${base64('bytes2')}",
177+
"unknown": "unknown"
178+
}
179+
}
180+
}
181+
"""
182+
183+
CustomBean expectedCustomBean = new CustomBean()
184+
.setByteArrayValue("bytes".getBytes("UTF-8"))
185+
.setByteValue(0xF as byte)
186+
.setDateValue(new Date(currentTime))
187+
.setIntValue(11)
188+
.setShortValue(22 as short)
189+
.setLongValue(33L)
190+
.setStringValue("s-value")
191+
.setNestedValue(new CustomBean()
192+
.setByteArrayValue("bytes2".getBytes("UTF-8"))
193+
.setByteValue(0xA as byte)
194+
.setDateValue(new Date(currentTime + 1))
195+
.setIntValue(111)
196+
.setShortValue(222 as short)
197+
.setLongValue(333L)
198+
.setStringValue("nested-value")
199+
)
200+
201+
def expected = [oneKey: "oneValue", custom: expectedCustomBean]
202+
def result = new JacksonDeserializer(Maps.of("custom", CustomBean).build())
203+
.deserialize(new StringReader(json))
204+
assertEquals expected, result
205+
}
206+
149207
/**
150208
* For: https://github.com/jwtk/jjwt/issues/564
151209
*/

0 commit comments

Comments
 (0)