Skip to content

Commit 4537f09

Browse files
authored
Fix #3948: retain transient field for ignoral if annotated with @JsonIgnoral (or similar) (#4048)
1 parent 5e94cb1 commit 4537f09

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

release-notes/CREDITS-2.x

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,3 +1673,7 @@ iProdigy (iProdigy@github)
16731673
(2.16.0)
16741674
* Contributed fix #4041: Actually cache EnumValues#internalMap
16751675
(2.16.0)
1676+
1677+
Jason Laber (jlaber@github)
1678+
* Reported #3948: `@JsonIgnore` no longer works for transient backing fields
1679+
(2.16.0)

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Project: jackson-databind
2020
(fix contributed by Joo-Hyuk K)
2121
#3928: `@JsonProperty` on constructor parameter changes default field serialization order
2222
(contributed by @pjfanning)
23+
#3948: `@JsonIgnore` no longer works for transient backing fields
24+
(reported by Jason L)
2325
#3950: Create new `JavaType` subtype `IterationType` (extending `SimpleType`)
2426
#3953: Use `JsonTypeInfo.Value` for annotation handling
2527
(contributed by Joo-Hyuk K)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public enum MapperFeature implements ConfigFeature
5858
* Feature is disabled by default, meaning that existence of `transient`
5959
* for a field does not necessarily lead to ignoral of getters or setters
6060
* but just ignoring the use of field for access.
61+
*<p>
62+
* NOTE! This should have no effect on <b>explicit</b> ignoral annotation
63+
* possibly added to {@code transient} fields: those should always have expected
64+
* semantics (same as if field was not {@code transient}).
6165
*
6266
* @since 2.6
6367
*/

src/main/java/com/fasterxml/jackson/databind/introspect/POJOPropertiesCollector.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,10 @@ protected void _addFields(Map<String, POJOPropertyBuilder> props)
612612
// only retain if also have ignoral annotations (for name or ignoral)
613613
if (transientAsIgnoral) {
614614
ignored = true;
615-
} else {
615+
616+
// 18-Jul-2023, tatu: [databind#3948] Need to retain if there was explicit
617+
// ignoral marker
618+
} else if (!ignored) {
616619
continue;
617620
}
618621
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.fasterxml.jackson.databind.ObjectMapper;
99
import java.io.Serializable;
1010

11+
// With [databind#3948] we should not drop `@JsonIgnore` regardless
12+
// of "transient" keyword.
1113
public class Transient3948Test extends BaseMapTest {
1214

1315
@JsonPropertyOrder(alphabetic = true)
@@ -43,9 +45,9 @@ public String getD() {
4345
}
4446
}
4547

46-
final ObjectMapper DEFAULT_MAPPER = newJsonMapper();
48+
private final ObjectMapper DEFAULT_MAPPER = newJsonMapper();
4749

48-
final ObjectMapper MAPPER_TRANSIENT = jsonMapperBuilder()
50+
private final ObjectMapper MAPPER_TRANSIENT = jsonMapperBuilder()
4951
.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true)
5052
.build();
5153

@@ -54,7 +56,7 @@ public void testJsonIgnoreSerialization() throws Exception {
5456

5557
String json = DEFAULT_MAPPER.writeValueAsString(obj1);
5658

57-
assertEquals(a2q("{'a':'hello','b':'world','cat':'jackson','dog':'databind'}"), json);
59+
assertEquals(a2q("{'a':'hello','cat':'jackson','dog':'databind'}"), json);
5860
}
5961

6062
public void testJsonIgnoreSerializationTransient() throws Exception {

0 commit comments

Comments
 (0)