Skip to content

Commit 554167c

Browse files
committed
Change visibility of PTV Matchers to allow use of custom matchers
1 parent bc56dd8 commit 554167c

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

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

+37-2
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ public class BasicPolymorphicTypeValidator
3737
* General matcher interface (predicate) for validating class values
3838
* (base type or resolved subtype)
3939
*/
40-
protected abstract static class TypeMatcher {
40+
public abstract static class TypeMatcher { // note: public since 2.11
4141
public abstract boolean match(Class<?> clazz);
4242
}
4343

4444
/**
4545
* General matcher interface (predicate) for validating unresolved
4646
* subclass class name.
4747
*/
48-
protected abstract static class NameMatcher {
48+
public abstract static class NameMatcher { // note: public since 2.11
4949
public abstract boolean match(String clazzName);
5050
}
5151

@@ -64,6 +64,11 @@ protected abstract static class NameMatcher {
6464
* rules are checked.
6565
*/
6666
public static class Builder {
67+
/**
68+
* Optional set of base types (exact match) that are NOT accepted
69+
* as base types for polymorphic properties. May be used to prevent "unsafe"
70+
* base types like {@link java.lang.Object} or {@link java.io.Serializable}.
71+
*/
6772
protected Set<Class<?>> _invalidBaseTypes;
6873

6974
/**
@@ -151,6 +156,21 @@ public boolean match(Class<?> clazz) {
151156
});
152157
}
153158

159+
/**
160+
* Method for appending custom matcher called with base type: if matcher returns
161+
* {@code true}, all possible subtypes will be accepted; if {@code false}, other
162+
* matchers are applied.
163+
*
164+
* @param matcher Custom matcher to apply to base type
165+
*
166+
* @return This Builder to allow call chaining
167+
*
168+
* @since 2.11
169+
*/
170+
public Builder allowIfBaseType(final TypeMatcher matcher) {
171+
return _appendBaseMatcher(matcher);
172+
}
173+
154174
/**
155175
* Method for appending matcher that will mark any polymorphic properties with exact
156176
* specific class to be invalid.
@@ -238,6 +258,21 @@ public boolean match(String clazzName) {
238258
});
239259
}
240260

261+
/**
262+
* Method for appending custom matcher called with resolved subtype: if matcher returns
263+
* {@code true}, type will be accepted; if {@code false}, other
264+
* matchers are applied.
265+
*
266+
* @param matcher Custom matcher to apply to resolved subtype
267+
*
268+
* @return This Builder to allow call chaining
269+
*
270+
* @since 2.11
271+
*/
272+
public Builder allowIfSubType(final TypeMatcher matcher) {
273+
return _appendSubClassMatcher(matcher);
274+
}
275+
241276
/**
242277
* Method for appending matcher that will allow all subtypes that are Java arrays
243278
* (regardless of element type). Note that this does NOT validate element type
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fasterxml.jackson.databind.jsontype.vld;
2+
3+
import com.fasterxml.jackson.databind.BaseMapTest;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
6+
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
7+
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
8+
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
9+
import com.fasterxml.jackson.databind.jsontype.vld.BasicPTVWithArraysTest.Base2534;
10+
import com.fasterxml.jackson.databind.jsontype.vld.BasicPTVWithArraysTest.Good2534;
11+
import com.fasterxml.jackson.databind.jsontype.vld.BasicPTVWithArraysTest.ObjectWrapper;
12+
13+
public class CustomPTVMatchersTest extends BaseMapTest
14+
{
15+
static abstract class CustomBase {
16+
public int x = 3;
17+
}
18+
19+
static class CustomGood extends CustomBase {
20+
protected CustomGood() { }
21+
public CustomGood(int x) {
22+
super();
23+
this.x = x;
24+
}
25+
}
26+
27+
static class CustomBad extends CustomBase {
28+
protected CustomBad() { }
29+
public CustomBad(int x) {
30+
super();
31+
this.x = x;
32+
}
33+
}
34+
35+
static final class ObjectWrapper {
36+
public Object value;
37+
38+
protected ObjectWrapper() { }
39+
public ObjectWrapper(Object v) { value = v; }
40+
}
41+
42+
/*
43+
/**********************************************************************
44+
/* Test methods
45+
/**********************************************************************
46+
*/
47+
48+
public void testCustomBaseMatchers() throws Exception
49+
{
50+
// TO BE COMPLETED
51+
}
52+
}

0 commit comments

Comments
 (0)