mirror of https://github.com/jwtk/jjwt.git
Allow JacksonDeserializer to work with Java 9+ Map.of
and other maps that do NOT allow null keys
This commit is contained in:
parent
09637f1e66
commit
e115085b14
|
@ -1,5 +1,11 @@
|
||||||
## Release Notes
|
## Release Notes
|
||||||
|
|
||||||
|
### 0.11.1
|
||||||
|
|
||||||
|
This patch release:
|
||||||
|
|
||||||
|
* Fixes issue when using Java 9+ `Map.of` with JacksonDeserializer which resulted in an NullPointerException
|
||||||
|
|
||||||
### 0.11.0
|
### 0.11.0
|
||||||
|
|
||||||
This minor release:
|
This minor release:
|
||||||
|
|
|
@ -120,8 +120,9 @@ public class JacksonDeserializer<T> implements Deserializer<T> {
|
||||||
@Override
|
@Override
|
||||||
public Object deserialize(JsonParser parser, DeserializationContext context) throws IOException {
|
public Object deserialize(JsonParser parser, DeserializationContext context) throws IOException {
|
||||||
// check if the current claim key is mapped, if so traverse it's value
|
// check if the current claim key is mapped, if so traverse it's value
|
||||||
if (claimTypeMap != null && claimTypeMap.containsKey(parser.currentName())) {
|
String name = parser.currentName();
|
||||||
Class type = claimTypeMap.get(parser.currentName());
|
if (claimTypeMap != null && name != null && claimTypeMap.containsKey(name)) {
|
||||||
|
Class type = claimTypeMap.get(name);
|
||||||
return parser.readValueAsTree().traverse(parser.getCodec()).readValueAs(type);
|
return parser.readValueAsTree().traverse(parser.getCodec()).readValueAs(type);
|
||||||
}
|
}
|
||||||
// otherwise default to super
|
// otherwise default to super
|
||||||
|
|
|
@ -105,6 +105,41 @@ class JacksonDeserializerTest {
|
||||||
assertEquals expected, result
|
assertEquals expected, result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For: https://github.com/jwtk/jjwt/issues/564
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testMappedTypeDeserializerWithMapNullCheck() {
|
||||||
|
|
||||||
|
// mimic map implementations that do NOT allow for null keys, or containsKey(null)
|
||||||
|
Map typeMap = new HashMap() {
|
||||||
|
@Override
|
||||||
|
boolean containsKey(Object key) {
|
||||||
|
if (key == null) {
|
||||||
|
throw new NullPointerException("key is null, expected for this test")
|
||||||
|
}
|
||||||
|
return super.containsKey(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: the following does NOT work with Java 1.7
|
||||||
|
// when we stop supporting that version we can use a partial mock instead
|
||||||
|
// the `typeMap.put("custom", CustomBean)` line below results in an NPE, (only on 1.7)
|
||||||
|
|
||||||
|
// Map typeMap = partialMockBuilder(HashMap)
|
||||||
|
// .addMockedMethod("containsKey")
|
||||||
|
// .createNiceMock()
|
||||||
|
//
|
||||||
|
// expect(typeMap.containsKey(null)).andThrow(new NullPointerException("key is null, expected for this test"))
|
||||||
|
// replay(typeMap)
|
||||||
|
|
||||||
|
typeMap.put("custom", CustomBean)
|
||||||
|
|
||||||
|
def deserializer = new JacksonDeserializer(typeMap)
|
||||||
|
def result = deserializer.deserialize('{"alg":"HS256"}'.getBytes("UTF-8"))
|
||||||
|
assertEquals(["alg": "HS256"], result)
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalArgumentException)
|
@Test(expected = IllegalArgumentException)
|
||||||
void testNullClaimTypeMap() {
|
void testNullClaimTypeMap() {
|
||||||
new JacksonDeserializer((Map) null)
|
new JacksonDeserializer((Map) null)
|
||||||
|
|
Loading…
Reference in New Issue