Skip to content

Commit 1ea2452

Browse files
committed
Fixed FasterXML#2840 (wasn't using passed-in PolymorphicTypeValidator)
1 parent ab43beb commit 1ea2452

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

release-notes/CREDITS-2.x

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,3 +1144,7 @@ Joshua Shannon (retrodaredevil@github)
11441144
Daniel Hrabovcak (TheSpiritXIII@github)
11451145
* Reported #2796: `TypeFactory.constructType()` does not take `TypeBindings` correctly
11461146
(2.11.2)
1147+
1148+
Daniel Wu (DanielYWoo@github)
1149+
* Reported #2840: `ObjectMapper.activateDefaultTypingAsProperty()` is not using
1150+
(2.11.3)

release-notes/VERSION-2.x

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Project: jackson-databind
44
=== Releases ===
55
------------------------------------------------------------------------
66

7+
2.11.3 (not yet released)
8+
9+
#2840: `ObjectMapper.activateDefaultTypingAsProperty()` is not using
10+
parameter `PolymorphicTypeValidator`
11+
(reported by Daniel W)
12+
713
2.11.2 (02-Aug-2020)
814

915
#2783: Parser/Generator features not set when using `ObjectMapper.createParser()`,

src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,8 +1962,7 @@ public ObjectMapper activateDefaultTypingAsProperty(PolymorphicTypeValidator ptv
19621962
DefaultTyping applicability, String propertyName)
19631963
{
19641964
TypeResolverBuilder<?> typer = _constructDefaultTypeResolverBuilder(applicability,
1965-
getPolymorphicTypeValidator());
1966-
1965+
ptv);
19671966
// we'll always use full class name, when using defaulting
19681967
typer = typer.init(JsonTypeInfo.Id.CLASS, null);
19691968
typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);

src/test/java/com/fasterxml/jackson/databind/jsontype/deftyping/TestDefaultForObject.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
import java.util.*;
44

5-
65
import com.fasterxml.jackson.core.*;
6+
77
import com.fasterxml.jackson.annotation.JsonTypeInfo;
8+
89
import com.fasterxml.jackson.databind.*;
10+
import com.fasterxml.jackson.databind.cfg.MapperConfig;
11+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
912
import com.fasterxml.jackson.databind.json.JsonMapper;
13+
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
1014
import com.fasterxml.jackson.databind.testutil.NoCheckSubTypeValidator;
1115
import com.fasterxml.jackson.databind.util.TokenBuffer;
1216

@@ -78,11 +82,20 @@ static class DiscussBean extends DomainBean {
7882
public String subject;
7983
}
8084

81-
static public class DomainBeanWrapper {
85+
static class DomainBeanWrapper {
8286
public String name;
8387
public Object myBean;
8488
}
8589

90+
@SuppressWarnings("serial")
91+
static class BlockAllPTV extends PolymorphicTypeValidator.Base
92+
{
93+
@Override
94+
public Validity validateBaseType(MapperConfig<?> config, JavaType baseType) {
95+
return Validity.DENIED;
96+
}
97+
}
98+
8699
/*
87100
/**********************************************************
88101
/* Unit tests
@@ -130,6 +143,23 @@ public void testBeanAsObjectUsingAsProperty() throws Exception
130143
assertEquals(StringBean.class, result.getClass());
131144
assertEquals("abc", ((StringBean) result).name);
132145
}
146+
147+
// [databind#2840]: ensure "as-property" uses PTV passed
148+
public void testAsPropertyWithPTV() throws Exception {
149+
ObjectMapper m = JsonMapper.builder()
150+
.activateDefaultTypingAsProperty(new BlockAllPTV(),
151+
ObjectMapper.DefaultTyping.NON_FINAL,
152+
"@classy")
153+
.build();
154+
String json = m.writeValueAsString(new StringBean("abc"));
155+
try {
156+
/*Object result =*/ m.readValue(json, Object.class);
157+
fail("Should not pass");
158+
} catch (InvalidDefinitionException e) {
159+
verifyException(e, "Configured `PolymorphicTypeValidator`");
160+
verifyException(e, "denied resolution of all subtypes of ");
161+
}
162+
}
133163

134164
/**
135165
* Unit test that verifies that an abstract bean is stored with type information

0 commit comments

Comments
 (0)