Skip to content

Commit 073fc19

Browse files
committed
Fixed #1184
1 parent 8166ed2 commit 073fc19

File tree

10 files changed

+54
-24
lines changed

10 files changed

+54
-24
lines changed

release-notes/CREDITS

+4
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,7 @@ Lokesh Kumar (LokeshN@github)
474474
* Reported #1217: `@JsonIgnoreProperties` on Pojo fields not working for deserialization
475475
(2.8.0)
476476

477+
Maarten Billemont (lhunath@github)
478+
* Suggested #1184: Allow overriding of `transient` with explicit inclusion with `@JsonProperty`
479+
(2.8.0)
480+

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Project: jackson-databind
2828
(reported, contributed fix by Ross G)
2929
#1181: Add the ability to specify the initial capacity of the ArrayNode
3030
(suggested by Matt V, mveitas@github)
31+
#1184: Allow overriding of `transient` with explicit inclusion with `@JsonProperty`
32+
(suggested by Maarten B)
3133
#1187: Refactor `AtomicReferenceDeserializer` into `ReferenceTypeDeserializer`
3234
#1204: Add a convenience accessor `JavaType.hasContentType()` (true for container or reference type)
3335
#1206: Add "anchor type" member for `ReferenceType`

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,13 @@ public enum MapperFeature implements ConfigFeature
220220
ALLOW_FINAL_FIELDS_AS_MUTATORS(true),
221221

222222
/**
223-
* Feature that determines for <code>transient</code> modifier for fields
224-
* is handled: if disabled, it is only taken to mean exclusion of
223+
* Feature that determines how <code>transient</code> modifier for fields
224+
* is handled: if disabled, it is only taken to mean exclusion of the field
225+
* as accessor; if true, removal of the whole property.
225226
*<p>
226227
* Feature is disabled by default, meaning that existence of `transient`
227-
* for a field does not necessarily lead to ignoral of getters or setters.
228+
* for a field does not necessarily lead to ignoral of getters or setters
229+
* but just ignoring the use of field for access.
228230
*
229231
* @since 2.6
230232
*/

src/main/java/com/fasterxml/jackson/databind/deser/std/NumberDeserializers.java

-6
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,6 @@ public final T getNullValue(DeserializationContext ctxt) throws JsonMappingExcep
139139
return _nullValue;
140140
}
141141

142-
@Override
143-
@Deprecated // remove in 2.7
144-
public final T getNullValue() {
145-
return _nullValue;
146-
}
147-
148142
@Override
149143
public T getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
150144
// [databind#1095]: Should not allow coercion from into null from Empty String

src/main/java/com/fasterxml/jackson/databind/ext/NioPathDeserializer.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@ public Path deserialize(JsonParser p, DeserializationContext ctxt) throws IOExce
3030
ctxt.reportMappingException(Path.class, p.getCurrentToken());
3131
}
3232
final String value = p.getText();
33-
if (value.contains(":")) {
34-
try {
35-
URI uri = new URI(value);
36-
return Paths.get(uri);
37-
} catch (URISyntaxException e) {
38-
return (Path) ctxt.handleInstantiationProblem(handledType(), value, e);
39-
}
40-
} else {
41-
// If someone gives us an input with no : at all, treat as local path, instead of failing
42-
// with invalid URI.
33+
// If someone gives us an input with no : at all, treat as local path, instead of failing
34+
// with invalid URI.
35+
if (value.indexOf(':') < 0) {
4336
return Paths.get(value);
4437
}
38+
try {
39+
URI uri = new URI(value);
40+
return Paths.get(uri);
41+
} catch (URISyntaxException e) {
42+
return (Path) ctxt.handleInstantiationProblem(handledType(), value, e);
43+
}
4544
}
4645
}

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ protected void _addFields(Map<String, POJOPropertyBuilder> props)
367367
} else {
368368
pn = ai.findNameForDeserialization(f);
369369
}
370-
boolean nameExplicit = (pn != null);
370+
boolean hasName = (pn != null);
371+
boolean nameExplicit = hasName;
371372

372373
if (nameExplicit && pn.isEmpty()) { // empty String meaning "use default name", here just means "same as field name"
373374
pn = _propNameFromSimple(implName);
@@ -383,9 +384,13 @@ protected void _addFields(Map<String, POJOPropertyBuilder> props)
383384

384385
// 13-May-2015, tatu: Moved from earlier place (AnnotatedClass) in 2.6
385386
if (f.isTransient()) {
386-
visible = false;
387-
if (transientAsIgnoral) {
388-
ignored = true;
387+
// 20-May-2016, tatu: as per [databind#1184] explicit annotation should override
388+
// "default" `transient`
389+
if (!hasName) {
390+
visible = false;
391+
if (transientAsIgnoral) {
392+
ignored = true;
393+
}
389394
}
390395
}
391396
/* [databind#190]: this is the place to prune final fields, if they are not

src/main/java/com/fasterxml/jackson/databind/node/TextNode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public String textValue() {
5757
* base64 encoded; if so, they are decoded and resulting binary
5858
* data is returned.
5959
*/
60+
@SuppressWarnings("resource")
6061
public byte[] getBinaryValue(Base64Variant b64variant) throws IOException
6162
{
62-
@SuppressWarnings("resource")
6363
ByteArrayBuilder builder = new ByteArrayBuilder(100);
6464
final String str = _value;
6565
int ptr = 0;

src/main/java/com/fasterxml/jackson/databind/ser/impl/StringArraySerializer.java

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class StringArraySerializer
2828
/* Note: not clean in general, but we are betting against
2929
* anyone re-defining properties of String.class here...
3030
*/
31+
@SuppressWarnings("deprecation")
3132
private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(String.class);
3233

3334
public final static StringArraySerializer instance = new StringArraySerializer();

src/main/java/com/fasterxml/jackson/databind/ser/std/StdArraySerializers.java

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public static class BooleanArraySerializer
8686
extends ArraySerializerBase<boolean[]>
8787
{
8888
// as above, assuming no one re-defines primitive/wrapper types
89+
@SuppressWarnings("deprecation")
8990
private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(Boolean.class);
9091

9192
public BooleanArraySerializer() { super(boolean[].class); }
@@ -176,6 +177,7 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t
176177
public static class ShortArraySerializer extends TypedPrimitiveArraySerializer<short[]>
177178
{
178179
// as above, assuming no one re-defines primitive/wrapper types
180+
@SuppressWarnings("deprecation")
179181
private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(Short.TYPE);
180182

181183
public ShortArraySerializer() { super(short[].class); }
@@ -343,6 +345,7 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t
343345
public static class IntArraySerializer extends ArraySerializerBase<int[]>
344346
{
345347
// as above, assuming no one re-defines primitive/wrapper types
348+
@SuppressWarnings("deprecation")
346349
private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(Integer.TYPE);
347350

348351
public IntArraySerializer() { super(int[].class); }
@@ -432,6 +435,7 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t
432435
public static class LongArraySerializer extends TypedPrimitiveArraySerializer<long[]>
433436
{
434437
// as above, assuming no one re-defines primitive/wrapper types
438+
@SuppressWarnings("deprecation")
435439
private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(Long.TYPE);
436440

437441
public LongArraySerializer() { super(long[].class); }
@@ -525,6 +529,7 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t
525529
public static class FloatArraySerializer extends TypedPrimitiveArraySerializer<float[]>
526530
{
527531
// as above, assuming no one re-defines primitive/wrapper types
532+
@SuppressWarnings("deprecation")
528533
private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(Float.TYPE);
529534

530535
public FloatArraySerializer() {
@@ -616,6 +621,7 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t
616621
public static class DoubleArraySerializer extends ArraySerializerBase<double[]>
617622
{
618623
// as above, assuming no one re-defines primitive/wrapper types
624+
@SuppressWarnings("deprecation")
619625
private final static JavaType VALUE_TYPE = TypeFactory.defaultInstance().uncheckedSimpleType(Double.TYPE);
620626

621627
public DoubleArraySerializer() { super(double[].class); }

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

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.beans.Transient;
44

5+
import com.fasterxml.jackson.annotation.JsonProperty;
56
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
67
import com.fasterxml.jackson.databind.*;
78

@@ -30,6 +31,15 @@ static class BeanTransient {
3031
public int getY() { return 4; }
3132
}
3233

34+
// for [databind#1184]
35+
static class OverridableTransient {
36+
@JsonProperty
37+
// @JsonProperty("value") // should override transient here, to force inclusion
38+
public transient int value;
39+
40+
public OverridableTransient(int v) { value = v; }
41+
}
42+
3343
/*
3444
/**********************************************************
3545
/* Unit tests
@@ -58,4 +68,11 @@ public void testBeanTransient() throws Exception
5868
assertEquals(aposToQuotes("{'y':4}"),
5969
MAPPER.writeValueAsString(new BeanTransient()));
6070
}
71+
72+
// for [databind#1184]
73+
public void testOverridingTransient() throws Exception
74+
{
75+
assertEquals(aposToQuotes("{'value':38}"),
76+
MAPPER.writeValueAsString(new OverridableTransient(38)));
77+
}
6178
}

0 commit comments

Comments
 (0)