diff options
author | Tatu Saloranta <tatu.saloranta@iki.fi> | 2020-07-18 12:44:16 -0700 |
---|---|---|
committer | Tatu Saloranta <tatu.saloranta@iki.fi> | 2020-07-18 12:44:16 -0700 |
commit | b3e5e6a5d771383d7ad2250cf54ac3c170f6d15d (patch) | |
tree | 51e2deef4a7562940e503c315d8fc917abca9576 /src | |
parent | 1a3a3e1707fdb4324e4f08258da36364c2dbd737 (diff) | |
parent | 8ea1c077f1ad903183192b09a2ded32dda04a8de (diff) | |
download | jackson-databind-b3e5e6a5d771383d7ad2250cf54ac3c170f6d15d.tar.gz |
Merge branch '2.11' into 2.12
Diffstat (limited to 'src')
4 files changed, 112 insertions, 137 deletions
diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerBuilder.java b/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerBuilder.java index e1911405f..339befd4a 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerBuilder.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerBuilder.java @@ -181,11 +181,23 @@ public class BeanSerializerBuilder */ public JsonSerializer<?> build() { + // [databind#2789]: There can be a case wherein `_typeId` is used, but + // nothing else. Rare but has happened; so force access. + if (_typeId != null) { + if (_config.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)) { + _typeId.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS)); + } + } + if (_anyGetter != null) { + _anyGetter.fixAccess(_config); + } + BeanPropertyWriter[] properties; // No properties, any getter or object id writer? // No real serializer; caller gets to handle if (_properties == null || _properties.isEmpty()) { if (_anyGetter == null && _objectIdWriter == null) { + // NOTE! Caller may still call `createDummy()` later on return null; } properties = NO_PROPERTIES; @@ -205,14 +217,6 @@ public class BeanSerializerBuilder _properties.size(), _filteredProperties.length)); } } - if (_anyGetter != null) { - _anyGetter.fixAccess(_config); - } - if (_typeId != null) { - if (_config.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)) { - _typeId.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS)); - } - } return new BeanSerializer(_beanDesc.getType(), this, properties, _filteredProperties); } diff --git a/src/test/java/com/fasterxml/jackson/databind/introspect/AutoDetect1947Test.java b/src/test/java/com/fasterxml/jackson/databind/introspect/AutoDetect1947Test.java deleted file mode 100644 index 6956e9650..000000000 --- a/src/test/java/com/fasterxml/jackson/databind/introspect/AutoDetect1947Test.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.fasterxml.jackson.databind.introspect; - -import com.fasterxml.jackson.annotation.JsonProperty; - -import com.fasterxml.jackson.databind.*; - -// Test(s) for [databind#1947], regression for 2.9 -public class AutoDetect1947Test extends BaseMapTest -{ - static class Entity1947 { - public int shouldBeDetected; - public String shouldNotBeDetected; - - @JsonProperty - public int getShouldBeDetected() { - return shouldBeDetected; - } - - public void setShouldBeDetected(int shouldBeDetected) { - this.shouldBeDetected = shouldBeDetected; - } - - public String getShouldNotBeDetected() { - return shouldNotBeDetected; - } - - public void setShouldNotBeDetected(String shouldNotBeDetected) { - this.shouldNotBeDetected = shouldNotBeDetected; - } - } - public void testDisablingAll() throws Exception - { - ObjectMapper mapper = jsonMapperBuilder() - .disable(MapperFeature.AUTO_DETECT_SETTERS) - .disable(MapperFeature.AUTO_DETECT_FIELDS) - .disable(MapperFeature.AUTO_DETECT_GETTERS) - .disable(MapperFeature.AUTO_DETECT_CREATORS) - .disable(MapperFeature.AUTO_DETECT_IS_GETTERS) - .build(); - String json = mapper.writeValueAsString(new Entity1947()); - JsonNode n = mapper.readTree(json); - assertEquals(1, n.size()); - assertTrue(n.has("shouldBeDetected")); - assertFalse(n.has("shouldNotBeDetected")); - } -} diff --git a/src/test/java/com/fasterxml/jackson/databind/introspect/TestAutoDetect.java b/src/test/java/com/fasterxml/jackson/databind/introspect/TestAutoDetect.java index 9ab933d33..55f2aa9e9 100644 --- a/src/test/java/com/fasterxml/jackson/databind/introspect/TestAutoDetect.java +++ b/src/test/java/com/fasterxml/jackson/databind/introspect/TestAutoDetect.java @@ -1,8 +1,12 @@ package com.fasterxml.jackson.databind.introspect; +import java.util.Objects; + import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; + import com.fasterxml.jackson.core.*; + import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.introspect.VisibilityChecker; @@ -35,13 +39,84 @@ public class TestAutoDetect } } + // [databind#1947] + static class Entity1947 { + public int shouldBeDetected; + public String shouldNotBeDetected; + + @JsonProperty + public int getShouldBeDetected() { + return shouldBeDetected; + } + + public void setShouldBeDetected(int shouldBeDetected) { + this.shouldBeDetected = shouldBeDetected; + } + + public String getShouldNotBeDetected() { + return shouldNotBeDetected; + } + + public void setShouldNotBeDetected(String shouldNotBeDetected) { + this.shouldNotBeDetected = shouldNotBeDetected; + } + } + + // For [databind#2789] + + @SuppressWarnings("unused") + @JsonAutoDetect( + getterVisibility = JsonAutoDetect.Visibility.NONE, + creatorVisibility = JsonAutoDetect.Visibility.NONE, + isGetterVisibility = JsonAutoDetect.Visibility.NONE, + fieldVisibility = JsonAutoDetect.Visibility.NONE, + setterVisibility = JsonAutoDetect.Visibility.NONE) + @JsonTypeInfo( + use = JsonTypeInfo.Id.NAME, + include = JsonTypeInfo.As.PROPERTY, + property = "type", + visible = true) + @JsonSubTypes({ + @JsonSubTypes.Type(name = "CLASS_A", value = DataClassA.class) + }) + private static abstract class DataParent2789 { + + @JsonProperty("type") + @JsonTypeId + private final DataType2789 type; + + DataParent2789() { + super(); + this.type = null; + } + + DataParent2789(final DataType2789 type) { + super(); + this.type = Objects.requireNonNull(type); + } + + public DataType2789 getType() { + return this.type; + } + } + + private static final class DataClassA extends DataParent2789 { + DataClassA() { + super(DataType2789.CLASS_A); + } + } + + private enum DataType2789 { + CLASS_A; + } + /* /******************************************************** /* Unit tests /******************************************************** */ - private final ObjectMapper MAPPER = new ObjectMapper(); + private final ObjectMapper MAPPER = newJsonMapper(); public void testPrivateCtor() throws Exception { @@ -101,4 +176,28 @@ public class TestAutoDetect Feature1347DeserBean result = mapper.readValue(JSON, Feature1347DeserBean.class); assertEquals(3, result.value); } + + // [databind#1947] + public void testDisablingAll() throws Exception + { + ObjectMapper mapper = jsonMapperBuilder() + .disable(MapperFeature.AUTO_DETECT_SETTERS) + .disable(MapperFeature.AUTO_DETECT_FIELDS) + .disable(MapperFeature.AUTO_DETECT_GETTERS) + .disable(MapperFeature.AUTO_DETECT_CREATORS) + .disable(MapperFeature.AUTO_DETECT_IS_GETTERS) + .build(); + String json = mapper.writeValueAsString(new Entity1947()); + JsonNode n = mapper.readTree(json); + assertEquals(1, n.size()); + assertTrue(n.has("shouldBeDetected")); + assertFalse(n.has("shouldNotBeDetected")); + } + + // [databind#2789] + public void testAnnotatedFieldIssue2789() throws Exception { + final String json = MAPPER.writeValueAsString(new DataClassA()); + final DataParent2789 copy = MAPPER.readValue(json, DataParent2789.class); + assertEquals(DataType2789.CLASS_A, copy.getType()); + } } diff --git a/src/test/java/com/fasterxml/jackson/failing/TestAutoDetect2789.java b/src/test/java/com/fasterxml/jackson/failing/TestAutoDetect2789.java deleted file mode 100644 index b2b703340..000000000 --- a/src/test/java/com/fasterxml/jackson/failing/TestAutoDetect2789.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.fasterxml.jackson.failing; - -import java.util.*; - -import com.fasterxml.jackson.annotation.*; - -import com.fasterxml.jackson.databind.*; - -/** - * Unit tests for checking extended auto-detect configuration, - * in context of serialization - */ -public class TestAutoDetect2789 - extends BaseMapTest -{ - // For [databind#2789] - - @SuppressWarnings("unused") - @JsonAutoDetect( - getterVisibility = JsonAutoDetect.Visibility.NONE, - creatorVisibility = JsonAutoDetect.Visibility.NONE, - isGetterVisibility = JsonAutoDetect.Visibility.NONE, - fieldVisibility = JsonAutoDetect.Visibility.NONE, - setterVisibility = JsonAutoDetect.Visibility.NONE) - @JsonTypeInfo( - use = JsonTypeInfo.Id.NAME, - include = JsonTypeInfo.As.PROPERTY, - property = "type", - visible = true) - @JsonSubTypes({ - @JsonSubTypes.Type(name = "CLASS_A", value = DataClassA.class) - }) - private static abstract class DataParent2789 { - - @JsonProperty("type") - @JsonTypeId - private final DataType2789 type; - - DataParent2789() { - super(); - this.type = null; - } - - DataParent2789(final DataType2789 type) { - super(); - this.type = Objects.requireNonNull(type); - } - - public DataType2789 getType() { - return this.type; - } - } - - private static final class DataClassA extends DataParent2789 { - DataClassA() { - super(DataType2789.CLASS_A); - } - } - - private enum DataType2789 { - CLASS_A; - } - - /* - /********************************************************* - /* Test methods - /********************************************************* - */ - - private final ObjectMapper MAPPER = newJsonMapper(); - - // [databind#2789] - - public void testAnnotatedFieldIssue2789() throws Exception { - final DataParent2789 test = new DataClassA(); - - final String json = MAPPER.writeValueAsString(test); - - final DataParent2789 copy = MAPPER.readValue(json, DataParent2789.class); - assertEquals(DataType2789.CLASS_A, copy.getType()); - } -} |