Skip to content

Commit 957ee39

Browse files
committed
Fix #3682 (prune transient Fields from being mutators)
1 parent 82caa1b commit 957ee39

File tree

6 files changed

+26
-4
lines changed

6 files changed

+26
-4
lines changed

release-notes/VERSION-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Project: jackson-databind
88

99
#3676: Allow use of `@JsonCreator(mode = Mode.PROPERTIES)` creator for POJOs
1010
with"empty String" coercion
11+
#3682: Transient `Field`s are not ignored as Mutators if there is visible Getter
1112

1213
2.14.1 (21-Nov-2022)
1314

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public enum MapperFeature implements ConfigFeature
5353
/**
5454
* Feature that determines how <code>transient</code> modifier for fields
5555
* is handled: if disabled, it is only taken to mean exclusion of the field
56-
* as accessor; if true, it is taken to imply removal of the whole property.
56+
* as an accessor; if true, it is taken to imply removal of the whole property.
5757
*<p>
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

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,12 @@ protected void _addFields(Map<String, POJOPropertyBuilder> props)
582582
// 20-May-2016, tatu: as per [databind#1184] explicit annotation should override
583583
// "default" `transient`
584584
if (!hasName) {
585-
visible = false;
585+
// 25-Nov-2022, tatu: [databind#3682] Drop transient Fields early;
586+
// only retain if also have ignoral annotations (for name or ignoral)
586587
if (transientAsIgnoral) {
587588
ignored = true;
589+
} else {
590+
continue;
588591
}
589592
}
590593
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ public JsonProperty.Access removeNonVisible(boolean inferMutators,
950950
case AUTO: // the default case: base it on visibility
951951
_getters = _removeNonVisible(_getters);
952952
_ctorParameters = _removeNonVisible(_ctorParameters);
953-
953+
954954
if (!inferMutators || (_getters == null)) {
955955
_fields = _removeNonVisible(_fields);
956956
_setters = _removeNonVisible(_setters);

src/test/java/com/fasterxml/jackson/databind/format/CollectionFormatShapeTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,4 @@ public void testListAsObjectRoundtrip() throws Exception
5555
CollectionAsPOJO result = MAPPER.readValue(json, CollectionAsPOJO.class);
5656
assertEquals(2, result.size());
5757
}
58-
5958
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.annotation.JsonProperty;
66
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
77
import com.fasterxml.jackson.databind.*;
8+
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
89

910
/**
1011
* Tests for both `transient` keyword and JDK 7
@@ -45,6 +46,12 @@ static class OverridableTransient {
4546
public OverridableTransient(int v) { tValue = v; }
4647
}
4748

49+
static class TransientToPrune {
50+
public transient int a;
51+
52+
public int getA() { return a; }
53+
}
54+
4855
/*
4956
/**********************************************************
5057
/* Unit tests
@@ -83,4 +90,16 @@ public void testOverridingTransient() throws Exception
8390
assertEquals(a2q("{'tValue':38}"),
8491
MAPPER.writeValueAsString(new OverridableTransient(38)));
8592
}
93+
94+
// for [databind#3682]: SHOULD prune `transient` Field, not pull in
95+
public void testTransientToPrune() throws Exception
96+
{
97+
try {
98+
TransientToPrune result = MAPPER.readValue("{\"a\":3}",
99+
TransientToPrune.class);
100+
fail("Should not pass, got: "+result);
101+
} catch (UnrecognizedPropertyException e) {
102+
verifyException(e, "Unrecognized", "\"a\"");
103+
}
104+
}
86105
}

0 commit comments

Comments
 (0)