Skip to content

Commit 8ea1c07

Browse files
committed
Fixed #2789
1 parent 781457b commit 8ea1c07

File tree

5 files changed

+114
-137
lines changed

5 files changed

+114
-137
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Project: jackson-databind
1010
`createGenerator()`
1111
#2785: Polymorphic subtypes not registering on copied ObjectMapper (2.11.1)
1212
(reported, fix contributed by Joshua S)
13+
#2789: Failure to read AnnotatedField value in Jackson 2.11
14+
(reported by isaki@github)
1315
#2796: `TypeFactory.constructType()` does not take `TypeBindings` correctly
1416
(reported by Daniel H)
1517

src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerBuilder.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,23 @@ public boolean hasProperties() {
181181
*/
182182
public JsonSerializer<?> build()
183183
{
184+
// [databind#2789]: There can be a case wherein `_typeId` is used, but
185+
// nothing else. Rare but has happened; so force access.
186+
if (_typeId != null) {
187+
if (_config.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)) {
188+
_typeId.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
189+
}
190+
}
191+
if (_anyGetter != null) {
192+
_anyGetter.fixAccess(_config);
193+
}
194+
184195
BeanPropertyWriter[] properties;
185196
// No properties, any getter or object id writer?
186197
// No real serializer; caller gets to handle
187198
if (_properties == null || _properties.isEmpty()) {
188199
if (_anyGetter == null && _objectIdWriter == null) {
200+
// NOTE! Caller may still call `createDummy()` later on
189201
return null;
190202
}
191203
properties = NO_PROPERTIES;
@@ -205,14 +217,6 @@ public JsonSerializer<?> build()
205217
_properties.size(), _filteredProperties.length));
206218
}
207219
}
208-
if (_anyGetter != null) {
209-
_anyGetter.fixAccess(_config);
210-
}
211-
if (_typeId != null) {
212-
if (_config.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)) {
213-
_typeId.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
214-
}
215-
}
216220
return new BeanSerializer(_beanDesc.getType(), this,
217221
properties, _filteredProperties);
218222
}

src/test/java/com/fasterxml/jackson/databind/introspect/AutoDetect1947Test.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/test/java/com/fasterxml/jackson/databind/introspect/TestAutoDetect.java

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.fasterxml.jackson.databind.introspect;
22

3+
import java.util.Objects;
4+
35
import com.fasterxml.jackson.annotation.*;
46
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
7+
58
import com.fasterxml.jackson.core.*;
9+
610
import com.fasterxml.jackson.databind.*;
711
import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
812

@@ -35,13 +39,84 @@ public void setValue(int x) {
3539
}
3640
}
3741

42+
// [databind#1947]
43+
static class Entity1947 {
44+
public int shouldBeDetected;
45+
public String shouldNotBeDetected;
46+
47+
@JsonProperty
48+
public int getShouldBeDetected() {
49+
return shouldBeDetected;
50+
}
51+
52+
public void setShouldBeDetected(int shouldBeDetected) {
53+
this.shouldBeDetected = shouldBeDetected;
54+
}
55+
56+
public String getShouldNotBeDetected() {
57+
return shouldNotBeDetected;
58+
}
59+
60+
public void setShouldNotBeDetected(String shouldNotBeDetected) {
61+
this.shouldNotBeDetected = shouldNotBeDetected;
62+
}
63+
}
64+
65+
// For [databind#2789]
66+
67+
@SuppressWarnings("unused")
68+
@JsonAutoDetect(
69+
getterVisibility = JsonAutoDetect.Visibility.NONE,
70+
creatorVisibility = JsonAutoDetect.Visibility.NONE,
71+
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
72+
fieldVisibility = JsonAutoDetect.Visibility.NONE,
73+
setterVisibility = JsonAutoDetect.Visibility.NONE)
74+
@JsonTypeInfo(
75+
use = JsonTypeInfo.Id.NAME,
76+
include = JsonTypeInfo.As.PROPERTY,
77+
property = "type",
78+
visible = true)
79+
@JsonSubTypes({
80+
@JsonSubTypes.Type(name = "CLASS_A", value = DataClassA.class)
81+
})
82+
private static abstract class DataParent2789 {
83+
84+
@JsonProperty("type")
85+
@JsonTypeId
86+
private final DataType2789 type;
87+
88+
DataParent2789() {
89+
super();
90+
this.type = null;
91+
}
92+
93+
DataParent2789(final DataType2789 type) {
94+
super();
95+
this.type = Objects.requireNonNull(type);
96+
}
97+
98+
public DataType2789 getType() {
99+
return this.type;
100+
}
101+
}
102+
103+
private static final class DataClassA extends DataParent2789 {
104+
DataClassA() {
105+
super(DataType2789.CLASS_A);
106+
}
107+
}
108+
109+
private enum DataType2789 {
110+
CLASS_A;
111+
}
112+
38113
/*
39114
/********************************************************
40115
/* Unit tests
41116
/********************************************************
42117
*/
43118

44-
private final ObjectMapper MAPPER = new ObjectMapper();
119+
private final ObjectMapper MAPPER = newJsonMapper();
45120

46121
public void testPrivateCtor() throws Exception
47122
{
@@ -101,4 +176,28 @@ public void testVisibilityConfigOverridesForDeser() throws Exception
101176
Feature1347DeserBean result = mapper.readValue(JSON, Feature1347DeserBean.class);
102177
assertEquals(3, result.value);
103178
}
179+
180+
// [databind#1947]
181+
public void testDisablingAll() throws Exception
182+
{
183+
ObjectMapper mapper = jsonMapperBuilder()
184+
.disable(MapperFeature.AUTO_DETECT_SETTERS)
185+
.disable(MapperFeature.AUTO_DETECT_FIELDS)
186+
.disable(MapperFeature.AUTO_DETECT_GETTERS)
187+
.disable(MapperFeature.AUTO_DETECT_CREATORS)
188+
.disable(MapperFeature.AUTO_DETECT_IS_GETTERS)
189+
.build();
190+
String json = mapper.writeValueAsString(new Entity1947());
191+
JsonNode n = mapper.readTree(json);
192+
assertEquals(1, n.size());
193+
assertTrue(n.has("shouldBeDetected"));
194+
assertFalse(n.has("shouldNotBeDetected"));
195+
}
196+
197+
// [databind#2789]
198+
public void testAnnotatedFieldIssue2789() throws Exception {
199+
final String json = MAPPER.writeValueAsString(new DataClassA());
200+
final DataParent2789 copy = MAPPER.readValue(json, DataParent2789.class);
201+
assertEquals(DataType2789.CLASS_A, copy.getType());
202+
}
104203
}

src/test/java/com/fasterxml/jackson/failing/TestAutoDetect2789.java

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)