Skip to content

Commit 295d9e4

Browse files
committed
Minor improvement wrt #546: prevent attempts to use JsonTypeInfo.As.EXTERNAL_PROPERTY for default typing (throw IllegalArgumentException if attempted)
1 parent e1b2009 commit 295d9e4

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,12 +1073,23 @@ public ObjectMapper enableDefaultTyping(DefaultTyping dti) {
10731073
* Method for enabling automatic inclusion of type information, needed
10741074
* for proper deserialization of polymorphic types (unless types
10751075
* have been annotated with {@link com.fasterxml.jackson.annotation.JsonTypeInfo}).
1076+
*<P>
1077+
* NOTE: use of {@link JsonTypeInfo.As#EXTERNAL_PROPERTY} is <b>NOT SUPPORTED</b>;
1078+
* and attempts of do so will throw an {@link IllegalArgumentException} to make
1079+
* this limitation explicit.
10761080
*
10771081
* @param applicability Defines kinds of types for which additional type information
10781082
* is added; see {@link DefaultTyping} for more information.
10791083
*/
10801084
public ObjectMapper enableDefaultTyping(DefaultTyping applicability, JsonTypeInfo.As includeAs)
10811085
{
1086+
/* 18-Sep-2014, tatu: Let's add explicit check to ensure no one tries to
1087+
* use "As.EXTERNAL_PROPERTY", since that will not work.
1088+
*/
1089+
if (includeAs == JsonTypeInfo.As.EXTERNAL_PROPERTY) {
1090+
throw new IllegalArgumentException("Can not use includeAs of "+includeAs);
1091+
}
1092+
10821093
TypeResolverBuilder<?> typer = new DefaultTypeResolverBuilder(applicability);
10831094
// we'll always use full class name, when using defaulting
10841095
typer = typer.init(JsonTypeInfo.Id.CLASS, null);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,18 @@ public void testFeature432() throws Exception
349349
String json = mapper.writeValueAsString(new BeanHolder(new StringBean("punny")));
350350
assertEquals("{\"bean\":{\"*CLASS*\":\"com.fasterxml.jackson.databind.jsontype.TestDefaultForObject$StringBean\",\"name\":\"punny\"}}", json);
351351
}
352+
353+
public void testNoGoWithExternalProperty() throws Exception
354+
{
355+
ObjectMapper mapper = new ObjectMapper();
356+
try {
357+
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT,
358+
JsonTypeInfo.As.EXTERNAL_PROPERTY);
359+
fail("Should not have passed");
360+
} catch (IllegalArgumentException e) {
361+
verifyException(e, "Can not use includeAs of EXTERNAL_PROPERTY");
362+
}
363+
}
352364

353365
/*
354366
/**********************************************************

0 commit comments

Comments
 (0)