Skip to content

Commit b68a1c0

Browse files
committed
Tweak PTV matchers to pass config/context
1 parent 01816ab commit b68a1c0

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

src/main/java/com/fasterxml/jackson/databind/jsontype/BasicPolymorphicTypeValidator.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ public class BasicPolymorphicTypeValidator
3838
* (base type or resolved subtype)
3939
*/
4040
public abstract static class TypeMatcher { // note: public since 2.11
41-
public abstract boolean match(Class<?> clazz);
41+
public abstract boolean match(MapperConfig<?> config, Class<?> clazz);
4242
}
4343

4444
/**
4545
* General matcher interface (predicate) for validating unresolved
4646
* subclass class name.
4747
*/
4848
public abstract static class NameMatcher { // note: public since 2.11
49-
public abstract boolean match(String clazzName);
49+
public abstract boolean match(MapperConfig<?> config, String clazzName);
5050
}
5151

5252
/*
@@ -104,7 +104,7 @@ protected Builder() { }
104104
public Builder allowIfBaseType(final Class<?> baseOfBase) {
105105
return _appendBaseMatcher(new TypeMatcher() {
106106
@Override
107-
public boolean match(Class<?> clazz) {
107+
public boolean match(MapperConfig<?> config, Class<?> clazz) {
108108
return baseOfBase.isAssignableFrom(clazz);
109109
}
110110
});
@@ -130,7 +130,7 @@ public boolean match(Class<?> clazz) {
130130
public Builder allowIfBaseType(final Pattern patternForBase) {
131131
return _appendBaseMatcher(new TypeMatcher() {
132132
@Override
133-
public boolean match(Class<?> clazz) {
133+
public boolean match(MapperConfig<?> config, Class<?> clazz) {
134134
return patternForBase.matcher(clazz.getName()).matches();
135135
}
136136
});
@@ -150,7 +150,7 @@ public boolean match(Class<?> clazz) {
150150
public Builder allowIfBaseType(final String prefixForBase) {
151151
return _appendBaseMatcher(new TypeMatcher() {
152152
@Override
153-
public boolean match(Class<?> clazz) {
153+
public boolean match(MapperConfig<?> config, Class<?> clazz) {
154154
return clazz.getName().startsWith(prefixForBase);
155155
}
156156
});
@@ -207,7 +207,7 @@ public Builder denyForExactBaseType(final Class<?> baseTypeToDeny) {
207207
public Builder allowIfSubType(final Class<?> subTypeBase) {
208208
return _appendSubClassMatcher(new TypeMatcher() {
209209
@Override
210-
public boolean match(Class<?> clazz) {
210+
public boolean match(MapperConfig<?> config, Class<?> clazz) {
211211
return subTypeBase.isAssignableFrom(clazz);
212212
}
213213
});
@@ -232,7 +232,7 @@ public boolean match(Class<?> clazz) {
232232
public Builder allowIfSubType(final Pattern patternForSubType) {
233233
return _appendSubNameMatcher(new NameMatcher() {
234234
@Override
235-
public boolean match(String clazzName) {
235+
public boolean match(MapperConfig<?> config, String clazzName) {
236236
return patternForSubType.matcher(clazzName).matches();
237237
}
238238
});
@@ -252,7 +252,7 @@ public boolean match(String clazzName) {
252252
public Builder allowIfSubType(final String prefixForSubType) {
253253
return _appendSubNameMatcher(new NameMatcher() {
254254
@Override
255-
public boolean match(String clazzName) {
255+
public boolean match(MapperConfig<?> config, String clazzName) {
256256
return clazzName.startsWith(prefixForSubType);
257257
}
258258
});
@@ -290,7 +290,7 @@ public Builder allowIfSubType(final TypeMatcher matcher) {
290290
public Builder allowIfSubTypeIsArray() {
291291
return _appendSubClassMatcher(new TypeMatcher() {
292292
@Override
293-
public boolean match(Class<?> clazz) {
293+
public boolean match(MapperConfig<?> config, Class<?> clazz) {
294294
return clazz.isArray();
295295
}
296296
});
@@ -320,7 +320,7 @@ public boolean match(Class<?> clazz) {
320320
public Builder allowSubTypesWithExplicitDeserializer() {
321321
return _appendSubClassMatcher(new TypeMatcher() {
322322
@Override
323-
public boolean match(Class<?> clazz) {
323+
public boolean match(MapperConfig<?> config, Class<?> clazz) {
324324
return clazz.isArray();
325325
}
326326
});
@@ -414,7 +414,7 @@ public Validity validateBaseType(MapperConfig<?> ctxt, JavaType baseType) {
414414
}
415415
if (_baseTypeMatchers != null) {
416416
for (TypeMatcher m : _baseTypeMatchers) {
417-
if (m.match(rawBase)) {
417+
if (m.match(ctxt, rawBase)) {
418418
return Validity.ALLOWED;
419419
}
420420
}
@@ -430,7 +430,7 @@ public Validity validateSubClassName(MapperConfig<?> ctxt, JavaType baseType,
430430
//System.err.println("validateSubClassName('"+subClassName+"')");
431431
if (_subTypeNameMatchers != null) {
432432
for (NameMatcher m : _subTypeNameMatchers) {
433-
if (m.match(subClassName)) {
433+
if (m.match(ctxt, subClassName)) {
434434
return Validity.ALLOWED;
435435
}
436436
}
@@ -447,7 +447,7 @@ public Validity validateSubType(MapperConfig<?> ctxt, JavaType baseType, JavaTyp
447447
if (_subClassMatchers != null) {
448448
final Class<?> subClass = subType.getRawClass();
449449
for (TypeMatcher m : _subClassMatchers) {
450-
if (m.match(subClass)) {
450+
if (m.match(ctxt, subClass)) {
451451
return Validity.ALLOWED;
452452
}
453453
}

src/test/java/com/fasterxml/jackson/databind/jsontype/vld/CustomPTVMatchersTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.databind.BaseMapTest;
66
import com.fasterxml.jackson.databind.ObjectMapper;
77
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
8+
import com.fasterxml.jackson.databind.cfg.MapperConfig;
89
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
910
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
1011
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
@@ -49,7 +50,7 @@ public void testCustomBaseMatchers() throws Exception
4950
PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder()
5051
.allowIfBaseType(new BasicPolymorphicTypeValidator.TypeMatcher() {
5152
@Override
52-
public boolean match(Class<?> base) {
53+
public boolean match(MapperConfig<?> ctxt, Class<?> base) {
5354
// Allow types within our packages
5455
return base.getName().startsWith("com.fasterxml.");
5556
}
@@ -81,7 +82,7 @@ public void testCustomSubtypeMatchers() throws Exception
8182
PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder()
8283
.allowIfSubType(new BasicPolymorphicTypeValidator.TypeMatcher() {
8384
@Override
84-
public boolean match(Class<?> clazz) {
85+
public boolean match(MapperConfig<?> ctxt, Class<?> clazz) {
8586
// Allow anything that looks "Good" :)
8687
return clazz.getSimpleName().endsWith("Good");
8788
}

0 commit comments

Comments
 (0)