Skip to content

Commit 94b7cba

Browse files
committed
Fixed #2796 (second part of fix to apply TypeModifiers on TypeFactory.constructParametricType()
1 parent 925c7c1 commit 94b7cba

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,10 @@ public JavaType constructParametricType(Class<?> parametrized, Class<?>... param
10491049
*/
10501050
public JavaType constructParametricType(Class<?> rawType, JavaType... parameterTypes)
10511051
{
1052-
return _fromClass(null, rawType, TypeBindings.create(rawType, parameterTypes));
1052+
// 16-Jul-2020, tatu: Since we do not call `_fromAny()`, need to make
1053+
// sure `TypeModifier`s are applied:
1054+
JavaType resultType = _fromClass(null, rawType, TypeBindings.create(rawType, parameterTypes));
1055+
return _applyModifiers(rawType, resultType);
10531056
}
10541057

10551058
/**

src/test/java/com/fasterxml/jackson/databind/module/TestTypeModifiers.java

+23-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.core.*;
77
import com.fasterxml.jackson.databind.*;
88
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
9+
import com.fasterxml.jackson.databind.json.JsonMapper;
910
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
1011
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
1112
import com.fasterxml.jackson.databind.module.SimpleDeserializers;
@@ -205,22 +206,23 @@ public JavaType modifyType(JavaType type, Type jdkType, TypeBindings bindings, T
205206
return type;
206207
}
207208
}
208-
209+
209210
/*
210211
/**********************************************************
211212
/* Unit tests
212213
/**********************************************************
213214
*/
214215

216+
private final ObjectMapper MAPPER_WITH_MODIFIER = JsonMapper.builder()
217+
.typeFactory(TypeFactory.defaultInstance().withModifier(new MyTypeModifier()))
218+
.build();
219+
215220
/**
216221
* Basic test for ensuring that we can get "xxx-like" types recognized.
217222
*/
218223
public void testMapLikeTypeConstruction() throws Exception
219224
{
220-
ObjectMapper mapper = new ObjectMapper();
221-
mapper.setTypeFactory(mapper.getTypeFactory().withModifier(new MyTypeModifier()));
222-
223-
JavaType type = mapper.constructType(MyMapLikeType.class);
225+
JavaType type = MAPPER_WITH_MODIFIER.constructType(MyMapLikeType.class);
224226
assertTrue(type.isMapLikeType());
225227
// also, must have resolved type info
226228
JavaType param = ((MapLikeType) type).getKeyType();
@@ -231,6 +233,21 @@ public void testMapLikeTypeConstruction() throws Exception
231233
assertSame(Integer.class, param.getRawClass());
232234
}
233235

236+
public void testMapLikeTypeViaParametric() throws Exception
237+
{
238+
// [databind#2796]: should refine with another call too
239+
JavaType type = MAPPER_WITH_MODIFIER.getTypeFactory().constructParametricType(MapMarker.class,
240+
new Class<?>[] { String.class, Double.class });
241+
assertTrue(type.isMapLikeType());
242+
JavaType param = ((MapLikeType) type).getKeyType();
243+
assertNotNull(param);
244+
assertSame(String.class, param.getRawClass());
245+
246+
param = ((MapLikeType) type).getContentType();
247+
assertNotNull(param);
248+
assertSame(Double.class, param.getRawClass());
249+
}
250+
234251
// [databind#2395] Can trigger problem this way too
235252
// NOTE: oddly enough, seems to ONLY fail
236253
public void testTypeResolutionForRecursive() throws Exception
@@ -247,10 +264,7 @@ public void setupModule(SetupContext context) {
247264

248265
public void testCollectionLikeTypeConstruction() throws Exception
249266
{
250-
ObjectMapper mapper = new ObjectMapper();
251-
mapper.setTypeFactory(mapper.getTypeFactory().withModifier(new MyTypeModifier()));
252-
253-
JavaType type = mapper.constructType(MyCollectionLikeType.class);
267+
JavaType type = MAPPER_WITH_MODIFIER.constructType(MyCollectionLikeType.class);
254268
assertTrue(type.isCollectionLikeType());
255269
JavaType param = ((CollectionLikeType) type).getContentType();
256270
assertNotNull(param);

src/test/java/com/fasterxml/jackson/databind/type/TestTypeFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
public class TestTypeFactory
1515
extends BaseMapTest
16-
{
16+
{
1717
/*
1818
/**********************************************************
1919
/* Helper types

0 commit comments

Comments
 (0)