Skip to content

Commit 9428f11

Browse files
authored
Fix part of #4160: remove DefaultTyping.EVERYTHING from 3.0 (#4174)
1 parent 71c6218 commit 9428f11

File tree

8 files changed

+32
-47
lines changed

8 files changed

+32
-47
lines changed

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ Versions: 3.x (for earlier see VERSION-2.x)
5555
#3536: Create new exception type `JsonNodeException` for use by `JsonNode`-related problems
5656
#3542: Rename "com.fasterxml.jackson" -> "tools.jackson"
5757
#3601: Change `Optional` deserialization from "absent" value into `null`, from "empty"
58+
$4160: Deprecate `DefaultTyping.EVERYTHING` in `2.x` and remove in `3.0`
5859
- Remove `MappingJsonFactory`
5960
- Add context parameter for `TypeSerializer` contextualization (`forProperty()`)

src/main/java/tools/jackson/databind/DefaultTyping.java

+2-23
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,7 @@ public enum DefaultTyping {
4747
/**
4848
* Enables default typing for non-final types as {@link #NON_FINAL},
4949
* but also includes Enums.
50-
* Designed to allow default typing of Enums without resorting to
51-
* {@link #EVERYTHING}, which has security implications.
5250
*/
53-
NON_FINAL_AND_ENUMS,
54-
55-
/**
56-
* Value that means that default typing will be used for
57-
* all types, with exception of small number of
58-
* "natural" types (String, Boolean, Integer, Double) that
59-
* can be correctly inferred from JSON, and primitives (which
60-
* can not be polymorphic either). Typing is also enabled for
61-
* all array types.
62-
*<p>
63-
* WARNING: most of the time this is <b>NOT</b> the setting you want
64-
* as it tends to add Type Ids everywhere, even in cases
65-
* where type can not be anything other than declared (for example
66-
* if declared value type of a property is {@code final} -- for example,
67-
* properties of type {@code long} (or wrapper {@code Long}).
68-
*<p>
69-
* Note that the only known use case for this setting is for serialization
70-
* when passing instances of final class, and base type is not
71-
* separately specified.
72-
*/
73-
EVERYTHING;
51+
NON_FINAL_AND_ENUMS
52+
;
7453
}

src/main/java/tools/jackson/databind/jsontype/impl/DefaultTypeResolverBuilder.java

-5
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,6 @@ public boolean useForType(JavaType t)
157157
// [databind#3569] Allow use of default typing for Enums
158158
|| t.isEnumType();
159159

160-
case EVERYTHING:
161-
// So, excluding primitives (handled earlier) and "Natural types" (handled
162-
// before this method is called), applied to everything
163-
return true;
164-
165160
default:
166161
case JAVA_LANG_OBJECT:
167162
return t.isJavaLangObject();

src/test-jdk11/java/tools/jackson/databind/jdk9/Java9ListsTest.java

+14-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import tools.jackson.databind.BaseMapTest;
99
import tools.jackson.databind.DefaultTyping;
1010
import tools.jackson.databind.ObjectMapper;
11+
import tools.jackson.databind.ObjectWriter;
1112
import tools.jackson.databind.json.JsonMapper;
1213
import tools.jackson.databind.testutil.NoCheckSubTypeValidator;
1314

@@ -17,7 +18,7 @@ public class Java9ListsTest extends BaseMapTest
1718
private final ObjectMapper MAPPER = JsonMapper.builder()
1819
.activateDefaultTypingAsProperty(
1920
NoCheckSubTypeValidator.instance,
20-
DefaultTyping.EVERYTHING,
21+
DefaultTyping.NON_FINAL,
2122
"@class"
2223
).build();
2324

@@ -39,56 +40,59 @@ public void testJava9ListOf() throws Exception
3940
System.err.println(" final? "+type.isFinal());
4041
}
4142
*/
42-
String actualJson = MAPPER.writeValueAsString(list);
43+
ObjectWriter w = MAPPER.writerFor(List.class);
44+
String actualJson = w.writeValueAsString(list);
4345
List<?> output = MAPPER.readValue(actualJson, List.class);
4446
assertEquals(1, output.size());
4547

4648
// and couple of alternatives:
4749
list = List.of("a", "b");
48-
actualJson = MAPPER.writeValueAsString(list);
50+
actualJson = w.writeValueAsString(list);
4951
output = MAPPER.readValue(actualJson, List.class);
5052
assertEquals(2, output.size());
5153

5254
list = List.of("a", "b", "c");
53-
actualJson = MAPPER.writeValueAsString(list);
55+
actualJson = w.writeValueAsString(list);
5456
output = MAPPER.readValue(actualJson, List.class);
5557
assertEquals(3, output.size());
5658

5759
list = List.of();
58-
actualJson = MAPPER.writeValueAsString(list);
60+
actualJson = w.writeValueAsString(list);
5961
output = MAPPER.readValue(actualJson, List.class);
6062
assertEquals(0, output.size());
6163
}
6264

6365
public void testJava9MapOf() throws Exception
6466
{
67+
ObjectWriter w = MAPPER.writerFor(Map.class);
6568
Map<String,String> map = Map.of("key", "value");
66-
String actualJson = MAPPER.writeValueAsString(map);
69+
String actualJson = w.writeValueAsString(map);
6770
Map<?,?> output = MAPPER.readValue(actualJson, Map.class);
6871
assertEquals(1, output.size());
6972

7073
// and alternatives
7174
map = Map.of("key", "value", "foo", "bar");
72-
actualJson = MAPPER.writeValueAsString(map);
75+
actualJson = w.writeValueAsString(map);
7376
output = MAPPER.readValue(actualJson, Map.class);
7477
assertEquals(2, output.size());
7578

7679
map = Map.of("key", "value", "foo", "bar", "last", "one");
77-
actualJson = MAPPER.writeValueAsString(map);
80+
actualJson = w.writeValueAsString(map);
7881
output = MAPPER.readValue(actualJson, Map.class);
7982
assertEquals(3, output.size());
8083

8184
map = Map.of();
82-
actualJson = MAPPER.writeValueAsString(map);
85+
actualJson = w.writeValueAsString(map);
8386
output = MAPPER.readValue(actualJson, Map.class);
8487
assertEquals(0, output.size());
8588
}
8689

8790
// [databind#3344]
8891
public void testJava9SetOf() throws Exception
8992
{
93+
ObjectWriter w = MAPPER.writerFor(Set.class);
9094
Set<?> set = Set.of("a", "b", "c");
91-
String actualJson = MAPPER.writeValueAsString(set);
95+
String actualJson = w.writeValueAsString(set);
9296
Set<?> output = MAPPER.readValue(actualJson, Set.class);
9397
assertTrue(output instanceof Set<?>);
9498
assertEquals(set, output);

src/test-jdk17/java/tools/jackson/databind/jdk17/Java17CollectionsTest.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import tools.jackson.databind.BaseMapTest;
88
import tools.jackson.databind.DefaultTyping;
99
import tools.jackson.databind.ObjectMapper;
10+
import tools.jackson.databind.ObjectWriter;
1011
import tools.jackson.databind.json.JsonMapper;
1112
import tools.jackson.databind.testutil.NoCheckSubTypeValidator;
1213

@@ -15,20 +16,21 @@ public class Java17CollectionsTest extends BaseMapTest
1516
private final ObjectMapper MAPPER = JsonMapper.builder()
1617
.activateDefaultTypingAsProperty(
1718
new NoCheckSubTypeValidator(),
18-
DefaultTyping.EVERYTHING,
19+
DefaultTyping.NON_FINAL,
1920
"@class"
2021
).build();
2122

2223
// [databind#3404]
2324
public void testJava9StreamOf() throws Exception
2425
{
26+
ObjectWriter w = MAPPER.writerFor(List.class);
2527
List<String> input = Stream.of("a", "b", "c").collect(Collectors.toList());
26-
String actualJson = MAPPER.writeValueAsString(input);
28+
String actualJson = w.writeValueAsString(input);
2729
List<?> result = MAPPER.readValue(actualJson, List.class);
2830
assertEquals(input, result);
2931

3032
input = Stream.of("a", "b", "c").toList();
31-
actualJson = MAPPER.writeValueAsString(input);
33+
actualJson = w.writeValueAsString(input);
3234
result = MAPPER.readValue(actualJson, List.class);
3335
assertEquals(input, result);
3436
}

src/test/java/tools/jackson/databind/jsontype/deftyping/TestDefaultForObject.java

+3
Original file line numberDiff line numberDiff line change
@@ -415,12 +415,15 @@ public void testWithFinalClass() throws Exception
415415
assertEquals(a2q("{'name':'abc'}"),
416416
mapper.writeValueAsString(new FinalStringBean("abc")));
417417

418+
// 23-Oct-2023, tatu: [databind#4160] Remove "EVERYTHING" option
419+
/*
418420
mapper = jsonMapperBuilder()
419421
.activateDefaultTyping(NoCheckSubTypeValidator.instance,
420422
DefaultTyping.EVERYTHING)
421423
.build();
422424
assertEquals(a2q("['"+FinalStringBean.class.getName()+"',{'name':'abc'}]"),
423425
mapper.writeValueAsString(new FinalStringBean("abc")));
426+
*/
424427
}
425428

426429
/*

src/test/java/tools/jackson/databind/jsontype/vld/BasicPTVKnownTypesTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class BasicPTVKnownTypesTest extends BaseMapTest
1313
.activateDefaultTyping(BasicPolymorphicTypeValidator.builder()
1414
.allowSubTypesWithExplicitDeserializer()
1515
.build(),
16-
DefaultTyping.EVERYTHING)
16+
DefaultTyping.NON_FINAL_AND_ENUMS)
1717
.build();
1818

1919
static class Dangerous {

src/test/java/tools/jackson/databind/jsontype/vld/CustomPTVMatchersTest.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package tools.jackson.databind.jsontype.vld;
22

3-
import java.net.URL;
3+
import java.util.TimeZone;
44

55
import tools.jackson.databind.BaseMapTest;
66
import tools.jackson.databind.DefaultTyping;
@@ -51,7 +51,7 @@ public void testCustomBaseMatchers() throws Exception
5151
.allowIfBaseType((ctxt, base) -> base.getName().startsWith("tools.jackson." ))
5252
.build();
5353
ObjectMapper mapper = jsonMapperBuilder()
54-
.activateDefaultTyping(ptv, DefaultTyping.EVERYTHING)
54+
.activateDefaultTyping(ptv, DefaultTyping.NON_FINAL)
5555
.build();
5656
// First: in this case, allow "Bad" one too (note: default typing based on
5757
// runtime type here)
@@ -60,12 +60,13 @@ public void testCustomBaseMatchers() throws Exception
6060
assertEquals(CustomBad.class, result.getClass());
6161

6262
// but other types not so good
63-
final String badJson = mapper.writeValueAsString(new URL("http://localhost") );
63+
// NOTE! Need to use non-final type (2.x used java.net.URL)
64+
final String badJson = mapper.writeValueAsString(TimeZone.getDefault());
6465
try {
65-
mapper.readValue(badJson, URL.class);
66+
mapper.readValue(badJson, TimeZone.class);
6667
fail("Should not pass");
6768
} catch (InvalidTypeIdException e) {
68-
verifyException(e, "Could not resolve type id 'java.net.URL'");
69+
verifyException(e, "Could not resolve type id 'java.util.TimeZone'");
6970
verifyException(e, "as a subtype of");
7071
}
7172
assertEquals(CustomBad.class, result.getClass());

0 commit comments

Comments
 (0)